fix: serialize autonomy job leases

This commit is contained in:
ImBenji
2026-08-04 21:07:42 +01:00
parent 5037e1e192
commit b9ee10a83a
6 changed files with 14 additions and 1 deletions
+9 -1
View File
@@ -31,7 +31,15 @@ function leaseNextJob(db, workerId, leaseSeconds = 60, jobTypes = null) {
`).run(workerId, `+${Math.max(1, Math.floor(leaseSeconds))} seconds`, job.id);
return updated.changes ? { ...job, status: 'leased', leased_by: workerId } : null;
});
return tx();
try {
// Acquire the write reservation before selecting. A deferred transaction can
// otherwise read a snapshot, lose the writer race, and fail with
// SQLITE_BUSY_SNAPSHOT when it attempts the lease update.
return tx.immediate();
} catch (error) {
if (String(error.code || '').startsWith('SQLITE_BUSY')) return null;
throw error;
}
}
function completeJob(db, id, workerId) {