Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best practice way to initialize an actor from the database

I have a top level actor (under the guardian), called Groups which on startup needs to load the list of groups from the database and create a bunch of child actors based on those groups in the database.

I have placed the database load code inside of the preStart function as I don't want any messages to be processed before the groups are loaded.

Currently my Groups actor looks like this;

var groups: Map[String, ActorRef] = Map()

override def preStart() = {
  groups = getGroupsFromDB() map createGroup
}

def createGroup(pair: (String, Long)) = {
  val (name, id) = pair
  val group = context.actorOf(Props(new Group(id, name)), name = name)

  name -> group
}

However I don't believe this is the best way to handle this, as what happens if the database server is not available? So what is the best pratice way of handling data initialization from a database?

like image 406
DomBlack Avatar asked Nov 13 '22 16:11

DomBlack


1 Answers

The Akka documentation explains how to supervise top level actors for fault-tolerance.

You can apply the principles there to manage the exceptions you may find if the DB is not available.

like image 75
Pere Villega Avatar answered Nov 15 '22 06:11

Pere Villega