tobogganSe connecter

louise

The Louise language: compiler, runtime, std, tooling

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

114 lignes · 2777 octetsBrut · Historique
1import std/io
2import std/process.{type proc, Crashed, Down}
3import std/supervisor.{Child, OneForAll, OneForOne, RestForOne}
4
5pub fn main :: -> () / <io, proc>
6{
7 // A child that always crashes is restarted twice, then the supervisor
8 // gives up and crashes itself.
9 let sup =
10 {
11 use <- process.spawn
12 supervisor.supervise
13 OneForOne
14 [ Child( "flaky"
15 , fn
16 {
17 io.println "worker started"
18 panic "worker failed"
19 }
20 )
21 ]
22 2
23 }
24 let Down(_, why) = process.receive_forever (process.monitor sup)
25 match why
26 { Crashed(m) -> io.println ("supervisor down: " <> m)
27 , _ -> io.println "supervisor stopped"
28 }
29 // A child that finishes normally is not restarted.
30 let calm =
31 {
32 use <- process.spawn
33 supervisor.supervise
34 OneForOne
35 [Child("once", fn { io.println "one-shot job" })]
36 5
37 io.println "supervisor done"
38 }
39 let Down(_, _) = process.receive_forever (process.monitor calm)
40 // One for all: a crash restarts the healthy sibling too; the sibling's
41 // own exit (it is killed) is not counted as another crash.
42 let pair =
43 {
44 use <- process.spawn
45 supervisor.supervise
46 OneForAll
47 [ Child( "a"
48 , fn
49 {
50 io.println "a started"
51 panic "a failed"
52 }
53 )
54 , Child( "b"
55 , fn
56 {
57 io.println "b started"
58 let never: process.Subject<Int> = process.subject ()
59 process.receive_forever never
60 ()
61 }
62 )
63 ]
64 1
65 }
66 let Down(_, _) = process.receive_forever (process.monitor pair)
67 io.println "pair supervisor gave up"
68 // Rest for one: a crash restarts the crashed child and the ones started
69 // after it, not the ones before.
70 let chain =
71 {
72 use <- process.spawn
73 supervisor.supervise
74 RestForOne
75 [ Child("first", fn { idle "first" })
76 , Child( "second"
77 , fn
78 {
79 io.println "second started"
80 panic "second failed"
81 }
82 )
83 , Child("third", fn { idle "third" })
84 ]
85 1
86 }
87 let Down(_, _) = process.receive_forever (process.monitor chain)
88 io.println "chain supervisor gave up"
89}
90
91fn idle :: name: String -> () / <proc, io>
92{
93 io.println (name <> " started")
94 let never: process.Subject<Int> = process.subject ()
95 process.receive_forever never
96 ()
97}
98//> worker started
99//> worker started
100//> worker started
101//> supervisor down: supervisor: too many restarts
102//> one-shot job
103//> supervisor done
104//> a started
105//> b started
106//> a started
107//> b started
108//> pair supervisor gave up
109//> first started
110//> second started
111//> third started
112//> second started
113//> third started
114//> chain supervisor gave up