import std/io import std/process. { type Pid , type Subject , type proc , Crashed , Down , ExitSignal , Killed , Normal , Shutdown } import std/supervisor. {ChildWith, OneForOne, Permanent, Spec, Temporary, Transient} // Supervisors with windowed restart intensity, child kinds and an ordered // shutdown, and `process.stop` (ADR 0060). Children report to the main // actor, which prints; every step waits for the one before, so the order // is the same on any number of threads. fn reason :: r: process.ExitReason -> String { match r { Normal -> "normal" , Killed -> "killed" , Crashed(m) -> "crashed: " <> m , Shutdown -> "shutdown" } } fn wait_down :: down: Subject -> String / { let Down(_, why) = process.receive_forever down reason why } /// An actor counting attempts: each `Subject` sent gets the next. fn counter :: -> Subject> / { let ready = process.subject () let _ = process.spawn fn { let asks = process.subject () process.send ready asks count asks 1 } process.receive_forever ready } fn count :: asks: Subject> n: Int -> () / { let reply = process.receive_forever asks process.send reply n count asks (n + 1) } fn attempt :: attempts: Subject> -> Int / { let reply = process.subject () process.send attempts reply process.receive_forever reply } /// A child that tells `log` it started, then, trapping exits, waits to be /// stopped and tells `log` it stops. fn polite :: name: String log: Subject -> () / { process.trap_exits True process.send log (name <> " started") let ExitSignal(_, why) = process.receive_forever (process.exits ()) process.send log (name <> " stopping (" <> reason why <> ")") } fn print_next :: log: Subject n: Int -> () / { if n > 0 { io.println (process.receive_forever log) print_next log (n - 1) } else { () } } fn wait_for :: log: Subject n: Int -> () / { if n > 0 { let _ = process.receive_forever log wait_for log (n - 1) } else { () } } pub fn main :: -> () / { // `stop` stops an actor that does not trap exits... let sleeper = { use <- process.spawn let never: Subject = process.subject () let _ = process.receive_forever never } let down = process.monitor sleeper process.stop sleeper io.println ("sleeper: " <> wait_down down) //> sleeper: shutdown // ...and tells one that does, which cleans up and returns. let log = process.subject () let careful = { use <- process.spawn polite "careful" log } let down = process.monitor careful print_next log 1 process.stop careful print_next log 1 io.println ("careful: " <> wait_down down) //> careful started //> careful stopping (shutdown) //> careful: normal // More than 3 restarts within a second: the supervisor gives up. let (_, fast_down) = { use <- process.spawn_monitor supervisor.run Spec( OneForOne , [ ChildWith( "fast" , fn { process.send log "fast started" panic "fast failed" } , Permanent , 1000 ) ] , 3 , 1000 ) } print_next log 4 io.println ("fast supervisor: " <> wait_down fast_down) //> fast started //> fast started //> fast started //> fast started //> fast supervisor: crashed: supervisor: too many restarts // The same crashes spread over more than the window: it keeps going. // (The clock is virtual: no socket is open.) let attempts = counter () let (_, slow_down) = { use <- process.spawn_monitor supervisor.run Spec( OneForOne , [ ChildWith( "slow" , fn { let n = attempt attempts process.send log ("slow attempt " <> show n) process.sleep 600 if n < 4 { panic "slow failed" } else { () } } , Transient , 1000 ) ] , 1 , 500 ) } print_next log 4 io.println ("slow supervisor: " <> wait_down slow_down) //> slow attempt 1 //> slow attempt 2 //> slow attempt 3 //> slow attempt 4 //> slow supervisor: normal // Kinds: a temporary child is not restarted after a crash (and its // supervisor, left without children, returns)... let (_, once_down) = { use <- process.spawn_monitor supervisor.run Spec( OneForOne , [ ChildWith( "once" , fn { process.send log "temporary ran" panic "temporary failed" } , Temporary , 1000 ) ] , 5 , 60000 ) } print_next log 1 io.println ("temporary supervisor: " <> wait_down once_down) //> temporary ran //> temporary supervisor: normal // ...a permanent one is restarted after returning. let returns = counter () let (kinds, kinds_down) = { use <- process.spawn_monitor supervisor.run Spec( OneForOne , [ ChildWith( "again" , fn { let n = attempt returns if n < 3 { process.send log ("permanent returned " <> show n) } else { polite "permanent" log } } , Permanent , 1000 ) ] , 5 , 60000 ) } print_next log 3 process.stop kinds print_next log 1 io.println ("permanent supervisor: " <> wait_down kinds_down) //> permanent returned 1 //> permanent returned 2 //> permanent started //> permanent stopping (shutdown) //> permanent supervisor: normal // An ordered shutdown: last started, first stopped; a nested supervisor // stops its own children first; a child that ignores the shutdown is // killed after its timeout. let stubborn_pid = process.subject () let (tree, tree_down) = { use <- process.spawn_monitor supervisor.run Spec( OneForOne , [ ChildWith("a", fn { polite "a" log }, Permanent, 1000) , ChildWith( "inner" , fn { supervisor.run Spec( OneForOne , [ChildWith("leaf", fn { polite "leaf" log }, Permanent, 1000)] , 1 , 1000 ) } , Permanent , 5000 ) , ChildWith( "stubborn" , fn { process.trap_exits True process.send stubborn_pid (process.self ()) let never: Subject = process.subject () let _ = process.receive_forever never } , Permanent , 200 ) ] , 1 , 1000 ) } // `a` and `leaf` start in either order. wait_for log 2 let stubborn = process.receive_forever stubborn_pid let stubborn_down = process.monitor stubborn process.stop tree let Down(_, why) = process.receive_forever stubborn_down io.println ("stubborn: " <> reason why) print_next log 2 io.println ("tree supervisor: " <> wait_down tree_down) //> stubborn: killed //> leaf stopping (shutdown) //> a stopping (shutdown) //> tree supervisor: normal }