2026-09-17 11:57:19 -07:00
|
|
|
import { Job, JobSeed, JobType, PendingCheck } from "@/domain/jobs/job.type.js";
|
2026-06-11 14:18:21 -07:00
|
|
|
|
|
|
|
|
export type JobPageQuery = {
|
|
|
|
|
page?: number;
|
|
|
|
|
rowsPerPage?: number;
|
|
|
|
|
};
|
|
|
|
|
export type JobPage = {
|
|
|
|
|
jobs: Job[];
|
|
|
|
|
count: number;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export interface IJobsRepository {
|
|
|
|
|
// ********************
|
|
|
|
|
// Claims
|
|
|
|
|
// ********************
|
|
|
|
|
|
|
|
|
|
// Atomically claim a job
|
|
|
|
|
claimDue(type: JobType, now: number): Promise<Job | null>;
|
|
|
|
|
|
2026-06-12 16:04:31 -07:00
|
|
|
// locks up to `limit` due rows in one batch
|
|
|
|
|
claimDueBatch(type: JobType, limit: number, now: number): Promise<Job[]>;
|
|
|
|
|
|
2026-06-15 12:05:15 -07:00
|
|
|
// Extends the locks we still hold. Returns how many were renewed
|
|
|
|
|
renewLocks(ids: string[], now: number): Promise<number>;
|
|
|
|
|
|
2026-06-11 14:18:21 -07:00
|
|
|
// Completes a repeating job
|
|
|
|
|
recordSuccess(id: string, nextScheduledAt: number, intervalMs: number, now: number): Promise<boolean>;
|
|
|
|
|
|
|
|
|
|
// Record failure, release lease, bump fail count, record reason, reset nextScheduledAt for retry
|
|
|
|
|
recordFailure(id: string, error: unknown, now: number): Promise<boolean>;
|
|
|
|
|
|
2026-06-12 15:43:36 -07:00
|
|
|
recordOneShot(id: string, now: number): Promise<boolean>;
|
|
|
|
|
|
2026-06-11 14:18:21 -07:00
|
|
|
// ********************
|
2026-09-17 11:57:19 -07:00
|
|
|
// Hand off to evaluator // This is accomplished pushing newly inserted Check IDs onto the evaluation row
|
2026-06-11 14:18:21 -07:00
|
|
|
// ********************
|
2026-09-17 11:57:19 -07:00
|
|
|
upsertEvaluate(monitorId: string, pending: PendingCheck[], now: number): Promise<boolean>;
|
|
|
|
|
|
|
|
|
|
// Removes processed checks from the evaluate row.
|
|
|
|
|
pullEvaluated(id: string, checkIds: string[]): Promise<boolean>;
|
2026-06-11 14:18:21 -07:00
|
|
|
|
|
|
|
|
// ********************
|
|
|
|
|
// Job CRUD
|
|
|
|
|
// ********************
|
|
|
|
|
|
2026-06-12 16:04:31 -07:00
|
|
|
// Create jobs. Takes a JobSeed
|
2026-06-12 15:43:36 -07:00
|
|
|
upsertJob(job: JobSeed): Promise<boolean>;
|
2026-06-11 14:18:21 -07:00
|
|
|
|
2026-06-17 09:38:21 -07:00
|
|
|
// Creates cleanup jobs, these are executed immediately so need their own method
|
|
|
|
|
upsertCleanupJob(job: JobSeed): Promise<boolean>;
|
|
|
|
|
|
2026-06-11 14:18:21 -07:00
|
|
|
// Pause/Resume
|
|
|
|
|
setActiveById(refId: string, isActive: boolean): Promise<boolean>;
|
|
|
|
|
|
|
|
|
|
// Edit job schedule
|
|
|
|
|
updateScheduleById(refId: string, type: JobType, intervalMs: number | null): Promise<boolean>;
|
|
|
|
|
|
2026-06-18 10:17:33 -07:00
|
|
|
// Re-arm the check/geo-check rows. Add jitter to prevent herd
|
|
|
|
|
markMonitorsDue(monitorIds: string[], now: number): Promise<number>;
|
|
|
|
|
|
2026-06-11 14:18:21 -07:00
|
|
|
// Delete, drop all rows
|
|
|
|
|
deleteById(refId: string): Promise<boolean>;
|
|
|
|
|
|
2026-09-15 08:59:13 +01:00
|
|
|
// Delete a single job of type
|
|
|
|
|
deleteByIdAndType(refId: string, type: JobType): Promise<boolean>;
|
2026-06-12 15:43:36 -07:00
|
|
|
|
2026-09-15 08:59:13 +01:00
|
|
|
// Global rows (refId null) are addressed by their canonical _id, see jobId
|
|
|
|
|
deleteGlobalJob(type: JobType): Promise<boolean>;
|
2026-09-15 08:42:09 +01:00
|
|
|
// Delete a global row only if nobody has rescheduled it since it was claimed (nextScheduledAt unchanged)
|
|
|
|
|
deleteGlobalJobIfUnchanged(type: JobType, nextScheduledAt: number): Promise<boolean>;
|
|
|
|
|
|
2026-06-17 09:38:21 -07:00
|
|
|
// Delete jobs that reference monitors that no longer exist
|
|
|
|
|
deleteByMonitorIdsNotIn(monitorIds: string[]): Promise<number>;
|
|
|
|
|
|
2026-06-11 14:18:21 -07:00
|
|
|
// Get job
|
|
|
|
|
findById(refId: string): Promise<Job[]>;
|
|
|
|
|
|
|
|
|
|
// ********************
|
|
|
|
|
// Observability
|
|
|
|
|
// ********************
|
|
|
|
|
findPage(pagination: JobPageQuery): Promise<JobPage>;
|
|
|
|
|
findAll(): Promise<Job[]>;
|
2026-06-25 11:02:18 -07:00
|
|
|
countDueBacklog(now: number): Promise<number>; // Scaling signal
|
2026-06-11 14:18:21 -07:00
|
|
|
}
|