interplane.Get Omega
Read in your language

spark-supervisor

Process supervisor and health monitor for sovereign daemons

Brought by drakestapleton

branch main Branches Tags History

spark-supervisor / om / watch.om

284 lines · 4649 bytes Raw Blame History
1note aien-watch DIR SUFFIX PERIOD TIMEOUT PROG [ARGS...]: path trigger for aien-supervise (replaces
2note systemd .path units). Runs inside the service's sandbox as the service user. Every PERIOD
3note seconds it lists DIR; when DIR holds a non-hidden entry ending in SUFFIX ("*" = any name) it
4note runs PROG once with this process's environment (through /usr/bin/env -i, since exec in Omega
5note passes none) and waits for it; TIMEOUT seconds (0 = none) later a run still going gets
6note SIGTERM, 5 s after that SIGKILL (TimeoutStartSec). It runs again when the set of matching
7note names changes, after a failure (backoff PERIOD*2^k up to 300 s), and at least hourly while
8note matches remain. An unreadable DIR ends it with status 1, so the supervisor restarts it.
9text tenv "/usr/bin/env"
10text tdi "-i"
11text tstar "*"
12text wpre "aien-watch: "
13text wusage "usage: aien-watch DIR SUFFIX PERIOD TIMEOUT PROG [ARGS...]"
14text wbadp "bad PERIOD or TIMEOUT"
15text wlist "cannot list "
16text wrun "ran for "
17text wmat " matching entries, status "
18text wtoo "arguments and environment do not fit one exec packet"
19text wtmo "run timed out, sending SIGTERM\n"
20global sfx uchar 256
21global wv ulong 16
22global st4 uchar 4
23const WSFX ulong 0
24const WSIG ulong 1
25const WRC ulong 2
26const WLAST ulong 3
27const WFAIL ulong 4
28const WPER ulong 5
29const WTMO ulong 6
30
31fn wmsg t view uchar * to void
32ostr wpre
33ostr t
34end
35
36fn match k ulong to bool
37if eq dnch k 0 46
38ret false
39end
40n = get wv WSFX
41if and eq n 1 eq get sfx 0 42
42ret true
43end
44m = get dnl k
45if lt m n
46ret false
47end
48o = sub add get dno k m n
49for i ulong n
50if ne get dnb add o i get sfx i
51ret false
52end
53end
54ret true
55end
56
57note Order-independent signature of the matching names: count in the high bits plus the sum
58note of each name's FNV-1a hash.
59fn nhash k ulong to ulong
60h = ulong 14695981039346656037
61for i ulong get dnl k
62h = xor h ulong dnch k i
63h = mul h 1099511628211
64end
65ret h
66end
67
68fn signat to ulong
69s = ulong 0
70c = ulong 0
71for k ulong get gu GDC
72if match k
73s = add s nhash k
74c = add c 1
75end
76end
77put gu GSV c
78ret add s shl c 48
79end
80
81fn dirpth to void
82pset
83n = arg 1
84pav n
85end
86
87note Packet: /usr/bin/env -i <each environment entry> PROG ARGS... (false when it does not fit).
88fn buildpk to bool
89pkreset
90pkstr tenv
91pkstr tdi
92c = ulong envcnt
93for j ulong c
94r = envread j av 0 len av
95if lt r 0
96ret false
97end
98if not pkroom ulong r
99ret false
100end
101pkav ulong r
102end
103a = ulong argcnt
104for i ulong sub a 5
105n = arg add i 5
106if not pkroom n
107ret false
108end
109pkav n
110end
111ret true
112end
113
114fn decode to long
115v = or ulong get st4 0 shl ulong get st4 1 8
116if ne and v 127 0
117ret long add 128 and v 127
118end
119ret long shr v 8
120end
121
122fn runone to long
123pid = fork
124if lt pid 0
125ret -1
126end
127if eq pid 0
128r = execprog pk 0 get gu GPK
129exit 127
130end
131t0 = monons
132tmo = mul long get wv WTMO 1000000000
133sent = ulong 0
134for outer ulong 1000000
135for inner ulong 1000000
136r = waitpid int pid st4 0 4 1
137if eq r pid
138ret decode
139end
140if lt r 0
141ret -1
142end
143el = sub monons t0
144if and gt tmo 0 and eq sent 0 ge el tmo
145wmsg wtmo
146oflush 2
147k = killpid int pid 15
148sent = 1
149end
150if and eq sent 1 ge el add tmo 5000000000
151k = killpid int pid 9
152sent = 2
153end
154s = sleepns 100000000
155end
156end
157ret -1
158end
159
160fn report c ulong rc long to void
161wmsg wrun
162onum long c
163ostr wmat
164onum rc
165ostr tnl
166oflush 2
167end
168
169fn pause1 to void
170p = get wv WPER
171f = get wv WFAIL
172if gt f 0
173s = shl p f
174if gt s 300
175s = 300
176end
177p = s
178end
179r = sleepns mul long p 1000000000
180end
181
182fn numarg i ulong lo long hi long to long
183n = arg i
184if eq n 0
185ret -1
186end
187p = long 0
188for j ulong n
189c = get av j
190if or or lt c 48 gt c 57 gt p 100000000
191ret -1
192end
193p = add mul p 10 long sub c 48
194end
195if or lt p lo gt p hi
196ret -1
197end
198ret p
199end
200
201fn main to int
202if lt argcnt 6
203wmsg wusage
204ostr tnl
205oflush 2
206ret 2
207end
208n = arg 2
209if or eq n 0 gt n 256
210wmsg wusage
211ostr tnl
212oflush 2
213ret 2
214end
215for i ulong n
216put sfx i get av i
217end
218put wv WSFX n
219p = numarg 3 1 86400
220t = numarg 4 0 86400
221if or lt p 0 lt t 0
222wmsg wbadp
223ostr tnl
224oflush 2
225ret 2
226end
227put wv WPER ulong p
228put wv WTMO ulong t
229if not buildpk
230wmsg wtoo
231ostr tnl
232oflush 2
233ret 2
234end
235put wv WSIG 0
236put wv WRC 0
237put wv WLAST 0
238put wv WFAIL 0
239for outer ulong 1000000
240for inner ulong 1000000
241r = wstep
242if ne r 0
243ret int r
244end
245end
246end
247ret 0
248end
249
250fn wstep to long
251dirpth
252r = lsdir
253if lt r 0
254wmsg wlist
255dirpth
256opth
257ostr tnl
258oflush 2
259ret 1
260end
261s = signat
262c = get gu GSV
263now = ulong div monons 1000000000
264due = or ne s get wv WSIG ne get wv WRC 0
265if ge sub now get wv WLAST 3600
266due = true
267end
268if and gt c 0 due
269rc = runone
270report c rc
271put wv WSIG s
272put wv WRC ulong rc
273put wv WLAST now
274if eq rc 0
275put wv WFAIL 0
276else
277if lt get wv WFAIL 9
278put wv WFAIL add get wv WFAIL 1
279end
280end
281end
282pause1
283ret 0
284end