Smeltery
Get started

Watchfire

v0.1

Dashboard and API

JSON API. Under /_watchfire/api (no sessions, no CSRF check):

Request Answer
GET /agents every agent's status
GET /agents/{name} {"status": …, "runs": […]} (the latest 20 runs)
POST /agents/{name}/{start\|stop\|pause\|resume\|restart} the new status
GET /agents/{name}/logs the last log lines (at most 200), also from the process that runs the agent
GET /runs?agent=&limit= the latest runs (default 50, at most 500)
GET /jobs {"driver", "pending", "reserved", "dead", "dead_letters": […]} (the latest 50)
POST /jobs/dead/{id}/retry queues a dead letter again: {"job_id": …}
DELETE /jobs/dead/{id} deletes a dead letter
GET /schedule the scheduled tasks with their next run
GET /events Server-Sent Events: snapshot (every agent) first, then status per change and event per emit; the stream ends when the app shuts down

Errors are {"error": "…"} with 401 (no or a wrong token), 404 (unknown agent, action or dead letter), 409 (the agent is in the wrong state, e.g. stopping a stopped agent; or the process holding it refused the command), 502 (the log lines the holding process sent could not be read), 503 (Watchfire is not running in this process and no other process can be reached, it is shutting down, or the commands table failed) or 500. A command for an agent in another process answers with the holder's own status for a refusal (404, 409, 503), 202 {"message"} when the holder took it but has not finished within 5 s, 504 when no process took it within 5 s, and 429 when 20 commands wait for the agent already. Every request with Authorization: Bearer <token> is accepted, where the token is the hex HMAC-SHA256 of "watchfire-api" keyed with APP_KEY (smeltery::watchfire::web::api_token(key)). Without the token only local development gets in: WATCHFIRE_DASHBOARD=local, APP_ENV=local, an APP_URL on this machine (localhost, *.localhost or a loopback address), and a request from a loopback address that did not come through a proxy in TRUSTED_PROXIES and carries no reverse-proxy or CDN header (Forwarded, X-Forwarded-For, -Host, -Proto, -Port, -Server, X-Real-IP, X-Original-Forwarded-For, X-Client-IP, Via, CF-Connecting-IP, True-Client-IP, Fastly-Client-IP), that names this machine as its Host (localhost, *.localhost, a loopback address or the APP_URL host, so a page on another domain that resolves to 127.0.0.1 gets nothing) and that no other site sent (an Origin other than the app's own, host and port, so another dev server on localhost:3000 is another site; more than one Origin; or a Sec-Fetch-Site other than same-origin or none, is refused; a link followed to the dashboard page opens it). Pool members are addressed with # encoded (fetcher%230). serve answers the API on the app's own port; work answers it only when WATCHFIRE_API_ADDR is set, with HTTP/1.1, at most 64 connections at once, 10 s for a request's headers (also the idle limit between requests) and X-Content-Type-Options: nosniff; bind it to 127.0.0.1.

Dashboard. GET /_watchfire is a page (a Mold template compiled into Watchfire, the same in debug and release builds) with summary tiles (agents running, paused, failed or backing off, in standby; queue depth, dead letters and the next scheduled run), every agent's state, heartbeat (on time or stalled: whether the last heartbeat arrived within the agent's timeout), last heartbeat, restarts, runs (job runs of a queue worker included), the process that runs it, pending backoff and next restart, last error and recent runs with their counters (run ids count per process, so runs of an earlier process are marked as such), the queue counts and dead letters, and the schedule. Each agent has the buttons its state allows (start; pause or resume; restart and stop): forms with @csrf posting to /_watchfire/agents/{name}/{action}, which redirect back with a flash message. Stop and restart ask first: they link to /_watchfire?agent=<name>&confirm=<action>, the page with that confirmation open (its form and a Cancel link, which has the focus), rendered without the live panels and without the 5 s reload. The page is sent with Cache-Control: no-store, X-Frame-Options: DENY and Content-Security-Policy: frame-ancestors 'none', so no other site can frame it. The page brings its own stylesheet, embedded in Watchfire and served at /_watchfire/assets/watchfire.css?v=<version> (to anyone, as it holds no data; 404 under off; cached for a year, and the version changes with the file), so it needs nothing from the app: no app.css, no Tailwind, no web fonts, no JavaScript besides Sparks. It uses the forge colours of new apps, follows the system's light or dark mode, and shows each agent as a card on narrow screens. The dashboard is for signed-in users that the app's gate admits, in app/agents/mod.rs:

