handle renewing locks for long running jobs

This commit is contained in:
Alex Holliday committed 2026-06-15 12:05:15 -07:00
1 parent 90bbc764fc
commit 142ecaaf8b
6 files changed
+41 -1505

No files matched your search

@@ -20,6 +20,9 @@ export interface IJobsRepository {
// locks up to `limit` due rows in one batch
claimDueBatch(type: JobType, limit: number, now: number): Promise<Job[]>;
// Extends the locks we still hold. Returns how many were renewed
renewLocks(ids: string[], now: number): Promise<number>;
// Completes a repeating job
recordSuccess(id: string, nextScheduledAt: number, intervalMs: number, now: number): Promise<boolean>;
@@ -76,6 +76,15 @@ class MongoJobsRepository implements IJobsRepository {
return docs.map(this.toEntity);
};
renewLocks = async (ids: string[], now: number) => {
if (ids.length === 0) return 0;
const res = await JobModel.updateMany(
{ _id: { $in: ids }, lockedBy: this.workerId }, // only locks held by this worker can be renewed
{ $set: { lockedUntil: now + LOCK_MS } }
);
return res.modifiedCount;
};
recordSuccess = async (id: string, scheduledRunAt: number, intervalMs: number, now: number) => {
// Fixed rate job scheduling
@@ -89,6 +98,7 @@ class MongoJobsRepository implements IJobsRepository {
const res = await JobModel.updateOne(
{
_id: id,
lockedBy: this.workerId, // Only the worker that claimed can record success
},
{
$set: {
@@ -109,6 +119,7 @@ class MongoJobsRepository implements IJobsRepository {
const res = await JobModel.updateOne(
{
_id: id,
lockedBy: this.workerId, // Only the worker that claimed can record success
},
{
$set: {
@@ -129,6 +140,7 @@ class MongoJobsRepository implements IJobsRepository {
const res = await JobModel.updateOne(
{
_id: id,
lockedBy: this.workerId, // Only the worker that claimed can record success
},
{
$set: {