supervise
Restart policies over monitored workers: run a set of named children (each an arity-0 callable spawned as a worker) under a declared policy — strategy (:one-for-one, :one-for-all, :rest-for-one), restart intensity (max-restarts within a window), and per-child exponential backoff — restarting them when they die according to each child's restart type (:permanent, :transient, :temporary). Provides start, stop, dynamic start-child/stop-child, status snapshots, and an observable event stream of the supervisor's own acts. The supervision loop runs in an ordinary worker, so supervisors nest by composition: a give-up terminates the loop with CXER5094 and a parent supervisor monitoring it escalates by policy. Composes workers, monitors, fan-out channels, and sched timers; adds no new concurrency primitive and no capability.
supervise:start
[$supervise:start] -> element — Validate a policy and child specs, spawn the children and the supervision loop, and return the closeable [supervisor] handle.
[?lib 'cx-stdlib/supervise' :as sup]
[?let [= $c [?channel name="doc-sup-start" buffer=1]]
[= $s [$sup:start [policy strategy=:one-for-one max-restarts=3]
([child name="a" [fn [?fn () [?receive from=$c]]]])]]
[= $ev [$sup:events $s]]
[= $e [?receive from=$ev max=1 deadline=5000]]
[= $_ [$sup:stop $s]]
$e]
([child-started name=a attempt=0])
supervise:stop
[$supervise:stop] -> null — Gracefully stop the supervisor: cancel children in reverse start order, each bounded by its shutdown, then stop the loop; idempotent.
[?lib 'cx-stdlib/supervise' :as sup]
[?let [= $c [?channel name="doc-sup-stop" buffer=1]]
[= $s [$sup:start [policy strategy=:one-for-one max-restarts=3]
([child name="a" [fn [?fn () [?receive from=$c]]]])]]
[= $r1 [$sup:stop $s]]
[= $r2 [$sup:stop $s]]
($r1, $r2)]
(null, null)
supervise:start-child
[$supervise:start-child] -> element — Add and start a child under the running policy; it joins at the end of start order. Duplicate names refuse CXER5091.
[?lib 'cx-stdlib/supervise' :as sup]
[?let [= $c [?channel name="doc-sup-sc" buffer=1]]
[= $c2 [?channel name="doc-sup-sc2" buffer=1]]
[= $s [$sup:start [policy strategy=:one-for-one max-restarts=3]
([child name="a" [fn [?fn () [?receive from=$c]]]])]]
[= $kid [child name="b" [fn [?fn () [?receive from=$c2]]]]]
[= $r [$sup:start-child $s $kid]]
[= $st [$sup:status $s]]
[= $_ [$sup:stop $s]]
($r, [$count $st/*])]
([ok], 2)
supervise:stop-child
[$supervise:stop-child] -> bool — Cancel (bounded by its shutdown) and REMOVE a child by name — true if it was present, false if unknown (a value; idempotent removal is normal).
[?lib 'cx-stdlib/supervise' :as sup]
[?let [= $c [?channel name="doc-sup-rc" buffer=1]]
[= $s [$sup:start [policy strategy=:one-for-one max-restarts=3]
([child name="a" [fn [?fn () [?receive from=$c]]]])]]
[= $r1 [$sup:stop-child $s "a"]]
[= $r2 [$sup:stop-child $s "a"]]
[= $_ [$sup:stop $s]]
($r1, $r2)]
(true, false)
supervise:status
[$supervise:status] -> element — Read a [supervisor-status] snapshot — restarts-in-window plus each child's name, state (running|restarting|abandoned|stopped), and attempt count; stays readable after stop.
[?lib 'cx-stdlib/supervise' :as sup]
[?let [= $c [?channel name="doc-sup-st" buffer=1]]
[= $s [$sup:start [policy strategy=:one-for-one max-restarts=3]
([child name="a" [fn [?fn () [?receive from=$c]]]])]]
[= $st [$sup:status $s]]
[= $_ [$sup:stop $s]]
$st]
[supervisor-status restarts-in-window=0 [child name=a state=running attempts=0]]
supervise:events
[$supervise:events] -> element — Subscribe to the supervisor's own acts — child-started/child-exited/child-restarted/child-abandoned/gave-up — as a standard fan-out subscription replayed from the retained floor.
[?lib 'cx-stdlib/supervise' :as sup]
[?let [= $c [?channel name="doc-sup-ev" buffer=4]]
[= $s [$sup:start [policy strategy=:one-for-one max-restarts=3]
([child name="a" [fn [?fn () [?receive from=$c]]]])]]
[= $ev [$sup:events $s]]
[= $e0 [?receive from=$ev max=1 deadline=5000]]
[= $_k [?send [go] to=$c]]
[= $e1 [?receive from=$ev max=1 deadline=5000]]
[= $_ [$sup:stop $s]]
($e0, $e1)]
(([child-started name=a attempt=0]), ([child-exited name=a reason=:done]))