Introduce a document blob lease mechanism to prevent race conditions between document registration and orphaned blob cleanup. The new `document_blob_leases` table ensures that only one process can claim a document blob for mutation or deletion at a time. Update the orphan reaper to acquire a lease before deleting blobs and to re-check for ownership after acquiring the lease, avoiding accidental deletion of in-flight uploads. Enhance scheduled task infrastructure with per-task fencing tokens to prevent stale runners from overwriting newer results, enforce runtime limits with abort signals, and expose scheduler mode and minimum interval to the admin panel and API. Adjust task handlers to accept a context with abort support, and update documentation and environment variable references for the new cron secret and scheduling behavior. BREAKING CHANGE: Scheduled tasks now require a `document_blob_leases` table and updated handler signatures. Vercel deployments must set `CRON_SECRET` for scheduled maintenance.
22 lines
753 B
SQL
22 lines
753 B
SQL
CREATE TABLE "document_blob_leases" (
|
|
"document_id" text PRIMARY KEY NOT NULL,
|
|
"lease_owner" text NOT NULL,
|
|
"lease_until_ms" bigint NOT NULL
|
|
);
|
|
--> statement-breakpoint
|
|
CREATE TABLE "scheduled_tasks" (
|
|
"key" text PRIMARY KEY NOT NULL,
|
|
"enabled" boolean DEFAULT true NOT NULL,
|
|
"interval_ms" bigint NOT NULL,
|
|
"last_status" text DEFAULT 'idle' NOT NULL,
|
|
"lease_owner" text,
|
|
"last_run_at" bigint,
|
|
"last_duration_ms" bigint,
|
|
"last_error" text,
|
|
"last_result_json" text,
|
|
"next_run_at" bigint,
|
|
"run_requested" boolean DEFAULT false NOT NULL,
|
|
"running_since" bigint,
|
|
"updated_at" bigint DEFAULT (extract(epoch from now()) * 1000)::bigint NOT NULL,
|
|
CONSTRAINT "scheduled_tasks_interval_ms_positive" CHECK ("scheduled_tasks"."interval_ms" > 0)
|
|
);
|