Skip to content

Commit c90f59d

Browse files
committed
fix: update SQLiteDatabaseAdapter to recover timed-out jobs and use job-specific TTR for reservation
1 parent f7c2336 commit c90f59d

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

example-queue-project/src/general-queue.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { DbQueue } from "@muniter/queue";
2-
import { SQLiteDatabaseAdapter } from "./sqlite-adapter";
2+
import { SQLiteDatabaseAdapter } from "./sqlite-adapter.js";
33

44
export interface GeneralJobs {
55
'process-image': { url: string; width: number; height: number };

example-queue-project/src/sqlite-adapter.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@ export class SQLiteDatabaseAdapter implements DatabaseAdapter {
2626
async reserveJob(timeout: number): Promise<QueueJobRecord | null> {
2727
const now = Date.now();
2828

29+
// First, recover any timed-out reserved jobs back to waiting
30+
await run(
31+
`UPDATE jobs SET
32+
status = 'waiting',
33+
reserve_time = NULL,
34+
expire_time = NULL,
35+
attempt = attempt + 1
36+
WHERE status = 'reserved'
37+
AND expire_time < ?`,
38+
[now]
39+
);
40+
2941
const job = await get(
3042
`SELECT * FROM jobs
3143
WHERE status = 'waiting'
@@ -37,13 +49,15 @@ export class SQLiteDatabaseAdapter implements DatabaseAdapter {
3749

3850
if (!job) return null;
3951

52+
// Use the job's TTR value, not the polling timeout
53+
const jobTtr = job.ttr || 300; // Default to 5 minutes if no TTR
4054
await run(
4155
`UPDATE jobs SET
4256
status = 'reserved',
4357
reserve_time = ?,
4458
expire_time = ?
4559
WHERE id = ?`,
46-
[now, now + timeout * 1000, job.id]
60+
[now, now + jobTtr * 1000, job.id]
4761
);
4862

4963
return {

0 commit comments

Comments
 (0)