| @@ -1,6 +1,6 @@ |
| 1 | 1 | import std/io |
| 2 | 2 | import std/process.{type proc, Down, Crashed} |
| 3 | | −import std/supervisor.{Child, OneForOne, OneForAll} |
| 3 | +import std/supervisor.{Child, OneForOne, OneForAll, RestForOne} |
| 4 | 4 | |
| 5 | 5 | pub fn main() -> () / <io, proc> { |
| 6 | 6 | // A child that always crashes is restarted twice, then the supervisor |
| @@ -40,6 +40,27 @@ pub fn main() -> () / <io, proc> { |
| 40 | 40 | }) |
| 41 | 41 | let Down(_, _) = process.receive_forever(process.monitor(pair)) |
| 42 | 42 | io.println("pair supervisor gave up") |
| 43 | + // Rest for one: a crash restarts the crashed child and the ones started |
| 44 | + // after it, not the ones before. |
| 45 | + let chain = process.spawn(fn() { |
| 46 | + supervisor.supervise(RestForOne, [ |
| 47 | + Child("first", fn() { idle("first") }), |
| 48 | + Child("second", fn() { |
| 49 | + io.println("second started") |
| 50 | + panic("second failed") |
| 51 | + }), |
| 52 | + Child("third", fn() { idle("third") }), |
| 53 | + ], 1) |
| 54 | + }) |
| 55 | + let Down(_, _) = process.receive_forever(process.monitor(chain)) |
| 56 | + io.println("chain supervisor gave up") |
| 57 | +} |
| 58 | + |
| 59 | +fn idle(name: String) -> () / <proc, io> { |
| 60 | + io.println(name <> " started") |
| 61 | + let never: process.Subject<Int> = process.subject() |
| 62 | + process.receive_forever(never) |
| 63 | + () |
| 43 | 64 | } |
| 44 | 65 | //> worker started |
| 45 | 66 | //> worker started |
| @@ -52,3 +73,9 @@ pub fn main() -> () / <io, proc> { |
| 52 | 73 | //> a started |
| 53 | 74 | //> b started |
| 54 | 75 | //> pair supervisor gave up |
| 76 | +//> first started |
| 77 | +//> second started |
| 78 | +//> third started |
| 79 | +//> second started |
| 80 | +//> third started |
| 81 | +//> chain supervisor gave up |