SHADE is young: 0.1.0-next.2 is a pre-release, and 0.1.0 isn't out yet.

SHADE
Developers

The worker container role

One image runs in four container roles, chosen by SHADE_API_ROLE: admin, public, worker and all. The worker container role runs the background work: core's schedules and the modules', and the modules' processes (Processes), and the modules' health checks every 5 minutes, as one of core's schedules (packages.health). The scheduler keeps how each schedule's last run ended in Redis (onRun), for the modules' health. It also runs queued jobs (form deliveries, email retries, planned notifications, trusted modules' jobs) on the QueueConsumer (kernel/queue/consumer.ts, Redis). "Worker" alone always means this container role; a Node worker thread is always called a worker thread.

  • No fallback. The admin and public container roles never run a schedule or a process. An instance split by container role runs its schedules and processes on its workers only, and the deployment template (deploy/compose.yml) always has one.
  • all is for development and the small profile (deploy/compose.small.yml). It serves everything and runs the background work in the same process, so it needs no worker.
  • A worker serves only /health and /health/live. /health is readiness (decision 0004 point 13): 200 only when Redis and Postgres answer and its image's migrations are applied, else 503: { "ok": true, "role": "worker", "checks": { "redis": "up", "database": "up", … } }. It needs no address.
  • Several workers may run. Each due time of a schedule runs on one of them, holding a Redis lease; so does each singleton process, while a per-instance process runs on every worker.

Where it is

