Smeltery
Get started

Watchfire

v0.1

Several processes

Several processes. serve runs the web server and the background work together. serve --no-agents (or WATCHFIRE_IN_SERVE=false) serves the web only, so a deployment can run web processes next to one or more work processes. When the processes share a cache store whose locks span processes (database, redis, memcached, and file for the processes of one machine; WATCHFIRE_LOCK_STORE, by default CACHE_STORE), they coordinate through its atomic locks:

  • Agents run in one process at a time. Each agent holds a lease (a lock named watchfire:agent:<name>). The other processes show it as standby and try to take the lease every WATCHFIRE_LEASE_TTL / 3. A process that stops releases its leases, so another one takes over within that interval; a process that dies leaves them to expire, so another one takes over between WATCHFIRE_LEASE_TTL and 4/3 of it after its last renewal. The holder renews a lease every third of its cut-off, TTL - shutdown timeout - TTL / 6 after the last renewal it sent. At the cut-off, whatever store call is still in flight (a renewal, or a status write of the running agent), or as soon as a renewal finds the lease taken, it stops the agent (within the agent's shutdown timeout) and goes to standby; a run whose lease went while its start was being recorded does not start. So it has stopped by TTL - TTL / 6 after the store's last renewal: processes whose clocks differ by less than WATCHFIRE_LEASE_TTL / 6 (5 s by default) never run an agent twice at once. A singleton whose shutdown timeout leaves a cut-off under a quarter of the TTL is refused at launch with the TTL it needs (at least 12/7 of the shutdown timeout: the default 10 s timeout needs at least 18); so is a coordinated scheduled call with Overlap::Skip / Queue (stopped within 10 s) under 18, by serve, work and schedule:run alike. The holder keeps the agent in every state: paused, stopped or completed there means the same everywhere while it runs; the next holder starts it as autostart says. remove frees the lease, so another process takes the agent over: remove it in every process, or from the registration. Pool members are agents of their own (fetcher#0 may run in one process and fetcher#1 in another). .per_process() (on the builder or AgentConfig) runs an agent in every process instead.
  • Each scheduled tick runs once. A job or call that is due claims its tick in the store (Cache::add of watchfire:schedule:<name>:<tick>); the first process runs it and the others skip it. A call with Overlap::Skip or Overlap::Queue also holds a lease while it runs, so it never runs in two processes at once: a run in another process makes it skip (Queue waits only for a run in its own process), and when its lease is lost the call is cancelled and dropped after 10 s. schedule:run claims the minute's tick and takes the same leases, so system cron on several servers, or next to serve, runs a task once; it skips every(d) tasks whose d is not a whole number of minutes (only serve / work run those then). .agent(name) targets start the agent in the process that holds it. .per_process() on a scheduled task runs it in every process.
  • Queue workers run in every process. The database queue reserves each job for one worker (above).
  • Live pages update from every process. serve --no-agents and work use the shared PubSub driver under PUBSUB_DRIVER=auto, so an agent's broadcast.to("counter").refresh() in work reaches the pages held by the web process.
  • Run history keeps processes apart. Each run records its process (RunRecord::process: host, process id and a random part). Every process holds a lease on its own name; runs left running by a process whose lease is gone are marked interrupted (checked at launch and every WATCHFIRE_LEASE_TTL). A process that loses its own lease (its store calls fail) takes it again as soon as the store answers and puts its running runs that were marked interrupted back to running; a run that ended meanwhile keeps its outcome. A final run record that could not be written (the database did not answer) is written again at the next check. Processes that share the database without coordinating (lock store off, or the local fallback below) hold no such lease, so a coordinating process marks their running runs interrupted.

At launch Watchfire checks the store by taking and freeing a lock. When it does not answer (or the cache_locks table is missing), a process under APP_ENV=local or testing runs everything itself (with a warning), and any other coordinates anyway: its agents wait in standby and its ticks are skipped until the store answers. A store named in WATCHFIRE_LOCK_STORE that cannot be opened is an error; one that does not answer the probe is an error under APP_ENV=local / testing and coordinated through (as above) elsewhere. The memory, array and null stores do not span processes: each process then runs every agent and every tick itself.

One dashboard for every process. With the Watchfire tables in the database, a process that does not run Watchfire (serve --no-agents) shows the registered agents from watchfire_agents and watchfire_runs, with the process that holds each one (or "no process holds it", with its last recorded state); a process where an agent is in standby shows the holder's row for it. A command for an agent that runs elsewhere goes into watchfire_commands. Every coordinating process looks there once a second for the agents it holds, carries the command out (each agent's commands in order, those of different agents side by side) and records the outcome (or the refusal, with its status); the dashboard (or API) waits up to 5 s for it and shows it. When the holder took the command but has not finished within 5 s (a slow stop) it says so; when no process took it, the command is carried out when a process holding the agent takes it, or lapses after 60 s. A command taken but never finished is closed after 11 minutes as "outcome never recorded"; a process that shuts down while carrying a command out closes it at once (503, outcome unknown). Command times come from the database's clock. At most 20 commands wait per agent. Log lines stay in the memory of the process that runs an agent; from another process, GET /agents/{name}/logs (and agents:logs) asks that process for them through watchfire_commands (the newest lines that fit in 60 KB; 504 when it does not answer within 5 s). The answer's row is deleted once read, or a minute after it was written when nobody read it. The /events stream stays with the process that runs an agent.

↑↓ moveEnter openEsc close