rust
use smeltery::watchfire::prelude::*;

pub fn register(w: &mut Watchfire) {
    // `auth` is the visitor's sign-in (`auth.id()`, `auth.user::<User>().await?`), `app` the app.
    w.dashboard_gate(|auth, _app| async move { Ok(auth.id() == Some(1)) });
}

Without a gate no signed-in user gets in. Guests are sent to the login route (JSON clients get 401), a signed-in user the gate refuses gets 403, and an error from the gate is a 500. With WATCHFIRE_DASHBOARD=local (default), APP_ENV=local and a local APP_URL, the requests the API's local rule admits (a loopback address, no reverse-proxy header, a Host on this machine and no other site's Origin; see the API) get in without signing in; auth asks for the gate under APP_ENV=local too; off hides the dashboard (404). The page, its forms and its live panels follow the same rule. The dashboard is a web route, so it is mounted only when the app has a usable APP_KEY.

When the app installs Sparks too (.agents(…) and .sparks(…), in either order), the dashboard is live: its panels are two Spark components registered by Watchfire, watchfire.agents (the agents table) and watchfire.queue (queue, dead letters, schedule), both streamed (#[spark(stream)]). Watchfire pushes a refresh of the agents panel after status changes, at most one every 500 ms however busy the supervisor is, and of the queue panel every 5 seconds, only while a dashboard is open. The buttons call the panel's act(name, action) action (with JavaScript, stop and restart first call ask(name, action) instead of following their link, which opens the confirmation in the panel's state, so a pushed refresh keeps it open; cancel closes it) through the Sparks update endpoint, which applies the same rule to actions and refreshes (401 for a guest, 403 for a signed-in user the gate refuses, 404 when off); its update requests show no request headers, so local development there also needs the session of a browser the page let in from a loopback address (any dashboard request the local rule does not admit takes that back). In a process that does not run an agent, the panels check the shared tables every 2 seconds while a dashboard is open and refresh when they changed. The page is rendered on the server first, so it reads without JavaScript; then the forms post as before (stop and restart lead to the confirmation page) and a <noscript> meta tag reloads the page every 5 seconds. Without Sparks the page reloads itself every 5 seconds (<meta http-equiv="refresh">).

Console. agents:list, agents:start|stop|pause|resume|restart <name> and agents:logs <name> call the running app's API (at WATCHFIRE_API_ADDR, else SERVER_HOST:SERVER_PORT) with the token, with a 10 s timeout, following no redirect; when nothing answers they say so and how to start the app. They refuse to send the token over plain http:// to an address that is not this machine (set a loopback WATCHFIRE_API_ADDR or an https:// URL). They print control characters from the app (terminal escape sequences in log lines) as �. agents:token prints the token. The token is derived from APP_KEY: a new APP_KEY is the way to revoke it (it also signs every user out and invalidates signed Spark state).

Alerts. An agent becoming failed (its run failed under Restart::Never, or it reached its restart limit), a stalled run, and a job moved to the dead letters each raise an alert: an error-level log line (target smeltery_watchfire::alert), every w.on_alert(|alert| async move { … }) hook (one at a time, at most 10 s each, in a task of their own, so supervision never waits), and a POST of the alert as JSON to WATCHFIRE_ALERT_WEBHOOK (10 s timeout, retried up to 3 times; a failed delivery is logged with the webhook's host only), and a mail to the addresses in WATCHFIRE_ALERT_MAIL (subject [APP_NAME] Watchfire alert: <kind> <agent>) when the app has .mail(). An Alert has kind (failed, stalled, dead_letter), app, agent, job, message and at_ms.

↑↓ moveEnter openEsc close