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 / services.om

404 lines · 6326 bytes Raw Blame History
1note Additional user-service contract. Dependency names are resolved after the initial scan.
2
3fn sxDefaults i ulong to void
4put sxDepCount i 0
5put sxPort i 0
6put sxHealthLen i 0
7put cgMem i 0
8put cgTasks i 0
9put cgCpu i 0
10put sxNice i 0
11end
12
13fn sxKey to bool
14ret or or or or or wis 0 sxAfter wis 0 sxHealth wis 0 sxMemory wis 0 sxTasks wis 0 sxCpu wis 0 knice
15end
16
17fn sxApply i ulong to bool
18if wis 0 sxAfter
19if lt nw 2
20ret false
21end
22for k ulong sub nw 1
23if not sxAddDependency i add k 1
24ret false
25end
26end
27ret true
28end
29if wis 0 sxHealth
30if ne nw 3
31ret false
32end
33port = wnum 1
34if or lt port 1 gt port 65535
35ret false
36end
37n = wlen 2
38if or eq n 0 ge n 4096
39ret false
40end
41if ne wch 2 0 47
42ret false
43end
44put sxPort i ulong port
45put sxHealthLen i n
46for j ulong n
47c = wch 2 j
48if or lt c 33 gt c 126
49ret false
50end
51put sxHealthPath add mul i 4096 j c
52end
53ret true
54end
55if ne nw 2
56ret false
57end
58v = wnum 1
59if wis 0 knice
60if or lt v 0 gt v 19
61ret false
62end
63put sxNice i v
64ret true
65end
66if le v 0
67ret false
68end
69if wis 0 sxMemory
70put cgMem i ulong v
71ret true
72end
73if wis 0 sxTasks
74put cgTasks i ulong v
75ret true
76end
77if wis 0 sxCpu
78if gt v 10000
79ret false
80end
81put cgCpu i ulong v
82ret true
83end
84ret false
85end
86
87fn sxNamesEqual slot ulong other ulong to bool
88for j ulong get snl other
89leftbyte = get snm add mul other 32 j
90rightbyte = get sxDepNames add mul slot 32 j
91if ne leftbyte rightbyte
92ret false
93end
94end
95ret true
96end
97
98fn sxDepIndex i ulong k ulong to long
99slot = add mul i 48 k
100for other ulong get sv VN
101if eq get snl other get sxDepLens slot
102if sxNamesEqual slot other
103ret long other
104end
105end
106end
107ret -1
108end
109
110fn sxCycle i ulong target ulong depth ulong to bool
111if ge depth get sv VN
112ret true
113end
114for k ulong get sxDepCount i
115other = sxDepIndex i k
116if lt other 0
117ret true
118end
119if eq ulong other target
120ret true
121end
122if sxCycle ulong other target add depth 1
123ret true
124end
125end
126ret false
127end
128
129fn sxReady i ulong to bool
130if sxCycle i i 0
131put sxReasonCode i 2
132setst i 7
133ret false
134end
135for k ulong get sxDepCount i
136other = sxDepIndex i k
137if lt other 0
138ret false
139end
140if or ne get sst ulong other 1 eq get sxHealthy ulong other 0
141ret false
142end
143end
144ret true
145end
146
147fn sxHealthSocket i ulong to long
148address = new uchar 16 0
149put address 0 2
150port = get sxPort i
151put address 2 uchar shr port 8
152put address 3 uchar and port 255
153put address 4 127
154put address 7 1
155fd = socknew 2 526337 0
156if lt fd 0
157ret fd
158end
159r = connect int fd address 0 16
160if and lt r 0 ne r -115
161c = fclose int fd
162ret r
163end
164r = waitio int fd 4 100000000
165if le r 0
166c = fclose int fd
167ret -110
168end
169ret fd
170end
171
172fn sxHealthRequest i ulong req edit uchar 8192 to ulong
173put gu GOB 0
174ostr sxHttpPrefix
175for j ulong get sxHealthLen i
176ob1 get sxHealthPath add mul i 4096 j
177end
178for j ulong len sxHttpTail
179if eq get sxHttpTail j 10
180ob1 13
181end
182ob1 get sxHttpTail j
183end
184n = get gu GOB
185for j ulong n
186put req j get ob j
187end
188put gu GOB 0
189ret n
190end
191
192fn sxHttp i ulong to bool
193response = new uchar 256 0
194req = new uchar 8192 0
195fd = sxHealthSocket i
196if lt fd 0
197ret false
198end
199n = sxHealthRequest i req
200position = ulong 0
201while lt position n bound 8192
202r = send int fd req position sub n position 0
203if le r 0
204c = fclose int fd
205ret false
206end
207position = add position ulong r
208end
209position = 0
210deadline = add monons 200000000
211while lt position 12 bound 256
212remaining = sub deadline monons
213if le remaining 0
214c = fclose int fd
215ret false
216end
217r = waitio int fd 1 remaining
218if le r 0
219c = fclose int fd
220ret false
221end
222r = recv int fd response position sub len response position 0
223if le r 0
224c = fclose int fd
225ret false
226end
227position = add position ulong r
228end
229c = fclose int fd
230if ne get response 0 72
231ret false
232end
233if ne get response 1 84
234ret false
235end
236if ne get response 2 84
237ret false
238end
239if ne get response 3 80
240ret false
241end
242ret and eq get response 8 32 eq get response 9 50
243end
244
245fn sxSetHealth i ulong value ulong to void
246if ne get sxHealthy i value
247put sxHealthy i value
248put sv VBEAT 1
249end
250end
251
252fn sxUpdateHealth i ulong to void
253if or ne get sst i 1 le get spid i 0
254sxSetHealth i 0
255ret
256end
257if ne killpid int get spid i 0 0
258sxSetHealth i 0
259ret
260end
261if gt get sxPort i 0
262if not sxHttp i
263sxSetHealth i 0
264ret
265end
266end
267sxSetHealth i 1
268end
269
270fn sxHasLiveDependent target ulong to bool
271for i ulong get sv VN
272if gt get spid i 0
273for k ulong get sxDepCount i
274if eq sxDepIndex i k long target
275ret true
276end
277end
278end
279end
280ret false
281end
282
283fn sxShutdownStep to void
284if eq get sv VEXIT 0
285ret
286end
287for k ulong get sv VN
288i = sub sub get sv VN k 1
289put sdn i 1
290if not sxHasLiveDependent i
291stopsvc i
292end
293end
294end
295
296fn sxStatus i ulong to void
297ostr sxHealthField
298onum long get sxHealthy i
299ostr sxTrigger
300if eq get sxReasonCode i 1
301ostr sxReason
302ostr sxLimit
303onum get cgWhy i
304end
305if eq get sxReasonCode i 2
306ostr sxReason
307ostr sxDependency
308end
309if gt get cgOom i 0
310ostr tsp
311ostr sxOomStatus
312end
313end
314
315fn sxExec i ulong to long
316environment = new uchar 4096 0
317n = ulong 2
318count = ulong 0
319p = ulong 0
320while lt p get gu GSF bound 999999
321p = lnext p
322if and ge nw 2 wis 0 kenv
323for k ulong sub nw 1
324wordidx = add k 1
325wlenv = wlen wordidx
326if gt add add n wlenv 2 len environment
327ret -7
328end
329put environment n uchar and wlenv 255
330put environment add n 1 uchar shr wlenv 8
331n = add n 2
332for j ulong wlenv
333put environment n wch wordidx j
334n = add n 1
335end
336count = add count 1
337end
338end
339end
340put environment 0 uchar and count 255
341put environment 1 uchar shr count 8
342if eq count 0
343ret execprog pk 0 get gu GPK
344end
345ret execenv pk 0 get gu GPK environment 0 n
346end
347
348fn sxUserPacket i ulong to bool
349pkreset
350p = ulong 0
351while lt p get gu GSF bound 999999
352p = lnext p
353if and gt nw 0 wis 0 kwatch
354note Legacy watcher is a separate seeded Omega executable, not a shell command.
355pkstr twatch
356for k ulong sub nw 1
357if not pkroom wlen add k 1
358ret false
359end
360pkword add k 1
361end
362end
363end
364p = 0
365while lt p get gu GSF bound 999999
366p = lnext p
367if and gt nw 0 wis 0 kexec
368for k ulong sub nw 1
369if not pkroom wlen add k 1
370ret false
371end
372pkword add k 1
373end
374ret true
375end
376end
377ret false
378end
379fn sxCleanGroup i ulong to void
380pid = get spid i
381if gt pid 0
382r = killpid neg int pid 9
383end
384end
385
386fn sxAddDependency i ulong widx ulong to bool
387n = wlen widx
388if not okname get ws widx get we widx
389ret false
390end
391cnt = get sxDepCount i
392if ge cnt 48
393ret false
394end
395slot = add mul i 48 cnt
396put sxDepLens slot n
397for j ulong n
398b = wch widx j
399put sxDepNames add mul slot 32 j b
400end
401put sxDepCount i add cnt 1
402ret true
403end
404