~/bend-docscommunity

daemon/dec.bend source

daemon/dec.bend on the hub · documented module

import Baseimport ../json.bend as Jsonimport ../swlive.bend as Limport ../store/base.bend as SBaseimport ../store/seq.bend as SSeqimport ../clock.bend as Clockimport ./events.bend as Evimport ./cadence.bend as Cadimport ./config.bend as Cfg# The decision the daemon acts on, what a tick answers, and the result# builders for the outcomes that need no switch. Kept apart from `apply` so# the policy side of a tick never imports the switch modules.type Dec is Data:  DNone{reason: String, detail: String}  DExhausted{earliest: Maybe<&2, U32>}  DGo{to: String, to_email: String, trigger: String, left_headroom: Maybe<&2, U32>, left_recovery: Maybe<&2, U32>}# The events of one tick, in order, and the sleeper's facts.type TickRes is Data:  TickRes{info: Cad.Info, events: List<&2, Json.Json>}# What `tick` was handed besides the decision.type Ctx is Data:  Ctx{ctx: SBase.Ctx, dry_run: Bool, cfg: Cfg.Cfg, now: U32, ts: String, opts: L.Opts, active: Json.Json, next_poll: Maybe<&2, U32>}def sel_o(+c: Bool, x: Cad.Outcome, y: Cad.Outcome) -> Cad.Outcome:  match c:    case True{}:      x    case False{}:      ydef blocked_reason(+r: String) -> Bool:  String.eq(r, "no-candidates") || String.eq(r, "no-comparison") || String.eq(r, "no-qualifying-candidate") || String.eq(r, "no-viable-target") || String.eq(r, "all-exhausted")def info(o: Cad.Outcome, +until: Maybe<&2, U32>, +long: Bool, +idle: Bool, +np: Maybe<&2, U32>) -> Cad.Info:  Cad.Info{o, until, long, idle, np}def one(i: Cad.Info, +e: Json.Json) -> TickRes:  TickRes{i, [e]}def iso_opt(+m: Maybe<&2, U32>) -> Maybe<&2, String>:  match m:    case None{}:      None{}    case Some{t}:      Some{Clock.iso8601_utc(t)}def ctx_ts(+c: Ctx) -> String:  match c:    case Ctx{x, d, cf, n, ts, o, a, np}:      tsdef ctx_np(+c: Ctx) -> Maybe<&2, U32>:  match c:    case Ctx{x, d, cf, n, ts, o, a, np}:      npdef none_res(+c: Ctx, +reason: String, +detail: String) -> TickRes:  one(info(sel_o(blocked_reason(reason), Cad.Blocked{}, Cad.NoAction{}), None{}, String.eq(reason, "no-candidates"), String.eq(reason, "active-idle"), ctx_np(c)), Ev.no_switch(ctx_ts(c), reason, detail))def exhausted_res(+c: Ctx, +earliest: Maybe<&2, U32>) -> TickRes:  one(info(Cad.Blocked{}, earliest, False{}, False{}, ctx_np(c)), Ev.all_exhausted(ctx_ts(c), iso_opt(earliest)))# A failed tick: the error as an event, exit 1.def error_res(+c: Ctx, +msg: String) -> TickRes:  one(info(Cad.Failed{}, None{}, False{}, False{}, ctx_np(c)), Ev.error(ctx_ts(c), msg, True{}))# ---- the switch itself ----------------------------------------------------------------------def ctx_now(+c: Ctx) -> U32:  match c:    case Ctx{x, d, cf, n, ts, o, a, np}:      ndef ctx_cfg(+c: Ctx) -> Cfg.Cfg:  match c:    case Ctx{x, d, cf, n, ts, o, a, np}:      cfdef ctx_store(+c: Ctx) -> SBase.Ctx:  match c:    case Ctx{x, d, cf, n, ts, o, a, np}:      xdef ctx_opts(+c: Ctx) -> L.Opts:  match c:    case Ctx{x, d, cf, n, ts, o, a, np}:      odef ctx_active(+c: Ctx) -> Json.Json:  match c:    case Ctx{x, d, cf, n, ts, o, a, np}:      adef ctx_dry(+c: Ctx) -> Bool:  match c:    case Ctx{x, d, cf, n, ts, o, a, np}:      d