LAgent: an agent framework in F# – Part IV – Custom error management

-

Download frame­work here.

All posts are here:

Custom error management

In the last part we saw what hap­pens by de­fault in the frame­work when an er­ror oc­curs. But that might not be what you want. You might want to have your so­phis­ti­cated er­ror de­tec­tion and re­cov­ery dis­trib­uted al­go­rithm.

To make such a thing pos­si­ble each agent has a man­ager. The man­ager is an agent that gets called when­ever an er­ror oc­curs in the agent it is mon­i­tor­ing.

In code:

let manager = spawnWorker (fun (agent, name:string, ex:Exception, msg:obj,
state, initialState) -> printfn "%s restarting ..." name; agent <-- Restart) counter1 <-- SetManager(manager)

Whenever an er­ror is gen­er­ated the man­ager re­ceives a tu­ple of:

(agent, name, ex­cep­tion, mes­sage, cur­rentState, ini­ti­tial­State)

This man­ager prints out some­thing and then restarts the agent. Let’s trig­ger an er­ror by post­ing the wrong mes­sage:

counter1 <-- "afdaf"
counter1 <-- 2

The ex­pec­ta­tion is that the counter will restart from 0 when­ever an er­ror is trig­gered. This is what hap­pens:

**Bob restart­ing

From 0 to 2**

Which is what we ex­pected. Obviously this is not a very so­phis­ti­cated er­ror re­cov­ery al­go­rithm. You might want to do some­thing more mean­ing­ful. Hopefully you have enough in­for­ma­tion to build what­ever you need.

A par­tic­u­larly im­por­tant class of un­ex­pected event is time­outs. We’ll talk about them next.

Tags