package main // Exhibit 22. The `selfcheck` job runs every five minutes (ratcloud.conf) and re-runs every // self-running exhibit's check, storing each result in zoo.db. The lights on this page are that stored // data, never live requests. It also reads root's own hourly check of the back of house, which root // publishes into the fact platform.json. import "core:fmt" import "core:strings" import "ratcloud:rc" Boh_Line :: struct { line, status, detail: string, } back_of_house :: proc() -> (at: i64, lines: []Boh_Line, ok: bool) { c := jget(fact("platform.json"), "check") if c == nil do return 0, nil, false out := make([dynamic]Boh_Line) // Lines marked "(exhibit N)" are root's view of an exhibit that has its own card. for l in jarr(c, "checks") { line := jstr(l, "line") if strings.has_prefix(line, "(exhibit") do continue append(&out, Boh_Line{line, jstr(l, "status"), jstr(l, "detail")}) } return parse_time(jstr(c, "at")), out[:], true } e22 :: proc() -> Exhibit { return { n = 22, wing = "Operations", title = "The zoo checks itself", promise = "Every self-running exhibit is rechecked on a schedule, and the front page says when.", block = "The zoo is an ordinary app: a five-minute job re-runs its exhibits and stores the results, and root's hourly check of the platform arrives in the fact platform.json.", src = {"e22_checks_itself.odin", #load("e22_checks_itself.odin", string)}, files = srcs(SELF_SRC), show = proc(me: ^rc.User) -> string { b := strings.builder_make() ms := meta_int("checked_ms") rc.render_to(&b, "

Checked {{1}}{{2}}.

", {{"1", rc.raw(ago(meta_int("checked_at")))}, {"2", fmt.tprintf(" (took %.1f s)", f64(ms) / 1000) if ms > 0 else ""}}) at, lines, ok := back_of_house() if !ok { strings.write_string(&b, `

Root's check of the back of house is not published to apps yet (the fact platform.json is missing).

`) return strings.to_string(b) } rc.render_to(&b, `

The back of house, checked by root {{1}}:

") return strings.to_string(b) }, // Runs last in the selfcheck job, after every other exhibit. check = proc() -> Check { red := make([dynamic]string) for &e in exhibits() do if e.n != 22 && e.kind == "self" && light(&e).color == "red" do append(&red, itoa(i64(e.n))) at, lines, ok := back_of_house() if !ok do return fail("root's back-of-house check is not readable: the fact platform.json is missing") // The same age root's tick and the alerts call stale (ROOT_CHECK_MAX_HOURS, zoo.odin, says why). if now() - at > ROOT_CHECK_MAX_HOURS * 3600 do return fail(fmt.tprintf("root's back-of-house check is %.1f hours old (root checks hourly)", f64(now() - at) / 3600)) bad := make([dynamic]string) for l in lines do if l.status != "pass" do append(&bad, l.line) if len(bad) > 0 do return fail(cat("back of house: ", strings.join(bad[:], "; "))) if len(red) > 0 do return fail(cat("red: exhibit", "s " if len(red) > 1 else " ", strings.join(red[:], ", "))) return pass(fmt.tprintf("every self-running exhibit is green, and root's %d back-of-house lines pass", len(lines))) }, } }