import std/io import std/process.{type proc, Crashed, Down} import std/supervisor.{Child, OneForAll, OneForOne, RestForOne} pub fn main :: -> () / { // A child that always crashes is restarted twice, then the supervisor // gives up and crashes itself. let sup = { use <- process.spawn supervisor.supervise OneForOne [ Child( "flaky" , fn { io.println "worker started" panic "worker failed" } ) ] 2 } let Down(_, why) = process.receive_forever (process.monitor sup) match why { Crashed(m) -> io.println ("supervisor down: " <> m) , _ -> io.println "supervisor stopped" } // A child that finishes normally is not restarted. let calm = { use <- process.spawn supervisor.supervise OneForOne [Child("once", fn { io.println "one-shot job" })] 5 io.println "supervisor done" } let Down(_, _) = process.receive_forever (process.monitor calm) // One for all: a crash restarts the healthy sibling too; the sibling's // own exit (it is killed) is not counted as another crash. let pair = { use <- process.spawn supervisor.supervise OneForAll [ Child( "a" , fn { io.println "a started" panic "a failed" } ) , Child( "b" , fn { io.println "b started" let never: process.Subject = process.subject () process.receive_forever never () } ) ] 1 } let Down(_, _) = process.receive_forever (process.monitor pair) io.println "pair supervisor gave up" // Rest for one: a crash restarts the crashed child and the ones started // after it, not the ones before. let chain = { use <- process.spawn supervisor.supervise RestForOne [ Child("first", fn { idle "first" }) , Child( "second" , fn { io.println "second started" panic "second failed" } ) , Child("third", fn { idle "third" }) ] 1 } let Down(_, _) = process.receive_forever (process.monitor chain) io.println "chain supervisor gave up" } fn idle :: name: String -> () / { io.println (name <> " started") let never: process.Subject = process.subject () process.receive_forever never () } //> worker started //> worker started //> worker started //> supervisor down: supervisor: too many restarts //> one-shot job //> supervisor done //> a started //> b started //> a started //> b started //> pair supervisor gave up //> first started //> second started //> third started //> second started //> third started //> chain supervisor gave up