F# – Starting an Agent with supervision

In Erlang, we have the Super­vi­sor behav­iour which makes it very easy to pro­vide the means to mon­i­tor and restart a whole net­work of work­ers and oth­er super­vi­sors based on some con­fig­ured strat­e­gy.

The Mail­box­Proces­sor (aka agents) in F# doesn’t come with the same high­er-lev­el abstrac­tions (such as Erlang’s gen_server, gen_fsm behav­iours) by default, but it’s very easy to mim­ic some of their capa­bil­i­ties by build­ing on top of the stan­dard F# agents.

Luca Bolognese’s light­weight LAgent frame­work is a per­fect exam­ple and even pro­vides inter­est­ing pos­si­bil­i­ties such as hot swap­ping of code (although still some way off of what’s pos­si­ble in Erlang in this regard).

Sim­i­lar­ly, the fol­low­ing snip­pet shows how you can start an agent with super­vi­sion so that any escaped excep­tions (which will crash the agent) are trapped and restarts the agent: