Snippets

Tuomas Hietanen Mailbox processor that won't die on errors

Created by Tuomas Hietanen
let internal createMailboxProcessor() = 
    MailboxProcessor.Start(fun inbox ->
        let rec loop () = async {
            let! msg = inbox.Receive()
            //...
            return! loop ()
        }
        loop ())

let mutable internal backgroundProcessor = createMailboxProcessor()

let rec setErrorHandler (p:MailboxProcessor<_>) =
    p.Error.Add(fun e ->
        printfn "Mailbox died. Spawning a new one. Error: %O" e
        backgroundProcessor <- createMailboxProcessor()
        setErrorHandler backgroundProcessor
    )
setErrorHandler backgroundProcessor

// Note: This is not ensuring the handling of the existing queue:
// backgroundProcessor.CurrentQueueLength
// You may want to retry some messages with some logics to skip the ones causing the error


Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.