Skip to content

feat(server,admin): scheduled jobs get a registry, run records and a Jobs page - #377

Merged
Aizen93 merged 8 commits into
developfrom
feat/admin-jobs
Nov 28, 2026
Merged

Aizen93 merged 8 commits into
developfrom
feat/admin-jobs

Conversation

@Aizen93

@Aizen93 Aizen93 commented Nov 27, 2026

Copy link
Copy Markdown
Owner

What was missing

The server's scheduled jobs each had their own lock and log lines and no shared record. None could be switched off without a release, and an administrator could not see what a run did.

What the code does now

  • utils/jobs.ts declares every scheduled job once: name, title, a description of what it does, its schedule, whether it can be switched, whether it has a count-only mode, and its default mode. Nine jobs: attachment clean-up, registration hygiene, the object-store orphan clean-up, report retention, the key-share sweep, the session sweep, the discovery cycle, and the two 60-second reapers (presence, dead voice nodes).
  • Every tick goes through one wrapper around the job's own lock. It reads the job's mode from the database at that tick, so a switch takes effect at every node's next tick with no restart, and runs nothing when the job is off.
  • A run is recorded in job_runs: when, on which node, the outcome (success, failed, skipped with its reason), the job's own counts, and a failure as the log describes it. A record holds numbers and a few fixed words only; a network error is stored by name and code. 200 runs are kept per job. The jobs that run every minute or every few minutes are recorded when something changes, and at most hourly when quiet.
  • A job that was switched off stays off. Only a missing settings table reads as the default mode; any other failure to read the mode skips the tick and records the skip. A job that is off cannot be run by hand either: the two existing manual triggers answer 409 while it is off, and a manual run is recorded like a scheduled one.
  • The two reapers are listed and recorded and cannot be switched: they keep presence and voice occupancy true.
  • Each job keeps its lock, schedule, guards, log lines and return values.
  • Routes: GET /admin/jobs, GET /admin/jobs/:name/runs (keyset paging), PUT /admin/jobs/:name (superadmin only, audited as job.update with the old and the new mode).
  • The dashboard has a Jobs page: each job with its description, schedule, mode, last run, next slot, marks for a failed or overdue job, the run history, and a confirmed switch for a superadmin.

Key files

  • apps/server/src/utils/jobs.ts, apps/server/src/utils/jobStore.ts
  • apps/server/src/routes/admin.ts (the scheduled-jobs routes and the two manual triggers)
  • apps/server/prisma/migrations/20261127120000_job_registry/migration.sql
  • apps/admin/src/components/AdminJobs.tsx, apps/admin/src/stores/adminStore.ts

Migrations

20261127120000_job_registry: additive. Two tables (job_settings, job_runs), one index, one key to users added under a lock timeout. It goes in before the server that reads it. A server older than it is unaffected. A server newer than it on a database without it runs every job in its default mode and records nothing.

Tests

  • The wrapper (off, on, a throwing job, a mode that cannot be read, the missing table, the summary validator, the bounds on records, pruning), the store, the migration's text, each job's behaviour through the registry, the routes, the manual triggers, the dashboard page, its store and its navigation.
  • pnpm typecheck, pnpm lint and the server, admin and desktop unit suites pass on the branch.

To check by hand

  • Open the Jobs page as an administrator and as a superadmin.
  • Switch a job off: its next slot does not run, and its manual trigger (where it has one) is refused. Switch it on again.
  • Read a job's run history after its next slot.

… runs

The server's scheduled jobs are declared once, in utils/jobs.ts: a name, a
title, a description, the schedule, whether the job can be switched and
whether it has a count mode. Every tick goes through one wrapper: it reads
the job's mode at that tick, runs nothing when it is off, hands the mode to
the job and records the run in job_runs, keeping the newest 200 per job.

