Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>One thing you can take advantage of is the trapExit, case Exit and exit, system. In your main actor, you can set trapExit to true:</p> <pre><code>// Foreman def act() { trapExit = true link(workerA) link(workerB) ... } </code></pre> <p>This means that your foreman actor will get an Exit message when the worker process terminates, including a reason:</p> <pre><code>// Foreman def act() { .... loop { react { case Exit (worker: Actor, reason: AnyRef) =&gt; { // decrement counter, or list of workers, and exit if empty } ... }} } </code></pre> <p>You can pattern match on the reason parameter. For instance, in your worker class, you might exit using a variety of case classes indicating what the foreman should do:</p> <pre><code>// Worker: exit(WorkComplete) exit(Emergency) </code></pre> <p>etc, and so on. When your worker throws an exception, it'll terminate and send the linked process an Exit message containing the exception. Given this sort of thing, you can end up with something like:</p> <pre><code>// Foreman def act() { .... loop { react { case Exit (worker: Actor, reason: WorkComplete) =&gt; { // decrement counter, or list of workers, and exit if empty } case Exit (worker: Actor, reason: TasksExhausted) =&gt; { // log something, or make shut down worker if too many are exhausted } case Exit (worker: Actor, reason: Exception) =&gt; { // log the exception, then restart the actor } ... }} } </code></pre> <p>It's unclear from your initial question if you want the workers to keep on working even when they're done until the foreman tells them they should exit when it's time. If that's the case, sending the workers a messages telling them to "exit when finished" works, and you can tell they've finished by using the trapExit mechanism.</p> <p>Hope this spurs an interesting solution!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload