@nicnocquee/dataqueue 1.16.0 → 1.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +44 -42
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +32 -31
- package/dist/index.d.ts +32 -31
- package/dist/index.js +44 -42
- package/dist/index.js.map +1 -1
- package/package.json +4 -2
- package/src/index.test.ts +30 -30
- package/src/index.ts +1 -1
- package/src/processor.test.ts +24 -24
- package/src/processor.ts +5 -5
- package/src/queue.test.ts +74 -73
- package/src/queue.ts +43 -40
- package/src/types.ts +37 -30
- package/src/utils.ts +19 -0
package/src/types.ts
CHANGED
|
@@ -4,11 +4,11 @@ import { Pool } from 'pg';
|
|
|
4
4
|
export type JobType<PayloadMap> = keyof PayloadMap & string;
|
|
5
5
|
|
|
6
6
|
export interface JobOptions<PayloadMap, T extends JobType<PayloadMap>> {
|
|
7
|
-
|
|
7
|
+
jobType: T;
|
|
8
8
|
payload: PayloadMap[T];
|
|
9
|
-
|
|
9
|
+
maxAttempts?: number;
|
|
10
10
|
priority?: number;
|
|
11
|
-
|
|
11
|
+
runAt?: Date | null;
|
|
12
12
|
/**
|
|
13
13
|
* Timeout for this job in milliseconds. If not set, uses the processor default or unlimited.
|
|
14
14
|
*/
|
|
@@ -26,9 +26,9 @@ export enum JobEventType {
|
|
|
26
26
|
|
|
27
27
|
export interface JobEvent {
|
|
28
28
|
id: number;
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
29
|
+
jobId: number;
|
|
30
|
+
eventType: JobEventType;
|
|
31
|
+
createdAt: Date;
|
|
32
32
|
metadata: any;
|
|
33
33
|
}
|
|
34
34
|
|
|
@@ -38,50 +38,57 @@ export enum FailureReason {
|
|
|
38
38
|
NoHandler = 'no_handler',
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
+
export type JobStatus =
|
|
42
|
+
| 'pending'
|
|
43
|
+
| 'processing'
|
|
44
|
+
| 'completed'
|
|
45
|
+
| 'failed'
|
|
46
|
+
| 'cancelled';
|
|
47
|
+
|
|
41
48
|
export interface JobRecord<PayloadMap, T extends JobType<PayloadMap>> {
|
|
42
49
|
id: number;
|
|
43
|
-
|
|
50
|
+
jobType: T;
|
|
44
51
|
payload: PayloadMap[T];
|
|
45
|
-
status:
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
52
|
+
status: JobStatus;
|
|
53
|
+
createdAt: Date;
|
|
54
|
+
updatedAt: Date;
|
|
55
|
+
lockedAt: Date | null;
|
|
56
|
+
lockedBy: string | null;
|
|
50
57
|
attempts: number;
|
|
51
|
-
|
|
52
|
-
|
|
58
|
+
maxAttempts: number;
|
|
59
|
+
nextAttemptAt: Date | null;
|
|
53
60
|
priority: number;
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
61
|
+
runAt: Date;
|
|
62
|
+
pendingReason?: string | null;
|
|
63
|
+
errorHistory?: { message: string; timestamp: string }[];
|
|
57
64
|
/**
|
|
58
65
|
* Timeout for this job in milliseconds (null means no timeout).
|
|
59
66
|
*/
|
|
60
|
-
|
|
67
|
+
timeoutMs?: number | null;
|
|
61
68
|
/**
|
|
62
69
|
* The reason for the last failure, if any.
|
|
63
70
|
*/
|
|
64
|
-
|
|
71
|
+
failureReason?: FailureReason | null;
|
|
65
72
|
/**
|
|
66
73
|
* The time the job was completed, if completed.
|
|
67
74
|
*/
|
|
68
|
-
|
|
75
|
+
completedAt: Date | null;
|
|
69
76
|
/**
|
|
70
77
|
* The time the job was first picked up for processing.
|
|
71
78
|
*/
|
|
72
|
-
|
|
79
|
+
startedAt: Date | null;
|
|
73
80
|
/**
|
|
74
81
|
* The time the job was last retried.
|
|
75
82
|
*/
|
|
76
|
-
|
|
83
|
+
lastRetriedAt: Date | null;
|
|
77
84
|
/**
|
|
78
85
|
* The time the job last failed.
|
|
79
86
|
*/
|
|
80
|
-
|
|
87
|
+
lastFailedAt: Date | null;
|
|
81
88
|
/**
|
|
82
89
|
* The time the job was last cancelled.
|
|
83
90
|
*/
|
|
84
|
-
|
|
91
|
+
lastCancelledAt: Date | null;
|
|
85
92
|
}
|
|
86
93
|
|
|
87
94
|
export type JobHandler<PayloadMap, T extends keyof PayloadMap> = (
|
|
@@ -179,10 +186,10 @@ export interface JobQueue<PayloadMap> {
|
|
|
179
186
|
* Get jobs by their status, with pagination.
|
|
180
187
|
* - If no limit is provided, all jobs are returned.
|
|
181
188
|
* - If no offset is provided, the first page is returned.
|
|
182
|
-
* - The jobs are returned in descending order of
|
|
189
|
+
* - The jobs are returned in descending order of createdAt.
|
|
183
190
|
*/
|
|
184
191
|
getJobsByStatus: <T extends JobType<PayloadMap>>(
|
|
185
|
-
status:
|
|
192
|
+
status: JobStatus,
|
|
186
193
|
limit?: number,
|
|
187
194
|
offset?: number,
|
|
188
195
|
) => Promise<JobRecord<PayloadMap, T>[]>;
|
|
@@ -219,14 +226,14 @@ export interface JobQueue<PayloadMap> {
|
|
|
219
226
|
* - If no filters are provided, all upcoming jobs are cancelled.
|
|
220
227
|
* - If filters are provided, only jobs that match the filters are cancelled.
|
|
221
228
|
* - The filters are:
|
|
222
|
-
* -
|
|
229
|
+
* - jobType: The job type to cancel.
|
|
223
230
|
* - priority: The priority of the job to cancel.
|
|
224
|
-
* -
|
|
231
|
+
* - runAt: The time the job is scheduled to run at.
|
|
225
232
|
*/
|
|
226
233
|
cancelAllUpcomingJobs: (filters?: {
|
|
227
|
-
|
|
234
|
+
jobType?: string;
|
|
228
235
|
priority?: number;
|
|
229
|
-
|
|
236
|
+
runAt?: Date;
|
|
230
237
|
}) => Promise<number>;
|
|
231
238
|
/**
|
|
232
239
|
* Create a job processor. Handlers must be provided per-processor.
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export const toCamelCase = (str: string) => {
|
|
2
|
+
return str.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase());
|
|
3
|
+
};
|
|
4
|
+
|
|
5
|
+
export const toSnakeCase = (str: string) => {
|
|
6
|
+
return str.replace(/([A-Z])/g, '_$1').toLowerCase();
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export const objectKeysToCamelCase = (obj: Record<string, any>) => {
|
|
10
|
+
return Object.fromEntries(
|
|
11
|
+
Object.entries(obj).map(([key, value]) => [toCamelCase(key), value]),
|
|
12
|
+
);
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export const objectKeysToSnakeCase = (obj: Record<string, any>) => {
|
|
16
|
+
return Object.fromEntries(
|
|
17
|
+
Object.entries(obj).map(([key, value]) => [toSnakeCase(key), value]),
|
|
18
|
+
);
|
|
19
|
+
};
|