@queuebase/core 1.3.0 → 1.5.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 +844 -17
- package/dist/index.d.cts +98 -4
- package/dist/index.d.ts +98 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +837 -17
- package/dist/index.js.map +1 -1
- package/dist/router.d.ts +7 -1
- package/dist/router.d.ts.map +1 -1
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +2 -1
- package/dist/utils.js.map +1 -1
- package/dist/utils.test.js +7 -0
- package/dist/utils.test.js.map +1 -1
- package/package.json +3 -2
package/dist/index.d.cts
CHANGED
|
@@ -23,6 +23,39 @@ interface EnqueueOptions {
|
|
|
23
23
|
/** Maximum concurrent jobs of this type (per worker) */
|
|
24
24
|
concurrency?: number;
|
|
25
25
|
}
|
|
26
|
+
/**
|
|
27
|
+
* Overlap policy for scheduled jobs
|
|
28
|
+
*/
|
|
29
|
+
type OverlapPolicy = 'skip' | 'allow';
|
|
30
|
+
/**
|
|
31
|
+
* Full schedule configuration object
|
|
32
|
+
*/
|
|
33
|
+
interface ScheduleConfig {
|
|
34
|
+
/** Cron expression or plain english shorthand */
|
|
35
|
+
cron: string;
|
|
36
|
+
/** IANA timezone (default: 'UTC') */
|
|
37
|
+
timezone?: string;
|
|
38
|
+
/** Whether this schedule is active in code (default: true) */
|
|
39
|
+
enabled?: boolean;
|
|
40
|
+
/** Overlap policy when previous run is still running (default: 'skip') */
|
|
41
|
+
overlap?: OverlapPolicy;
|
|
42
|
+
/** Timeout for scheduled runs (e.g., '5m', '30s', or milliseconds) */
|
|
43
|
+
timeout?: string | number;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Schedule can be a plain english/cron string shorthand or a full config object
|
|
47
|
+
*/
|
|
48
|
+
type ScheduleInput = string | ScheduleConfig;
|
|
49
|
+
/**
|
|
50
|
+
* Resolved schedule metadata after parsing — always has a standard cron expression
|
|
51
|
+
*/
|
|
52
|
+
interface ResolvedSchedule {
|
|
53
|
+
cronExpression: string;
|
|
54
|
+
timezone: string;
|
|
55
|
+
enabled: boolean;
|
|
56
|
+
overlap: OverlapPolicy;
|
|
57
|
+
timeoutMs: number | null;
|
|
58
|
+
}
|
|
26
59
|
/**
|
|
27
60
|
* A job definition with typed input/output
|
|
28
61
|
*/
|
|
@@ -33,6 +66,8 @@ interface JobDefinition<TInput = unknown, TOutput = unknown> {
|
|
|
33
66
|
handler: (ctx: JobContext<TInput>) => Promise<TOutput>;
|
|
34
67
|
/** Default options for this job */
|
|
35
68
|
defaults?: EnqueueOptions;
|
|
69
|
+
/** Cron schedule — only valid when input accepts empty object */
|
|
70
|
+
schedule?: ScheduleInput;
|
|
36
71
|
}
|
|
37
72
|
/**
|
|
38
73
|
* Context passed to job handlers
|
|
@@ -46,6 +81,8 @@ interface JobContext<TInput = unknown> {
|
|
|
46
81
|
attempt: number;
|
|
47
82
|
/** Maximum attempts allowed */
|
|
48
83
|
maxAttempts: number;
|
|
84
|
+
/** Explicitly mark this job as failed */
|
|
85
|
+
fail: (reason: string) => never;
|
|
49
86
|
}
|
|
50
87
|
/**
|
|
51
88
|
* Internal representation of a queued job
|
|
@@ -119,6 +156,7 @@ interface AnyJobDefinition {
|
|
|
119
156
|
input: z.ZodTypeAny;
|
|
120
157
|
handler: (ctx: JobContext<unknown>) => Promise<unknown>;
|
|
121
158
|
defaults?: EnqueueOptions;
|
|
159
|
+
schedule?: ScheduleInput;
|
|
122
160
|
}
|
|
123
161
|
/**
|
|
124
162
|
* A collection of job definitions
|
|
@@ -159,13 +197,28 @@ type JobClient<T extends Record<string, AnyJobDefinition>> = CallableJobRouter<T
|
|
|
159
197
|
};
|
|
160
198
|
|
|
161
199
|
/**
|
|
162
|
-
*
|
|
200
|
+
* A job definition that accepts empty input — schedule is allowed.
|
|
163
201
|
*/
|
|
164
|
-
|
|
202
|
+
interface SchedulableJobConfig<TInput extends Record<string, never>, TOutput> {
|
|
165
203
|
input: z.ZodType<TInput>;
|
|
166
204
|
handler: (ctx: JobContext<TInput>) => Promise<TOutput>;
|
|
167
205
|
defaults?: EnqueueOptions;
|
|
168
|
-
|
|
206
|
+
schedule?: ScheduleInput;
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* A job definition with non-empty input — schedule is not allowed.
|
|
210
|
+
*/
|
|
211
|
+
interface NonSchedulableJobConfig<TInput, TOutput> {
|
|
212
|
+
input: z.ZodType<TInput>;
|
|
213
|
+
handler: (ctx: JobContext<TInput>) => Promise<TOutput>;
|
|
214
|
+
defaults?: EnqueueOptions;
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Creates a job definition with typed input and handler.
|
|
218
|
+
* The `schedule` property is only available when the input schema accepts `{}`.
|
|
219
|
+
*/
|
|
220
|
+
declare function job<TInput extends Record<string, never>, TOutput>(definition: SchedulableJobConfig<TInput, TOutput>): AnyJobDefinition;
|
|
221
|
+
declare function job<TInput, TOutput>(definition: NonSchedulableJobConfig<TInput, TOutput>): AnyJobDefinition;
|
|
169
222
|
/**
|
|
170
223
|
* Infer the input type from a job definition's Zod schema
|
|
171
224
|
*/
|
|
@@ -175,6 +228,44 @@ type InferJobInput<T extends AnyJobDefinition> = T['input'] extends z.ZodType<in
|
|
|
175
228
|
*/
|
|
176
229
|
type InferJobOutput<T extends AnyJobDefinition> = T['handler'] extends (ctx: JobContext<unknown>) => Promise<infer O> ? O : never;
|
|
177
230
|
|
|
231
|
+
/**
|
|
232
|
+
* Parse a plain english expression to a cron string.
|
|
233
|
+
* Returns the input unchanged if no pattern matches (assumed to be raw cron).
|
|
234
|
+
*/
|
|
235
|
+
declare function parsePlainEnglish(expression: string): string;
|
|
236
|
+
/**
|
|
237
|
+
* Validate a cron expression using croner.
|
|
238
|
+
* Throws if the expression is invalid.
|
|
239
|
+
*/
|
|
240
|
+
declare function validateCron(expression: string): void;
|
|
241
|
+
/**
|
|
242
|
+
* Compute the next run time for a cron expression with optional timezone.
|
|
243
|
+
*/
|
|
244
|
+
declare function nextRun(expression: string, timezone?: string): Date | null;
|
|
245
|
+
/**
|
|
246
|
+
* Compute multiple next run times for a cron expression.
|
|
247
|
+
*/
|
|
248
|
+
declare function nextRuns(expression: string, count: number, timezone?: string): Date[];
|
|
249
|
+
/**
|
|
250
|
+
* Resolve a schedule input into a fully resolved schedule.
|
|
251
|
+
* Parses plain english, validates the cron expression, and resolves defaults.
|
|
252
|
+
*/
|
|
253
|
+
declare function resolveSchedule(input: ScheduleInput): ResolvedSchedule;
|
|
254
|
+
/**
|
|
255
|
+
* Metadata for a single schedule extracted from a job router.
|
|
256
|
+
*/
|
|
257
|
+
interface ScheduleMetadata {
|
|
258
|
+
jobName: string;
|
|
259
|
+
schedule: ResolvedSchedule;
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Extract all schedule metadata from a job router.
|
|
263
|
+
* Only includes jobs that have a schedule defined.
|
|
264
|
+
*/
|
|
265
|
+
declare function extractSchedules(router: Record<string, {
|
|
266
|
+
schedule?: ScheduleInput;
|
|
267
|
+
}>): ScheduleMetadata[];
|
|
268
|
+
|
|
178
269
|
/**
|
|
179
270
|
* Parse a delay string into milliseconds
|
|
180
271
|
* Supports: '5s', '5m', '5h', '5d' or raw milliseconds
|
|
@@ -213,6 +304,9 @@ declare function signPayload(payload: string, secret: string): {
|
|
|
213
304
|
*/
|
|
214
305
|
declare function verifySignature(payload: string, signature: string, secret: string, toleranceSeconds?: number): boolean;
|
|
215
306
|
|
|
307
|
+
declare class JobFailureError extends Error {
|
|
308
|
+
constructor(reason: string);
|
|
309
|
+
}
|
|
216
310
|
interface HandlerRequest {
|
|
217
311
|
body: string;
|
|
218
312
|
signatureHeader: string | null;
|
|
@@ -225,4 +319,4 @@ declare function processJobCallback(router: Record<string, AnyJobDefinition>, re
|
|
|
225
319
|
webhookSecret?: string;
|
|
226
320
|
}): Promise<HandlerResponse>;
|
|
227
321
|
|
|
228
|
-
export { type AnyJobDefinition, type BackoffStrategy, type CallableJob, type CallableJobRouter, type EnqueueOptions, type EnqueueResult, type HandlerRequest, type HandlerResponse, type InferJobInput, type InferJobOutput, type JobClient, type JobContext, type JobDefinition, type JobResult, type JobRouter, type JobStatus, type JobStatusResponse, type QueuebaseConfig, type QueuedJob, WEBHOOK_HEADERS, calculateBackoff, createJobRouter, generateJobId, generatePublicId, job, parseDelay, processJobCallback, signPayload, verifySignature };
|
|
322
|
+
export { type AnyJobDefinition, type BackoffStrategy, type CallableJob, type CallableJobRouter, type EnqueueOptions, type EnqueueResult, type HandlerRequest, type HandlerResponse, type InferJobInput, type InferJobOutput, type JobClient, type JobContext, type JobDefinition, JobFailureError, type JobResult, type JobRouter, type JobStatus, type JobStatusResponse, type OverlapPolicy, type QueuebaseConfig, type QueuedJob, type ResolvedSchedule, type ScheduleConfig, type ScheduleInput, type ScheduleMetadata, WEBHOOK_HEADERS, calculateBackoff, createJobRouter, extractSchedules, generateJobId, generatePublicId, job, nextRun, nextRuns, parseDelay, parsePlainEnglish, processJobCallback, resolveSchedule, signPayload, validateCron, verifySignature };
|
package/dist/index.d.ts
CHANGED
|
@@ -23,6 +23,39 @@ interface EnqueueOptions {
|
|
|
23
23
|
/** Maximum concurrent jobs of this type (per worker) */
|
|
24
24
|
concurrency?: number;
|
|
25
25
|
}
|
|
26
|
+
/**
|
|
27
|
+
* Overlap policy for scheduled jobs
|
|
28
|
+
*/
|
|
29
|
+
type OverlapPolicy = 'skip' | 'allow';
|
|
30
|
+
/**
|
|
31
|
+
* Full schedule configuration object
|
|
32
|
+
*/
|
|
33
|
+
interface ScheduleConfig {
|
|
34
|
+
/** Cron expression or plain english shorthand */
|
|
35
|
+
cron: string;
|
|
36
|
+
/** IANA timezone (default: 'UTC') */
|
|
37
|
+
timezone?: string;
|
|
38
|
+
/** Whether this schedule is active in code (default: true) */
|
|
39
|
+
enabled?: boolean;
|
|
40
|
+
/** Overlap policy when previous run is still running (default: 'skip') */
|
|
41
|
+
overlap?: OverlapPolicy;
|
|
42
|
+
/** Timeout for scheduled runs (e.g., '5m', '30s', or milliseconds) */
|
|
43
|
+
timeout?: string | number;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Schedule can be a plain english/cron string shorthand or a full config object
|
|
47
|
+
*/
|
|
48
|
+
type ScheduleInput = string | ScheduleConfig;
|
|
49
|
+
/**
|
|
50
|
+
* Resolved schedule metadata after parsing — always has a standard cron expression
|
|
51
|
+
*/
|
|
52
|
+
interface ResolvedSchedule {
|
|
53
|
+
cronExpression: string;
|
|
54
|
+
timezone: string;
|
|
55
|
+
enabled: boolean;
|
|
56
|
+
overlap: OverlapPolicy;
|
|
57
|
+
timeoutMs: number | null;
|
|
58
|
+
}
|
|
26
59
|
/**
|
|
27
60
|
* A job definition with typed input/output
|
|
28
61
|
*/
|
|
@@ -33,6 +66,8 @@ interface JobDefinition<TInput = unknown, TOutput = unknown> {
|
|
|
33
66
|
handler: (ctx: JobContext<TInput>) => Promise<TOutput>;
|
|
34
67
|
/** Default options for this job */
|
|
35
68
|
defaults?: EnqueueOptions;
|
|
69
|
+
/** Cron schedule — only valid when input accepts empty object */
|
|
70
|
+
schedule?: ScheduleInput;
|
|
36
71
|
}
|
|
37
72
|
/**
|
|
38
73
|
* Context passed to job handlers
|
|
@@ -46,6 +81,8 @@ interface JobContext<TInput = unknown> {
|
|
|
46
81
|
attempt: number;
|
|
47
82
|
/** Maximum attempts allowed */
|
|
48
83
|
maxAttempts: number;
|
|
84
|
+
/** Explicitly mark this job as failed */
|
|
85
|
+
fail: (reason: string) => never;
|
|
49
86
|
}
|
|
50
87
|
/**
|
|
51
88
|
* Internal representation of a queued job
|
|
@@ -119,6 +156,7 @@ interface AnyJobDefinition {
|
|
|
119
156
|
input: z.ZodTypeAny;
|
|
120
157
|
handler: (ctx: JobContext<unknown>) => Promise<unknown>;
|
|
121
158
|
defaults?: EnqueueOptions;
|
|
159
|
+
schedule?: ScheduleInput;
|
|
122
160
|
}
|
|
123
161
|
/**
|
|
124
162
|
* A collection of job definitions
|
|
@@ -159,13 +197,28 @@ type JobClient<T extends Record<string, AnyJobDefinition>> = CallableJobRouter<T
|
|
|
159
197
|
};
|
|
160
198
|
|
|
161
199
|
/**
|
|
162
|
-
*
|
|
200
|
+
* A job definition that accepts empty input — schedule is allowed.
|
|
163
201
|
*/
|
|
164
|
-
|
|
202
|
+
interface SchedulableJobConfig<TInput extends Record<string, never>, TOutput> {
|
|
165
203
|
input: z.ZodType<TInput>;
|
|
166
204
|
handler: (ctx: JobContext<TInput>) => Promise<TOutput>;
|
|
167
205
|
defaults?: EnqueueOptions;
|
|
168
|
-
|
|
206
|
+
schedule?: ScheduleInput;
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* A job definition with non-empty input — schedule is not allowed.
|
|
210
|
+
*/
|
|
211
|
+
interface NonSchedulableJobConfig<TInput, TOutput> {
|
|
212
|
+
input: z.ZodType<TInput>;
|
|
213
|
+
handler: (ctx: JobContext<TInput>) => Promise<TOutput>;
|
|
214
|
+
defaults?: EnqueueOptions;
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Creates a job definition with typed input and handler.
|
|
218
|
+
* The `schedule` property is only available when the input schema accepts `{}`.
|
|
219
|
+
*/
|
|
220
|
+
declare function job<TInput extends Record<string, never>, TOutput>(definition: SchedulableJobConfig<TInput, TOutput>): AnyJobDefinition;
|
|
221
|
+
declare function job<TInput, TOutput>(definition: NonSchedulableJobConfig<TInput, TOutput>): AnyJobDefinition;
|
|
169
222
|
/**
|
|
170
223
|
* Infer the input type from a job definition's Zod schema
|
|
171
224
|
*/
|
|
@@ -175,6 +228,44 @@ type InferJobInput<T extends AnyJobDefinition> = T['input'] extends z.ZodType<in
|
|
|
175
228
|
*/
|
|
176
229
|
type InferJobOutput<T extends AnyJobDefinition> = T['handler'] extends (ctx: JobContext<unknown>) => Promise<infer O> ? O : never;
|
|
177
230
|
|
|
231
|
+
/**
|
|
232
|
+
* Parse a plain english expression to a cron string.
|
|
233
|
+
* Returns the input unchanged if no pattern matches (assumed to be raw cron).
|
|
234
|
+
*/
|
|
235
|
+
declare function parsePlainEnglish(expression: string): string;
|
|
236
|
+
/**
|
|
237
|
+
* Validate a cron expression using croner.
|
|
238
|
+
* Throws if the expression is invalid.
|
|
239
|
+
*/
|
|
240
|
+
declare function validateCron(expression: string): void;
|
|
241
|
+
/**
|
|
242
|
+
* Compute the next run time for a cron expression with optional timezone.
|
|
243
|
+
*/
|
|
244
|
+
declare function nextRun(expression: string, timezone?: string): Date | null;
|
|
245
|
+
/**
|
|
246
|
+
* Compute multiple next run times for a cron expression.
|
|
247
|
+
*/
|
|
248
|
+
declare function nextRuns(expression: string, count: number, timezone?: string): Date[];
|
|
249
|
+
/**
|
|
250
|
+
* Resolve a schedule input into a fully resolved schedule.
|
|
251
|
+
* Parses plain english, validates the cron expression, and resolves defaults.
|
|
252
|
+
*/
|
|
253
|
+
declare function resolveSchedule(input: ScheduleInput): ResolvedSchedule;
|
|
254
|
+
/**
|
|
255
|
+
* Metadata for a single schedule extracted from a job router.
|
|
256
|
+
*/
|
|
257
|
+
interface ScheduleMetadata {
|
|
258
|
+
jobName: string;
|
|
259
|
+
schedule: ResolvedSchedule;
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Extract all schedule metadata from a job router.
|
|
263
|
+
* Only includes jobs that have a schedule defined.
|
|
264
|
+
*/
|
|
265
|
+
declare function extractSchedules(router: Record<string, {
|
|
266
|
+
schedule?: ScheduleInput;
|
|
267
|
+
}>): ScheduleMetadata[];
|
|
268
|
+
|
|
178
269
|
/**
|
|
179
270
|
* Parse a delay string into milliseconds
|
|
180
271
|
* Supports: '5s', '5m', '5h', '5d' or raw milliseconds
|
|
@@ -213,6 +304,9 @@ declare function signPayload(payload: string, secret: string): {
|
|
|
213
304
|
*/
|
|
214
305
|
declare function verifySignature(payload: string, signature: string, secret: string, toleranceSeconds?: number): boolean;
|
|
215
306
|
|
|
307
|
+
declare class JobFailureError extends Error {
|
|
308
|
+
constructor(reason: string);
|
|
309
|
+
}
|
|
216
310
|
interface HandlerRequest {
|
|
217
311
|
body: string;
|
|
218
312
|
signatureHeader: string | null;
|
|
@@ -225,4 +319,4 @@ declare function processJobCallback(router: Record<string, AnyJobDefinition>, re
|
|
|
225
319
|
webhookSecret?: string;
|
|
226
320
|
}): Promise<HandlerResponse>;
|
|
227
321
|
|
|
228
|
-
export { type AnyJobDefinition, type BackoffStrategy, type CallableJob, type CallableJobRouter, type EnqueueOptions, type EnqueueResult, type HandlerRequest, type HandlerResponse, type InferJobInput, type InferJobOutput, type JobClient, type JobContext, type JobDefinition, type JobResult, type JobRouter, type JobStatus, type JobStatusResponse, type QueuebaseConfig, type QueuedJob, WEBHOOK_HEADERS, calculateBackoff, createJobRouter, generateJobId, generatePublicId, job, parseDelay, processJobCallback, signPayload, verifySignature };
|
|
322
|
+
export { type AnyJobDefinition, type BackoffStrategy, type CallableJob, type CallableJobRouter, type EnqueueOptions, type EnqueueResult, type HandlerRequest, type HandlerResponse, type InferJobInput, type InferJobOutput, type JobClient, type JobContext, type JobDefinition, JobFailureError, type JobResult, type JobRouter, type JobStatus, type JobStatusResponse, type OverlapPolicy, type QueuebaseConfig, type QueuedJob, type ResolvedSchedule, type ScheduleConfig, type ScheduleInput, type ScheduleMetadata, WEBHOOK_HEADERS, calculateBackoff, createJobRouter, extractSchedules, generateJobId, generatePublicId, job, nextRun, nextRuns, parseDelay, parsePlainEnglish, processJobCallback, resolveSchedule, signPayload, validateCron, verifySignature };
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,YAAY,EACV,SAAS,EACT,eAAe,EACf,cAAc,EACd,aAAa,EACb,UAAU,EACV,SAAS,EACT,SAAS,EACT,iBAAiB,EACjB,aAAa,EACb,eAAe,GAChB,MAAM,YAAY,CAAC;AAGpB,OAAO,EAAE,GAAG,EAAE,KAAK,aAAa,EAAE,KAAK,cAAc,EAAE,MAAM,UAAU,CAAC;AAGxE,OAAO,EACL,eAAe,EACf,KAAK,gBAAgB,EACrB,KAAK,SAAS,EACd,KAAK,WAAW,EAChB,KAAK,iBAAiB,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,YAAY,EACV,SAAS,EACT,eAAe,EACf,cAAc,EACd,aAAa,EACb,UAAU,EACV,SAAS,EACT,SAAS,EACT,iBAAiB,EACjB,aAAa,EACb,eAAe,GAChB,MAAM,YAAY,CAAC;AAGpB,OAAO,EAAE,GAAG,EAAE,KAAK,aAAa,EAAE,KAAK,cAAc,EAAE,MAAM,UAAU,CAAC;AAGxE,OAAO,EACL,eAAe,EACf,KAAK,gBAAgB,EACrB,KAAK,SAAS,EACd,KAAK,WAAW,EAChB,KAAK,iBAAiB,EACtB,KAAK,SAAS,GACf,MAAM,aAAa,CAAC;AAGrB,OAAO,EAAE,UAAU,EAAE,gBAAgB,EAAE,aAAa,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAG3F,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAG7E,OAAO,EACL,kBAAkB,EAClB,KAAK,cAAc,EACnB,KAAK,eAAe,GACrB,MAAM,cAAc,CAAC"}
|