-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathIndexedDbQueueStorage.ts
More file actions
973 lines (876 loc) · 34.1 KB
/
Copy pathIndexedDbQueueStorage.ts
File metadata and controls
973 lines (876 loc) · 34.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
/**
* @license
* Copyright 2025 Steven Roussey <sroussey@gmail.com>
* SPDX-License-Identifier: Apache-2.0
*/
import type {
IQueueStorage,
JobStorageFormat,
PrefixColumn,
QueueChangePayload,
QueueStorageOptions,
QueueSubscribeOptions,
} from "@workglow/job-queue";
import { JobStatus, validateLeaseMs } from "@workglow/job-queue";
import { HybridSubscriptionManager } from "@workglow/storage";
import { createServiceToken, deepEqual, getLogger, makeFingerprint, uuid4 } from "@workglow/util";
import { IndexedDbMigrationRunner } from "../migrations/IndexedDbMigrationRunner";
import { indexedDbQueueMigrations } from "../migrations/indexedDbQueueMigrations";
import { openIdb } from "../storage/openIdb";
export const INDEXED_DB_QUEUE_STORAGE = createServiceToken<IQueueStorage<any, any>>(
"jobqueue.storage.indexedDb"
);
export interface IndexedDbQueueStorageOptions extends QueueStorageOptions {
/** Enable BroadcastChannel notifications (default: true) */
readonly useBroadcastChannel?: boolean;
/** Backup polling interval in ms (default: 5000, 0 to disable) */
readonly backupPollingIntervalMs?: number;
}
export class IndexedDbQueueStorage<Input, Output> implements IQueueStorage<Input, Output> {
public readonly scope = "process" as const;
private db: IDBDatabase | undefined;
private readonly tableName: string;
protected readonly prefixes: readonly PrefixColumn[];
protected readonly prefixValues: Readonly<Record<string, string | number>>;
private hybridManager: HybridSubscriptionManager<
JobStorageFormat<Input, Output>,
unknown,
QueueChangePayload<Input, Output>
> | null = null;
private readonly hybridOptions: {
readonly useBroadcastChannel: boolean;
readonly backupPollingIntervalMs: number;
};
constructor(
public readonly queueName: string,
options: IndexedDbQueueStorageOptions = {}
) {
this.prefixes = options.prefixes ?? [];
this.prefixValues = options.prefixValues ?? {};
this.hybridOptions = {
useBroadcastChannel: options.useBroadcastChannel ?? true,
backupPollingIntervalMs: options.backupPollingIntervalMs ?? 5000,
};
// Table name varies by prefix configuration to avoid conflicts
if (this.prefixes.length > 0) {
const prefixNames = this.prefixes.map((p) => p.name).join("_");
this.tableName = `jobs_${prefixNames}`;
} else {
this.tableName = "jobs";
}
}
private matchesPrefixes(job: JobStorageFormat<Input, Output> & Record<string, unknown>): boolean {
for (const [key, value] of Object.entries(this.prefixValues)) {
if (job[key] !== value) {
return false;
}
}
return true;
}
/** Prefix values as an array in column order for index key construction. */
private getPrefixKeyValues(): Array<string | number> {
return this.prefixes.map((p) => this.prefixValues[p.name]);
}
private async getDb(): Promise<IDBDatabase> {
if (this.db) return this.db;
await this.migrate();
return this.db!;
}
/**
* Returns the versioned migrations that this storage's object store +
* indexes depend on. Callers can compose them with other storages'
* migrations under a shared {@link IndexedDbMigrationRunner}; otherwise
* call {@link migrate}.
*/
public getMigrations() {
return indexedDbQueueMigrations(this.tableName, this.prefixes);
}
/**
* Applies any pending migrations for this queue's IndexedDB database, then
* opens a long-lived connection at the migrated version. Idempotent — a
* second call closes any prior handle (so it doesn't pin the
* pre-migration version or block another tab's upgrade), reruns the
* runner (no-op if the bookkeeping store says everything is applied), and
* reopens the connection.
*/
public async migrate(): Promise<void> {
if (this.db) {
try {
this.db.close();
} catch {
// ignore — close is best-effort
}
this.db = undefined;
}
const runner = new IndexedDbMigrationRunner(this.tableName);
await runner.run(this.getMigrations());
this.db = await openIdb(this.tableName);
}
public async add(job: JobStorageFormat<Input, Output>): Promise<unknown> {
const db = await this.getDb();
const now = new Date().toISOString();
const jobWithPrefixes = job as JobStorageFormat<Input, Output> & Record<string, unknown>;
jobWithPrefixes.id = jobWithPrefixes.id ?? uuid4();
jobWithPrefixes.job_run_id = jobWithPrefixes.job_run_id ?? uuid4();
jobWithPrefixes.queue = this.queueName;
jobWithPrefixes.fingerprint = await makeFingerprint(jobWithPrefixes.input);
jobWithPrefixes.status = JobStatus.PENDING;
jobWithPrefixes.progress = 0;
jobWithPrefixes.progress_message = "";
jobWithPrefixes.progress_details = null;
jobWithPrefixes.created_at = now;
jobWithPrefixes.visible_at = now;
for (const [key, value] of Object.entries(this.prefixValues)) {
jobWithPrefixes[key] = value;
}
const tx = db.transaction(this.tableName, "readwrite");
const store = tx.objectStore(this.tableName);
return new Promise((resolve, reject) => {
const request = store.add(jobWithPrefixes);
tx.oncomplete = () => {
this.hybridManager?.notifyLocalChange();
resolve(jobWithPrefixes.id);
};
tx.onerror = () => reject(tx.error);
request.onerror = () => reject(request.error);
});
}
async get(id: unknown): Promise<JobStorageFormat<Input, Output> | undefined> {
const db = await this.getDb();
const tx = db.transaction(this.tableName, "readonly");
const store = tx.objectStore(this.tableName);
const request = store.get(id as string);
return new Promise((resolve, reject) => {
request.onsuccess = () => {
const job = request.result as
| (JobStorageFormat<Input, Output> & Record<string, unknown>)
| undefined;
if (job && job.queue === this.queueName && this.matchesPrefixes(job)) {
resolve(job);
} else {
resolve(undefined);
}
};
request.onerror = () => reject(request.error);
tx.onerror = () => reject(tx.error);
});
}
public async peek(
status: JobStatus = JobStatus.PENDING,
num: number = 100
): Promise<JobStorageFormat<Input, Output>[]> {
const db = await this.getDb();
const tx = db.transaction(this.tableName, "readonly");
const store = tx.objectStore(this.tableName);
const index = store.index("queue_status_visible_at");
const prefixKeyValues = this.getPrefixKeyValues();
return new Promise((resolve, reject) => {
const ret = new Map<unknown, JobStorageFormat<Input, Output>>();
const keyRange = IDBKeyRange.bound(
[...prefixKeyValues, this.queueName, status, ""],
[...prefixKeyValues, this.queueName, status, "\uffff"]
);
const cursorRequest = index.openCursor(keyRange);
const handleCursor = (e: Event) => {
const cursor = (e.target as IDBRequest<IDBCursorWithValue>).result;
if (!cursor || ret.size >= num) {
resolve(Array.from(ret.values()));
return;
}
const job = cursor.value as JobStorageFormat<Input, Output> & Record<string, unknown>;
// Map keyed by id ensures no duplicates
if (this.matchesPrefixes(job)) {
ret.set(cursor.value.id, cursor.value);
}
cursor.continue();
};
cursorRequest.onsuccess = handleCursor;
cursorRequest.onerror = () => reject(cursorRequest.error);
tx.onerror = () => reject(tx.error);
});
}
/**
* Retrieves the next job from the queue using optimistic locking.
* Claims PENDING jobs ready to run, and also reclaims PROCESSING jobs whose
* lease has expired (crash recovery). Sets lease_expires_at on the claimed row.
*
* IndexedDB uses snapshot isolation, so concurrent transactions can both see the same
* PENDING job. To prevent processing the same job multiple times, this method:
* 1. Claims a job by setting it to PROCESSING with a unique claim token
* 2. After the transaction completes, re-reads the job to verify the claim succeeded
* 3. If another worker claimed it first (different claim token), returns undefined
*
* @param workerId - Worker ID to associate with the job (required)
* @param opts - Optional options including leaseMs (default 30000)
* @returns A promise that resolves to the next job or undefined if the queue is empty.
*/
public async next(
workerId: string,
opts?: { leaseMs?: number }
): Promise<JobStorageFormat<Input, Output> | undefined> {
const leaseMs = opts?.leaseMs ?? 30000;
validateLeaseMs(leaseMs, "leaseMs");
const db = await this.getDb();
const tx = db.transaction(this.tableName, "readwrite");
const store = tx.objectStore(this.tableName);
const now = new Date().toISOString();
const leaseExpiry = new Date(Date.now() + leaseMs).toISOString();
const prefixKeyValues = this.getPrefixKeyValues();
// Used after the tx to verify we actually won the race to claim this job.
const claimToken = workerId;
const jobToReturn = await new Promise<JobStorageFormat<Input, Output> | undefined>(
(resolve, reject) => {
let claimedJob: JobStorageFormat<Input, Output> | undefined;
const tryClaimJob = (job: JobStorageFormat<Input, Output> & Record<string, unknown>) => {
// Lease-expiry reclaim consumes one attempt against max_attempts;
// a fresh PENDING claim does not (the worker's validateJobState
// FAILs the job when attempts >= max_attempts on the next step).
const isLeaseExpiryReclaim = job.status === JobStatus.PROCESSING;
job.status = JobStatus.PROCESSING;
job.last_attempted_at = now;
job.lease_owner = claimToken;
job.lease_expires_at = leaseExpiry;
if (isLeaseExpiryReclaim) {
job.attempts = ((job.attempts as number | undefined) ?? 0) + 1;
}
// Always clear stale abort_requested_at on (re)claim. A PROCESSING
// row may have had abort_requested_at set before the previous
// worker crashed; the new owner must start with a clean slate.
job.abort_requested_at = null;
try {
const updateRequest = store.put(job);
updateRequest.onsuccess = () => {
claimedJob = job;
};
updateRequest.onerror = () => {};
} catch {
// ignore
}
};
const pendingIndex = store.index("queue_status_visible_at");
const pendingRequest = pendingIndex.openCursor(
IDBKeyRange.bound(
[...prefixKeyValues, this.queueName, JobStatus.PENDING, ""],
[...prefixKeyValues, this.queueName, JobStatus.PENDING, now],
false,
false
)
);
pendingRequest.onsuccess = (e) => {
const cursor = (e.target as IDBRequest<IDBCursorWithValue>).result;
if (!cursor) {
// No PENDING job found — try expired-lease PROCESSING job
if (!claimedJob) {
tryExpiredLeaseScan();
}
return;
}
const job = cursor.value as JobStorageFormat<Input, Output> & Record<string, unknown>;
if (
job.queue !== this.queueName ||
job.status !== JobStatus.PENDING ||
!this.matchesPrefixes(job)
) {
cursor.continue();
return;
}
tryClaimJob(job);
// Don't continue cursor — claim attempted
};
pendingRequest.onerror = () => reject(pendingRequest.error);
const tryExpiredLeaseScan = () => {
const processingIndex = store.index("queue_status_visible_at");
const processingRequest = processingIndex.openCursor(
IDBKeyRange.bound(
[...prefixKeyValues, this.queueName, JobStatus.PROCESSING, ""],
[...prefixKeyValues, this.queueName, JobStatus.PROCESSING, ""],
false,
false
)
);
processingRequest.onsuccess = (e) => {
const cursor = (e.target as IDBRequest<IDBCursorWithValue>).result;
if (!cursor) return; // none found, tx.oncomplete will resolve with undefined
const job = cursor.value as JobStorageFormat<Input, Output> & Record<string, unknown>;
if (
job.queue !== this.queueName ||
job.status !== JobStatus.PROCESSING ||
!this.matchesPrefixes(job)
) {
cursor.continue();
return;
}
// null lease_expires_at = expired per spec
if (job.lease_expires_at && job.lease_expires_at >= now) {
cursor.continue();
return;
}
tryClaimJob(job);
};
processingRequest.onerror = () => reject(processingRequest.error);
};
tx.oncomplete = () => {
if (claimedJob) {
this.hybridManager?.notifyLocalChange();
}
resolve(claimedJob);
};
tx.onerror = () => reject(tx.error);
}
);
if (!jobToReturn) {
return undefined;
}
// Verify we actually won the race by re-reading the job
const verifiedJob = await this.get(jobToReturn.id);
if (!verifiedJob) {
return undefined;
}
if (verifiedJob.lease_owner !== claimToken) {
return undefined;
}
if (verifiedJob.status !== JobStatus.PROCESSING) {
return undefined;
}
return verifiedJob;
}
/**
* Extend the lease on a currently PROCESSING job.
* @param id - The ID of the job to extend the lease for
* @param workerId - Worker ID that must match the current lease owner (lease_owner)
* @param ms - Number of milliseconds to extend the lease by
*/
public async extendLease(id: unknown, workerId: string, ms: number): Promise<void> {
validateLeaseMs(ms, "ms");
const job = await this.get(id);
if (!job || job.status !== JobStatus.PROCESSING || job.lease_owner !== workerId) {
throw new Error(
`extendLease failed: job ${String(id)} is not PROCESSING or lease is not owned by worker ${workerId}`
);
}
job.lease_expires_at = new Date(Date.now() + ms).toISOString();
await this.put(job);
}
public async size(status = JobStatus.PENDING): Promise<number> {
const db = await this.getDb();
const prefixKeyValues = this.getPrefixKeyValues();
return new Promise((resolve, reject) => {
const tx = db.transaction(this.tableName, "readonly");
const store = tx.objectStore(this.tableName);
const index = store.index("queue_status");
const keyRange = IDBKeyRange.only([...prefixKeyValues, this.queueName, status]);
const request = index.count(keyRange);
request.onsuccess = () => resolve(request.result);
request.onerror = () => reject(request.error);
tx.onerror = () => reject(tx.error);
});
}
public async complete(job: JobStorageFormat<Input, Output>): Promise<void> {
const db = await this.getDb();
const tx = db.transaction(this.tableName, "readwrite");
const store = tx.objectStore(this.tableName);
return new Promise((resolve, reject) => {
const getReq = store.get(job.id as string);
getReq.onsuccess = () => {
const existing = getReq.result as
| (JobStorageFormat<Input, Output> & Record<string, unknown>)
| undefined;
if (!existing || existing.queue !== this.queueName || !this.matchesPrefixes(existing)) {
// Contract: complete() must silently no-op (not throw) when the row is
// missing or belongs to another queue. Lease-expiry races can legitimately
// make the target row disappear between claim and complete, so the
// transaction is allowed to commit with no write and resolve.
return;
}
const currentAttempts = existing.attempts ?? 0;
job.attempts = currentAttempts + 1;
// PENDING-retry / terminal completion: always clear
// abort_requested_at so a flag set DURING the attempt does not
// survive into the next retry and immediately cancel it.
const jobAsRecord = job as JobStorageFormat<Input, Output> & Record<string, unknown>;
jobAsRecord.abort_requested_at = null;
job.queue = this.queueName;
const jobWithPrefixes = jobAsRecord;
for (const [key, value] of Object.entries(this.prefixValues)) {
jobWithPrefixes[key] = value;
}
const putReq = store.put(jobWithPrefixes);
putReq.onsuccess = () => {};
putReq.onerror = () => reject(putReq.error);
};
getReq.onerror = () => reject(getReq.error);
tx.oncomplete = () => {
this.hybridManager?.notifyLocalChange();
resolve();
};
tx.onerror = () => reject(tx.error);
});
}
public async releaseClaim(id: unknown): Promise<void> {
const job = await this.get(id);
if (!job) return;
job.status = JobStatus.PENDING;
job.lease_owner = null;
job.progress = 0;
job.progress_message = "";
job.progress_details = null;
// Clear stale abort_requested_at — an abort flag set during the previous
// claim must not immediately cancel the next worker that picks up the row.
(job as unknown as Record<string, unknown>).abort_requested_at = null;
await this.put(job);
}
/**
* Aborts a job.
* - If PENDING: immediately mark as FAILED with abort_requested_at set.
* - If PROCESSING: set abort_requested_at only (leave status as PROCESSING).
* - Otherwise: no-op.
*/
public async abort(id: unknown): Promise<void> {
const job = await this.get(id);
if (!job) return;
const now = new Date().toISOString();
if (job.status === JobStatus.PENDING) {
job.status = JobStatus.FAILED;
job.abort_requested_at = now;
job.completed_at = now;
// Use put() (not complete()) so attempts is NOT bumped — the worker
// never actually attempted this job. Matches the cross-backend
// contract verified in InMemory/Postgres.
await this.put(job);
} else if (job.status === JobStatus.PROCESSING) {
job.abort_requested_at = now;
await this.put(job);
}
}
/** Force-overwrite status without touching attempts (used to persist DISABLED after lease release). */
public async saveStatus(id: unknown, status: string): Promise<void> {
const db = await this.getDb();
const tx = db.transaction(this.tableName, "readwrite");
const store = tx.objectStore(this.tableName);
const getRequest = store.get(id as IDBValidKey);
return new Promise((resolve, reject) => {
getRequest.onsuccess = () => {
const record = getRequest.result;
if (!record) {
resolve();
return;
}
const putRequest = store.put({ ...record, status });
putRequest.onsuccess = () => resolve();
putRequest.onerror = () => reject(putRequest.error);
};
getRequest.onerror = () => reject(getRequest.error);
});
}
public async getByRunId(job_run_id: string): Promise<JobStorageFormat<Input, Output>[]> {
const db = await this.getDb();
const tx = db.transaction(this.tableName, "readonly");
const store = tx.objectStore(this.tableName);
const index = store.index("queue_job_run_id");
const prefixKeyValues = this.getPrefixKeyValues();
const keyRange = IDBKeyRange.only([...prefixKeyValues, this.queueName, job_run_id]);
const request = index.getAll(keyRange);
return new Promise((resolve, reject) => {
request.onsuccess = () => {
const results = (request.result || []).filter(
(job: JobStorageFormat<Input, Output> & Record<string, unknown>) =>
this.matchesPrefixes(job)
);
resolve(results);
};
request.onerror = () => reject(request.error);
tx.onerror = () => reject(tx.error);
});
}
/**
* Terminal write that does NOT bump `attempts`. See IQueueStorage.finalize
* for the rationale (avoids double-counting on ack/fail).
*/
public async finalize(
id: unknown,
fields: {
output?: Output | null;
error?: string | null;
error_code?: string | null;
status?: JobStatus;
completed_at?: string | null;
abort_requested_at?: string | null;
lease_owner?: string | null;
progress?: number;
progress_message?: string;
progress_details?: Record<string, any> | null;
visible_at?: string | null;
}
): Promise<void> {
const existing = await this.get(id);
if (!existing) return;
const updated = existing as JobStorageFormat<Input, Output> & Record<string, unknown>;
if ("output" in fields) updated.output = fields.output ?? null;
if ("error" in fields) updated.error = fields.error ?? null;
if ("error_code" in fields) updated.error_code = fields.error_code ?? null;
if ("status" in fields) updated.status = fields.status;
if ("completed_at" in fields) updated.completed_at = fields.completed_at ?? null;
if ("abort_requested_at" in fields) {
updated.abort_requested_at = fields.abort_requested_at ?? null;
}
if ("lease_owner" in fields) updated.lease_owner = fields.lease_owner ?? null;
if ("progress" in fields) updated.progress = fields.progress ?? 0;
if ("progress_message" in fields) updated.progress_message = fields.progress_message ?? "";
if ("progress_details" in fields) updated.progress_details = fields.progress_details ?? null;
if ("visible_at" in fields) updated.visible_at = fields.visible_at ?? null;
await this.put(updated);
}
/**
* Atomically writes status=DISABLED, releases the lease, clears progress
* fields, and stamps completed_at (preserving an existing value). IDB
* is single-threaded per-origin within a JS context, so the get + finalize
* pair is observably atomic — no other transaction can interleave between
* them inside this microtask chain.
*/
public async markDisabled(id: unknown): Promise<void> {
const existing = await this.get(id);
if (!existing) return;
const completedAt = existing.completed_at ?? new Date().toISOString();
await this.finalize(id, {
status: JobStatus.DISABLED,
completed_at: completedAt,
lease_owner: null,
progress: 0,
progress_message: "",
progress_details: null,
});
}
public async deleteAll(): Promise<void> {
const db = await this.getDb();
const tx = db.transaction(this.tableName, "readwrite");
const store = tx.objectStore(this.tableName);
const index = store.index("queue_status");
const prefixKeyValues = this.getPrefixKeyValues();
return new Promise((resolve, reject) => {
const keyRange = IDBKeyRange.bound(
[...prefixKeyValues, this.queueName, ""],
[...prefixKeyValues, this.queueName, "\uffff"]
);
const request = index.openCursor(keyRange);
request.onsuccess = (event) => {
const cursor = (event.target as IDBRequest<IDBCursorWithValue>).result;
if (cursor) {
const job = cursor.value as JobStorageFormat<Input, Output> & Record<string, unknown>;
if (job.queue === this.queueName && this.matchesPrefixes(job)) {
const deleteRequest = cursor.delete();
deleteRequest.onsuccess = () => {
cursor.continue();
};
deleteRequest.onerror = () => {
cursor.continue();
};
} else {
cursor.continue();
}
}
};
tx.oncomplete = () => {
this.hybridManager?.notifyLocalChange();
resolve();
};
tx.onerror = () => reject(tx.error);
request.onerror = () => reject(request.error);
});
}
public async outputForInput(input: Input): Promise<Output | null> {
const fingerprint = await makeFingerprint(input);
const db = await this.getDb();
const tx = db.transaction(this.tableName, "readonly");
const store = tx.objectStore(this.tableName);
const index = store.index("queue_fingerprint_status");
const prefixKeyValues = this.getPrefixKeyValues();
const request = index.get([
...prefixKeyValues,
this.queueName,
fingerprint,
JobStatus.COMPLETED,
]);
return new Promise((resolve, reject) => {
request.onsuccess = () => {
const job = request.result as
| (JobStorageFormat<Input, Output> & Record<string, unknown>)
| undefined;
if (job && this.matchesPrefixes(job)) {
resolve(job.output ?? null);
} else {
resolve(null);
}
};
request.onerror = () => reject(request.error);
tx.onerror = () => reject(tx.error);
});
}
public async saveProgress(
id: unknown,
progress: number,
message: string,
details: Record<string, any> | null
): Promise<void> {
const job = await this.get(id);
if (!job) {
// Contract: progress updates for a missing job silently no-op — the job may
// have already completed/been removed, or a lease-expiry race removed it.
getLogger().warn("Job not found for progress update", { id });
return;
}
job.progress = progress;
job.progress_message = message;
job.progress_details = details;
await this.put(job);
}
/**
* Persists a job to the store without modifying attempts or other completion logic.
*/
private async put(job: JobStorageFormat<Input, Output>): Promise<void> {
const db = await this.getDb();
const tx = db.transaction(this.tableName, "readwrite");
const store = tx.objectStore(this.tableName);
job.queue = this.queueName;
const jobWithPrefixes = job as JobStorageFormat<Input, Output> & Record<string, unknown>;
for (const [key, value] of Object.entries(this.prefixValues)) {
jobWithPrefixes[key] = value;
}
return new Promise((resolve, reject) => {
const putReq = store.put(jobWithPrefixes);
putReq.onerror = () => reject(putReq.error);
tx.oncomplete = () => {
this.hybridManager?.notifyLocalChange();
resolve();
};
tx.onerror = () => reject(tx.error);
});
}
public async delete(id: unknown): Promise<void> {
const job = await this.get(id);
if (!job) return;
const db = await this.getDb();
const tx = db.transaction(this.tableName, "readwrite");
const store = tx.objectStore(this.tableName);
const request = store.delete(id as string);
return new Promise((resolve, reject) => {
request.onsuccess = () => resolve();
request.onerror = () => reject(request.error);
tx.oncomplete = () => {
this.hybridManager?.notifyLocalChange();
};
tx.onerror = () => reject(tx.error);
});
}
public async deleteJobsByStatusAndAge(status: JobStatus, olderThanMs: number): Promise<void> {
const db = await this.getDb();
const tx = db.transaction(this.tableName, "readwrite");
const store = tx.objectStore(this.tableName);
const index = store.index("queue_status");
const cutoffDate = new Date(Date.now() - olderThanMs).toISOString();
const prefixKeyValues = this.getPrefixKeyValues();
const keyRange = IDBKeyRange.only([...prefixKeyValues, this.queueName, status]);
return new Promise((resolve, reject) => {
const request = index.openCursor(keyRange);
request.onsuccess = (event) => {
const cursor = (event.target as IDBRequest<IDBCursorWithValue>).result;
if (cursor) {
const job = cursor.value as JobStorageFormat<Input, Output> & Record<string, unknown>;
if (
job.queue === this.queueName &&
this.matchesPrefixes(job) &&
job.status === status &&
job.completed_at &&
job.completed_at <= cutoffDate
) {
cursor.delete();
}
cursor.continue();
}
};
tx.oncomplete = () => {
this.hybridManager?.notifyLocalChange();
resolve();
};
tx.onerror = () => reject(tx.error);
request.onerror = () => reject(request.error);
});
}
private async getAllJobs(): Promise<Array<JobStorageFormat<Input, Output>>> {
const db = await this.getDb();
const tx = db.transaction(this.tableName, "readonly");
const store = tx.objectStore(this.tableName);
const index = store.index("queue_status");
const prefixKeyValues = this.getPrefixKeyValues();
return new Promise((resolve, reject) => {
const jobs: Array<JobStorageFormat<Input, Output>> = [];
const keyRange = IDBKeyRange.bound(
[...prefixKeyValues, this.queueName, ""],
[...prefixKeyValues, this.queueName, "\uffff"]
);
const request = index.openCursor(keyRange);
request.onsuccess = (event) => {
const cursor = (event.target as IDBRequest<IDBCursorWithValue>).result;
if (cursor) {
const job = cursor.value as JobStorageFormat<Input, Output> & Record<string, unknown>;
if (job.queue === this.queueName && this.matchesPrefixes(job)) {
jobs.push(job);
}
cursor.continue();
}
};
tx.oncomplete = () => resolve(jobs);
tx.onerror = () => reject(tx.error);
request.onerror = () => reject(request.error);
});
}
private async getAllJobsWithFilter(
prefixFilter: Readonly<Record<string, string | number>>
): Promise<Array<JobStorageFormat<Input, Output>>> {
const db = await this.getDb();
const tx = db.transaction(this.tableName, "readonly");
const store = tx.objectStore(this.tableName);
return new Promise((resolve, reject) => {
const jobs: Array<JobStorageFormat<Input, Output>> = [];
const request = store.openCursor();
request.onsuccess = (event) => {
const cursor = (event.target as IDBRequest<IDBCursorWithValue>).result;
if (cursor) {
const job = cursor.value as JobStorageFormat<Input, Output> & Record<string, unknown>;
if (job.queue !== this.queueName) {
cursor.continue();
return;
}
if (Object.keys(prefixFilter).length === 0) {
jobs.push(job);
} else {
let matches = true;
for (const [key, value] of Object.entries(prefixFilter)) {
if (job[key] !== value) {
matches = false;
break;
}
}
if (matches) {
jobs.push(job);
}
}
cursor.continue();
}
};
tx.oncomplete = () => resolve(jobs);
tx.onerror = () => reject(tx.error);
request.onerror = () => reject(request.error);
});
}
private isCustomPrefixFilter(prefixFilter?: Readonly<Record<string, string | number>>): boolean {
if (prefixFilter === undefined) {
return false;
}
if (Object.keys(prefixFilter).length === 0) {
return true;
}
const instanceKeys = Object.keys(this.prefixValues);
const filterKeys = Object.keys(prefixFilter);
if (instanceKeys.length !== filterKeys.length) {
return true;
}
for (const key of instanceKeys) {
if (this.prefixValues[key] !== prefixFilter[key]) {
return true;
}
}
return false;
}
private getHybridManager(): HybridSubscriptionManager<
JobStorageFormat<Input, Output>,
unknown,
QueueChangePayload<Input, Output>
> {
if (!this.hybridManager) {
const channelName = `indexeddb-queue-${this.tableName}-${this.queueName}`;
this.hybridManager = new HybridSubscriptionManager<
JobStorageFormat<Input, Output>,
unknown,
QueueChangePayload<Input, Output>
>(
channelName,
async () => {
const jobs = await this.getAllJobs();
return new Map(jobs.map((j) => [j.id, j]));
},
(a, b) => deepEqual(a, b),
{
insert: (item) => ({ type: "INSERT" as const, new: item }),
update: (oldItem, newItem) => ({ type: "UPDATE" as const, old: oldItem, new: newItem }),
delete: (item) => ({ type: "DELETE" as const, old: item }),
},
{
defaultIntervalMs: 1000,
useBroadcastChannel: this.hybridOptions.useBroadcastChannel,
backupPollingIntervalMs: this.hybridOptions.backupPollingIntervalMs,
}
);
}
return this.hybridManager;
}
private subscribeWithCustomPrefixFilter(
callback: (change: QueueChangePayload<Input, Output>) => void,
prefixFilter: Readonly<Record<string, string | number>>,
intervalMs: number
): () => void {
let lastKnownJobs = new Map<unknown, JobStorageFormat<Input, Output>>();
let cancelled = false;
const poll = async () => {
if (cancelled) return;
try {
const currentJobs = await this.getAllJobsWithFilter(prefixFilter);
if (cancelled) return;
const currentMap = new Map(currentJobs.map((j) => [j.id, j]));
for (const [id, job] of currentMap) {
const old = lastKnownJobs.get(id);
if (!old) {
callback({ type: "INSERT", new: job });
} else if (!deepEqual(old, job)) {
callback({ type: "UPDATE", old, new: job });
}
}
for (const [id, job] of lastKnownJobs) {
if (!currentMap.has(id)) {
callback({ type: "DELETE", old: job });
}
}
lastKnownJobs = currentMap;
} catch {
// ignore polling errors
}
};
const intervalId = setInterval(poll, intervalMs);
poll();
return () => {
cancelled = true;
clearInterval(intervalId);
};
}
/**
* Uses polling since IndexedDB has no native cross-tab change notifications.
* Normal subscriptions share a single polling loop; custom prefix filter
* subscriptions get a dedicated loop with DB-level filtering.
*/
public subscribeToChanges(
callback: (change: QueueChangePayload<Input, Output>) => void,
options?: QueueSubscribeOptions
): () => void {
const intervalMs = options?.pollingIntervalMs ?? 1000;
if (this.isCustomPrefixFilter(options?.prefixFilter)) {
return this.subscribeWithCustomPrefixFilter(callback, options!.prefixFilter!, intervalMs);
}
const manager = this.getHybridManager();
return manager.subscribe(callback, { intervalMs });
}
destroy(): void {
if (this.hybridManager) {
this.hybridManager.destroy();
this.hybridManager = null;
}
}
}