A mode that cannot be read is the job's default mode, with one warning per
job per hour. A stored mode the job cannot run in runs nothing. A record
that cannot be written is warned about and never fails the job. A job that
runs more often than hourly is recorded only when its outcome changes, a
count is not zero or it fails, and otherwise once an hour.

A run's summary holds only counts and the words of a closed list, under
keys the job declares; a failure is recorded as utils/loggableError.ts
describes it.

The migration 20261127120000_job_registry adds job_settings and job_runs.
It is additive and goes in before the server that reads them.
Every tick of the nine scheduled jobs goes through the registry's wrapper:
the presence reaper, the voice reaper, attachment expiry, registration
hygiene, the object-store orphan sweep, report retention, the key-share
sweep, the session sweep and the discovery cycle. The wrapper sits around
each job's own lock: a job that is off claims no lock and runs nothing, and
its next tick is armed as after a run.

Each job keeps its lock, its schedule, its guards, its log lines and its
return values. It tells the run what it counted, that another node held
the lock (not recorded), that Redis could not be reached, that a guard or a
bound refused the run, or that it caught its own error; a failed run is
still logged by the job as before. The two reapers are not switchable and
never read a mode.

The admin triggers of the hygiene and orphan sweeps call the locked sweep
directly: they are neither switched nor recorded.

The server sets the database store at start, before the first job.
…h a job

GET /admin/jobs lists every job of the registry with its description, its
schedule, whether it can be switched, its mode and who set it, its last run
and its last failure, the next slot of a daily job, and whether it is
overdue (its last run older than twice the longest gap its records can
have). GET /admin/jobs/:name/runs pages one job's history newest first by
an explicit keyset on the run id (limit 1 to 100, 25 by default). Every
administrator may read them.

PUT /admin/jobs/:name { mode } is for superadmins: an unknown job or mode,
count on a job without it and any change to a job that cannot be switched
answer 400. A change is audited as job.update with the old and the new
mode, and answered with the job as the list shows it. The routes are on
the admin router, behind its authentication, administrator check and
limiter; their statements are in utils/jobStore.ts.

The audit log labels and offers job.update.
The navigation has a Jobs entry. Its page shows each scheduled job of the
server with its description, its schedule, its mode, its last run (outcome,
when, how long) and its next slot, and marks a job whose last run failed or
that is overdue. Opening a job shows its runs, newest first and paged, with
their counts and, for a failure, the error.

A superadmin switches a job through the confirmation dialog; every other
administrator reads the mode with the reason they cannot change it, and a
job that cannot be switched shows why. A refused switch toasts the
server's reason, leaves the dialog open and changes nothing on screen.
…led read

A scheduled job that was switched off does not run because its mode
could not be read. Only a missing settings table reads as the default
mode, with one warning per job per hour. Any other failure of the read,
its 10 s bound included, skips the tick: one error line per job per
hour, and a run recorded as skipped (mode-unreadable).

A job that is off cannot be run by hand either. The two manual admin
triggers (registration hygiene, orphan clean-up) go through the
registry: 409 "This job is switched off" while it is off, the error
itself when the mode cannot be read, otherwise the run is recorded with
the word "manual". A dry run reads only and stays outside the registry.

The list shows what the server will do: a stored mode the job cannot
run in is shown as not running, a row stored for a job that cannot be
switched is ignored, a job that is on with no run on record says so,
and the audit keeps the stored value as it was. A switch that was
written is answered as done even when the read for the answer fails.

A record never holds a network address: an error that carries a system
error code is stored by name, code and status. For the jobs recorded on
change, the same failure is recorded at most once in 15 minutes. A
stored summary is read against the job's declared keys.

The dashboard switches a mode with one button per mode and its
confirmation, and applies a list answer only when no later request or
switch has been answered since.
@Aizen93
Aizen93 merged commit 5ae19ad into develop Nov 28, 2026
9 checks passed
@Aizen93
Aizen93 deleted the feat/admin-jobs branch November 28, 2026 17:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant