mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-28 07:30:04 +00:00
feat(server): take a database lease before a cron tick runs
waitForCompletion only kept a tick from overlapping itself inside one process. Two processes on the same database (a rolling update's overlap, a second replica, a dev container pointed at the same file) both ran every job: two auto-backups, double syncs and retention sweeps, and reminder runs racing each other. A new migration adds scheduler_leases, and CronRegistrarService now runs every registered tick through SchedulerLeasesRepository.acquire: one conditional UPDATE moves the job's row to this process when it is free (no holder, a lapsed holder, or this process already), so exactly one process wins. A running tick renews its lease every 20 seconds, a crashed holder frees the job after a minute, and a finished tick keeps the lease for 30 seconds so a peer whose timer fires late does not run the same tick again. The per-row reminder claims stay as they are; jobs need no change of their own.
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
import { Migration } from '@mikro-orm/migrations';
|
||||
|
||||
/**
|
||||
* A cron tick runs in one process. Every job registered through
|
||||
* CronRegistrarService takes a row here before its tick body runs: the row
|
||||
* names the process holding the job and until when, and a process only runs
|
||||
* the tick when one conditional UPDATE moved the row to itself. Two processes
|
||||
* on one database (a rolling update's overlap, a second replica, a stray dev
|
||||
* container) therefore no longer both send the reminders, take the backup or
|
||||
* run the sync.
|
||||
*
|
||||
* `expires_at` is in milliseconds since the epoch. A row is only ever written
|
||||
* by the scheduler, so there is nothing to backfill.
|
||||
*/
|
||||
export class Migration20200101042500_a_cron_tick_runs_in_one_process extends Migration {
|
||||
override name = 'Migration20200101042500_a_cron_tick_runs_in_one_process';
|
||||
|
||||
override async up(): Promise<void> {
|
||||
this.addSql(`
|
||||
CREATE TABLE IF NOT EXISTS scheduler_leases (
|
||||
name TEXT NOT NULL PRIMARY KEY,
|
||||
owner TEXT NOT NULL,
|
||||
expires_at INTEGER NOT NULL
|
||||
)
|
||||
`);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user