PartWhat
apps/api/src/config.tsSHADE_API_ROLE; runsBackground (worker and all), healthOnly (worker); REDIS_PREFIX.
apps/api/src/server.tsStarts up as every container role does, then startRoleWork(runtime), and stops it before closing on SIGTERM.
apps/api/src/bootstrap/worker.tsstartRoleWork: startBackground on worker and all, watchForWorker on admin, nothing on public. startBackground writes the heartbeat, runs backgroundSchedules(runtime) on a Scheduler and starts the processes (startProcesses, bootstrap/processes.ts) with the thread entry server.ts passes.
shade/kernel/processes/supervisor.ts, apps/api/src/threads/thread-factory.ts, apps/api/src/threads/process-thread.tsThe modules' processes: ProcessSupervisor, the worker threads and their entry. See Processes.
apps/api/src/bootstrap/app.ts, routes/web.tsThe worker's app: healthRoute only.
shade/kernel/schedules/scheduler.tsScheduler (start(schedules), stop()), dueTime, dueMarkTtl, SCHEDULE_LEASE_TTL_MS, CRON_RECHECK_MS.
shade/kernel/schedules/cron.tsparseCron(expr) (five fields, plain refusals), nextRun(cron, timeZone, after) through croner 11.0.0 with the clock-change rules, timeZoneProblem.
shade/features/errors/server/background.tsThe heartbeat: writeWorkerHeartbeat, backgroundWorkStatus (the admin's question), reportMissingWorker (the error log).
apps/admin/app/components/BackgroundWorkAlert.vueThe admin's error on every screen (mounted in layouts/default.vue).

Schedules

Core's schedules (retention, kv's and files' purges, notifications, email retries, left-behind media files, the status rows of workers gone for a day, the security alerts; each registered by its feature with registerCoreSchedule, kernel/schedules/registry.ts, and built for the worker by coreSchedules) and the modules' (schedules: [{ name, intervalMs | cron, timeZone?, run(ctx, signal) }]) run on the kernel's Scheduler. It keeps what startSchedules did before and adds what several workers need:

  • Due times. A schedule is due at the multiples of its interval since the epoch (dueTime), the same moment on every worker: an hourly schedule at the top of each hour. Before, each process counted its interval from its own start.
  • One worker per due time. Each due time takes once:schedule:<name>:<due> on Redis (Leases.once, SET NX PX), so one worker runs it and the others skip it. The mark lives twice the interval, at least a minute and at most a day.
  • A lease per run. The run then holds lease:schedule:<name> (30 seconds, renewed every 10). A lease still held by the previous run, here or on another worker, skips this due time: a run never overlaps itself.
  • A lost lease aborts. When a renewal fails (Redis lost the lease, or does not answer), the run's signal aborts. Each run is one transaction, checked against the signal before it commits, so a run that lost its lease commits nothing, even when it ignores the signal. This replaced the Postgres advisory lock.
  • Missed due times (no worker ran) are not caught up: the next one runs.
  • The module's own actor. A module's schedule runs with asModule, whatever the context factory gives; core's run as the system actor, system: <schedule>, which holds only the modules' system grants. A module that is off skips its due times, and so does a module whose optional schedule capability is off.
  • Errors go to the error log (source: 'job', with the module) and never stop the worker.
  • Stopping. stop() starts no new run and gives the runs in progress the stop deadline less five seconds (SHADE_STOP_GRACE_MS, 30 seconds by default on the worker, so 25); then their signal aborts and they roll back. Every role stops the same way (bootstrap/shutdown.ts); the template's stop_grace_period is 40 seconds.

Cron schedules

A module's schedule may give cron (five fields) and a timeZone instead of intervalMs (Modules: schedules); the loader checks both when the module loads (scheduleTimingProblems, parseCron, timeZoneProblem), and backgroundSchedules hands the Scheduler the parsed expression (timing: { cron, timeZone }).

  • Due times. nextRun(cron, timeZone, after) (kernel/schedules/cron.ts) asks croner 11.0.0 for the next time after after in the time zone, through Intl. croner only works the time out; the Scheduler decides who runs. Every worker works out the same times, so the due-time marks (once:schedule:<name>:<due>, the due time in milliseconds, kept twice the gap to the next due time, at least a minute and at most a day), the lease, the skip while running and the abort are the interval schedules'.
  • The time zone. The schedule's own, else the site's (timeZone in the site settings, Runtime.siteTimeZone(), kept in memory and read again on a site message, which a change of the setting sends, and every 60 seconds).
  • The timer. A cron schedule's timer wakes at its due time or after a minute (CRON_RECHECK_MS), whichever comes first, and works the next due time out again from where it last looked. So a change of the site's time zone applies from the next due time on (a due time it moved into the past is not run), and a yearly schedule never overflows a Node timer. Several due times missed at once (a worker that was stopped) run once, the latest.
  • The clock changes. In the autumn a time that occurs twice runs once, the first time. croner does this itself for an expression with one hour and one minute, but runs others (*/30 2 * * *) on both occurrences, so nextRun skips the second time around, and keeps the first when the search starts inside the repeated hour. In the spring a time that doesn't exist runs at the first minute after the jump. croner does this for one hour and one minute (02:30 runs at 03:00), but skips others (10,40 * * * * from 01:40 to 03:10), so nextRun looks for a matching minute inside each jump between after and croner's answer and runs at the jump instead. Several matching minutes in one jump are one run.

Processes

The worker runs the trusted modules' processes on the kernel's ProcessSupervisor, each in a worker thread of its own with a heap limit and a heartbeat, restarted with backoff, a singleton on a Redis lease. It follows the plugins channel: a module switched on or off, an optional process capability, an admin's Restart. On a graceful stop it stops every process within its stop timeout, inside the 40-second stop grace. SHADE_PROCESSES_MEMORY_MB (192 by default) is what its processes' heaps may take together. See Processes.

The heartbeat and the admin's error

Each worker writes its heartbeat at start and every 10 seconds: the cache key worker:heartbeat (on Redis shade:cache:worker:heartbeat), holding which worker wrote it and when, for a day. Every worker writes the same key, so one worker of several is enough.

  • GET /api/health/worker (any signed-in team member, backgroundWorkStatus) answers { running }: false when no heartbeat came in for two minutes. Admins (errors.read) also get lastSeenAt and worker. On all it is always { running: true }: that process runs the work itself.
  • The admin shell asks every minute and shows Background work is not running: no worker container answers in the corner of every screen while running is false, in English and Dutch (app.backgroundWork.*).
  • Each admin container checks every minute, from two minutes after its start (watchForWorker). While no heartbeat came in for two minutes it records the same message in the error log (NoWorker, source api, scope background work), once an hour across the admin containers (Leases.once on worker-missing:<hour>).

What stays in every container

  • The sandbox. Untrusted modules run in every container role's sandbox, the worker's included, with the 5-second re-read of their switch (Messages replacing polling). A schedule can cause events (the editorial module's scheduled publish writes the publish events), and untrusted modules hear them on the worker as they do any other. Their own schedules (decision 0011 point 20) run on the worker's scheduler beside the trusted modules', each run a call into the module's sandbox (POST /__shade/schedules/<name>): each reload of the untrusted modules (followUntrustedSchedules, apps/api/src/bootstrap/worker.ts) takes the schedules of a module switched off or removed off the scheduler, calling off a run under way, before its sandbox stops, and adds those of a module switched on once its sandbox runs (Untrusted modules).
  • Trusted modules' thread tasks, in whichever process runs one: the website module's accessibility check in the admin for a team member's publish, in the worker for a scheduled publish of the editorial module. Their thread entry is apps/api/src/threads/task-thread.ts (built as dist/task-thread.mjs), started by threads/task-threads.ts (Thread tasks).
  • Following other containers. Every container follows the module switch, roles, the sandbox, the Site URL and restart requests; a worker restarts when an admin asks for it after installing a server package, and loads the new modules' schedules and processes. The events a process's changes write are delivered from the outbox like any other (decision 0014), to every module that hears them, untrusted ones included.
  • Migrations run on admin (and all) only; the template starts the worker once the admin is healthy.

Deploying it

deploy/compose.yml has the worker service: the same image and settings as admin, SHADE_API_ROLE=worker, no port, MIGRATE_ON_START=false, the admin's data volume (the media it cleans up), its server packages unpacked into a tmpfs of its own (MODULE_PACKAGES_DIR=/app/packages/module-packages, a folder inside the tmpfs, which start-up empties and fills again; so it never empties the admin's), stop_grace_period: 40s, and the same hardening as every API container. Setting up an instance and Self-hosting by hand set it up by hand; shade install does it for you (Self-hosting without Coolify).

REDIS_PREFIX (default shade:) starts every Redis key and channel. An instance has a Redis of its own and keeps the default; the browser tests' servers share one Redis and set one each, so one server's schedules never take another's due times.

Tests

  • shade/test/kernel/schedules/scheduler.test.ts (memory ports, test clock): "runs a schedule once per due time across workers", "skips a tick while the previous run goes on", "skips schedules of modules that are off", "aborts a run whose lease is lost, and rolls its transaction back", errors reported, the module's own actor, stopping; and cron schedules: "runs a cron schedule once per due time across workers, in its time zone", "runs once on the night the clocks go back, and at the jump on the night they go forward", "skips a cron due time while the previous run goes on, and aborts a run whose lease is lost", "follows the instance's time zone for a cron schedule that names none, from the next due time on".
  • shade/test/kernel/schedules/cron.test.ts: "finds the next run in Europe/Amsterdam across both clock changes, once each", "refuses six fields, seconds and unknown names".
  • shade/test/kernel/modules/loader.test.ts: "loads a cron schedule with its time zone, and refuses one it cannot read when the module loads"; an untrusted module's schedules, by interval or cron, load with the schedule capability, at most 5.
  • apps/api/test/flows/sandbox-schedules.test.ts: an untrusted module's schedules end to end, in workerd.
  • shade/test/kernel/modules/capabilities.test.ts: "writes each schedule's line from its declaration: its interval, or its cron expression and time zone, and asks again when one changes".
  • apps/api/test/bootstrap/runtime.messages.test.ts: "gives another container a new time zone at once, the one cron schedules without their own run in".
  • shade/kernel/coordination/contract.ts (memory and Redis): "marks a due time once across holders, until it expires".
  • apps/api/test/sweep/access.test.ts: "the worker container role serves only /health", "runs no schedules or queued jobs on the admin container role".
  • apps/api/test/bootstrap/worker.test.ts: "shows the admin a clear error when no worker heartbeat has come in for two minutes", "never shows the error on the all container role, which runs the background work itself", one worker of several is enough, and the error log once an hour (db project).
  • apps/api/test/bootstrap/capture.test.ts: a schedule that throws is recorded as job, with its module.
  • e2e/tests/health.spec.ts: no error on all; the error on every screen, in English and Dutch, for the answer an admin gets without a worker.
  • test/scripts/deploy-template.test.ts: the template always has a worker, with the same image, database and Redis, no port and the stop grace; pnpm deploy:smoke starts the template without its worker, checks the admin's error, then starts the worker and checks its hardening, its /health, that it serves nothing else, that it starts its process supervisor and that the processes' thread entry loads in a thread within 64 MB, and that its heartbeat ends the error.
  • Processes: see Processes: tests.