@monque/core 1.0.0 → 1.1.1

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.
Files changed (45) hide show
  1. package/LICENSE +15 -0
  2. package/README.md +20 -0
  3. package/dist/CHANGELOG.md +83 -0
  4. package/dist/LICENSE +15 -0
  5. package/dist/README.md +150 -0
  6. package/dist/index.cjs +2209 -942
  7. package/dist/index.cjs.map +1 -1
  8. package/dist/index.d.cts +537 -280
  9. package/dist/index.d.cts.map +1 -1
  10. package/dist/index.d.mts +538 -281
  11. package/dist/index.d.mts.map +1 -1
  12. package/dist/index.mjs +2213 -951
  13. package/dist/index.mjs.map +1 -1
  14. package/package.json +9 -8
  15. package/src/events/index.ts +1 -0
  16. package/src/events/types.ts +113 -0
  17. package/src/index.ts +51 -0
  18. package/src/jobs/guards.ts +220 -0
  19. package/src/jobs/index.ts +29 -0
  20. package/src/jobs/types.ts +335 -0
  21. package/src/reset.d.ts +1 -0
  22. package/src/scheduler/helpers.ts +107 -0
  23. package/src/scheduler/index.ts +5 -0
  24. package/src/scheduler/monque.ts +1309 -0
  25. package/src/scheduler/services/change-stream-handler.ts +239 -0
  26. package/src/scheduler/services/index.ts +8 -0
  27. package/src/scheduler/services/job-manager.ts +455 -0
  28. package/src/scheduler/services/job-processor.ts +301 -0
  29. package/src/scheduler/services/job-query.ts +411 -0
  30. package/src/scheduler/services/job-scheduler.ts +267 -0
  31. package/src/scheduler/services/types.ts +48 -0
  32. package/src/scheduler/types.ts +123 -0
  33. package/src/shared/errors.ts +225 -0
  34. package/src/shared/index.ts +18 -0
  35. package/src/shared/utils/backoff.ts +77 -0
  36. package/src/shared/utils/cron.ts +67 -0
  37. package/src/shared/utils/index.ts +7 -0
  38. package/src/workers/index.ts +1 -0
  39. package/src/workers/types.ts +39 -0
  40. package/dist/errors-D5ZGG2uI.cjs +0 -155
  41. package/dist/errors-D5ZGG2uI.cjs.map +0 -1
  42. package/dist/errors-DEvnqoOC.mjs +0 -3
  43. package/dist/errors-DQ2_gprw.mjs +0 -125
  44. package/dist/errors-DQ2_gprw.mjs.map +0 -1
  45. package/dist/errors-Dfli-u59.cjs +0 -3
@@ -0,0 +1,77 @@
1
+ /**
2
+ * Default base interval for exponential backoff in milliseconds.
3
+ * @default 1000
4
+ */
5
+ export const DEFAULT_BASE_INTERVAL = 1000;
6
+
7
+ /**
8
+ * Default maximum delay cap for exponential backoff in milliseconds.
9
+ *
10
+ * This prevents unbounded delays (e.g. failCount=20 is >11 days at 1s base)
11
+ * and avoids precision/overflow issues for very large fail counts.
12
+ * @default 86400000 (24 hours)
13
+ */
14
+ export const DEFAULT_MAX_BACKOFF_DELAY = 24 * 60 * 60 * 1_000;
15
+
16
+ /**
17
+ * Calculate the next run time using exponential backoff.
18
+ *
19
+ * Formula: nextRunAt = now + (2^failCount × baseInterval)
20
+ *
21
+ * @param failCount - Number of previous failed attempts
22
+ * @param baseInterval - Base interval in milliseconds (default: 1000ms)
23
+ * @param maxDelay - Maximum delay in milliseconds (optional)
24
+ * @returns The next run date
25
+ *
26
+ * @example
27
+ * ```typescript
28
+ * // First retry (failCount=1): 2^1 * 1000 = 2000ms delay
29
+ * const nextRun = calculateBackoff(1);
30
+ *
31
+ * // Second retry (failCount=2): 2^2 * 1000 = 4000ms delay
32
+ * const nextRun = calculateBackoff(2);
33
+ *
34
+ * // With custom base interval
35
+ * const nextRun = calculateBackoff(3, 500); // 2^3 * 500 = 4000ms delay
36
+ *
37
+ * // With max delay
38
+ * const nextRun = calculateBackoff(10, 1000, 60000); // capped at 60000ms
39
+ * ```
40
+ */
41
+ export function calculateBackoff(
42
+ failCount: number,
43
+ baseInterval: number = DEFAULT_BASE_INTERVAL,
44
+ maxDelay?: number,
45
+ ): Date {
46
+ const effectiveMaxDelay = maxDelay ?? DEFAULT_MAX_BACKOFF_DELAY;
47
+ let delay = 2 ** failCount * baseInterval;
48
+
49
+ if (delay > effectiveMaxDelay) {
50
+ delay = effectiveMaxDelay;
51
+ }
52
+
53
+ return new Date(Date.now() + delay);
54
+ }
55
+
56
+ /**
57
+ * Calculate just the delay in milliseconds for a given fail count.
58
+ *
59
+ * @param failCount - Number of previous failed attempts
60
+ * @param baseInterval - Base interval in milliseconds (default: 1000ms)
61
+ * @param maxDelay - Maximum delay in milliseconds (optional)
62
+ * @returns The delay in milliseconds
63
+ */
64
+ export function calculateBackoffDelay(
65
+ failCount: number,
66
+ baseInterval: number = DEFAULT_BASE_INTERVAL,
67
+ maxDelay?: number,
68
+ ): number {
69
+ const effectiveMaxDelay = maxDelay ?? DEFAULT_MAX_BACKOFF_DELAY;
70
+ let delay = 2 ** failCount * baseInterval;
71
+
72
+ if (delay > effectiveMaxDelay) {
73
+ delay = effectiveMaxDelay;
74
+ }
75
+
76
+ return delay;
77
+ }
@@ -0,0 +1,67 @@
1
+ import { CronExpressionParser } from 'cron-parser';
2
+
3
+ import { InvalidCronError } from '../errors.js';
4
+
5
+ /**
6
+ * Parse a cron expression and return the next scheduled run date.
7
+ *
8
+ * @param expression - A 5-field cron expression (minute hour day-of-month month day-of-week) or a predefined expression
9
+ * @param currentDate - The reference date for calculating next run (default: now)
10
+ * @returns The next scheduled run date
11
+ * @throws {InvalidCronError} If the cron expression is invalid
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * // Every minute
16
+ * const nextRun = getNextCronDate('* * * * *');
17
+ *
18
+ * // Every day at midnight
19
+ * const nextRun = getNextCronDate('0 0 * * *');
20
+ *
21
+ * // Using predefined expression
22
+ * const nextRun = getNextCronDate('@daily');
23
+ *
24
+ * // Every Monday at 9am
25
+ * const nextRun = getNextCronDate('0 9 * * 1');
26
+ * ```
27
+ */
28
+ export function getNextCronDate(expression: string, currentDate?: Date): Date {
29
+ try {
30
+ const interval = CronExpressionParser.parse(expression, {
31
+ currentDate: currentDate ?? new Date(),
32
+ });
33
+ return interval.next().toDate();
34
+ } catch (error) {
35
+ handleCronParseError(expression, error);
36
+ }
37
+ }
38
+
39
+ /**
40
+ * Validate a cron expression without calculating the next run date.
41
+ *
42
+ * @param expression - A 5-field cron expression
43
+ * @throws {InvalidCronError} If the cron expression is invalid
44
+ *
45
+ * @example
46
+ * ```typescript
47
+ * validateCronExpression('0 9 * * 1'); // Throws if invalid
48
+ * ```
49
+ */
50
+ export function validateCronExpression(expression: string): void {
51
+ try {
52
+ CronExpressionParser.parse(expression);
53
+ } catch (error) {
54
+ handleCronParseError(expression, error);
55
+ }
56
+ }
57
+
58
+ function handleCronParseError(expression: string, error: unknown): never {
59
+ /* istanbul ignore next -- @preserve cron-parser always throws Error objects */
60
+ const errorMessage = error instanceof Error ? error.message : 'Unknown parsing error';
61
+ throw new InvalidCronError(
62
+ expression,
63
+ `Invalid cron expression "${expression}": ${errorMessage}. ` +
64
+ 'Expected 5-field format: "minute hour day-of-month month day-of-week" or predefined expression (e.g. @daily). ' +
65
+ 'Example: "0 9 * * 1" (every Monday at 9am)',
66
+ );
67
+ }
@@ -0,0 +1,7 @@
1
+ export {
2
+ calculateBackoff,
3
+ calculateBackoffDelay,
4
+ DEFAULT_BASE_INTERVAL,
5
+ DEFAULT_MAX_BACKOFF_DELAY,
6
+ } from './backoff.js';
7
+ export { getNextCronDate, validateCronExpression } from './cron.js';
@@ -0,0 +1 @@
1
+ export type { WorkerOptions, WorkerRegistration } from './types.js';
@@ -0,0 +1,39 @@
1
+ import type { JobHandler, PersistedJob } from '@/jobs';
2
+
3
+ /**
4
+ * Options for registering a worker.
5
+ *
6
+ * @example
7
+ * ```typescript
8
+ * monque.register('send-email', emailHandler, {
9
+ * concurrency: 3,
10
+ * });
11
+ * ```
12
+ */
13
+ export interface WorkerOptions {
14
+ /**
15
+ * Number of concurrent jobs this worker can process.
16
+ * @default 5 (uses defaultConcurrency from MonqueOptions)
17
+ */
18
+ concurrency?: number;
19
+
20
+ /**
21
+ * Allow replacing an existing worker for the same job name.
22
+ * If false (default) and a worker already exists, throws WorkerRegistrationError.
23
+ * @default false
24
+ */
25
+ replace?: boolean;
26
+ }
27
+
28
+ /**
29
+ * Internal worker registration with handler and options.
30
+ * Tracks the handler, concurrency limit, and currently active jobs.
31
+ */
32
+ export interface WorkerRegistration<T = unknown> {
33
+ /** The job handler function */
34
+ handler: JobHandler<T>;
35
+ /** Maximum concurrent jobs for this worker */
36
+ concurrency: number;
37
+ /** Map of active job IDs to their job data */
38
+ activeJobs: Map<string, PersistedJob<T>>;
39
+ }
@@ -1,155 +0,0 @@
1
-
2
- //#region src/shared/errors.ts
3
- /**
4
- * Base error class for all Monque-related errors.
5
- *
6
- * @example
7
- * ```typescript
8
- * try {
9
- * await monque.enqueue('job', data);
10
- * } catch (error) {
11
- * if (error instanceof MonqueError) {
12
- * console.error('Monque error:', error.message);
13
- * }
14
- * }
15
- * ```
16
- */
17
- var MonqueError = class MonqueError extends Error {
18
- constructor(message) {
19
- super(message);
20
- this.name = "MonqueError";
21
- /* istanbul ignore next -- @preserve captureStackTrace is always available in Node.js */
22
- if (Error.captureStackTrace) Error.captureStackTrace(this, MonqueError);
23
- }
24
- };
25
- /**
26
- * Error thrown when an invalid cron expression is provided.
27
- *
28
- * @example
29
- * ```typescript
30
- * try {
31
- * await monque.schedule('invalid cron', 'job', data);
32
- * } catch (error) {
33
- * if (error instanceof InvalidCronError) {
34
- * console.error('Invalid expression:', error.expression);
35
- * }
36
- * }
37
- * ```
38
- */
39
- var InvalidCronError = class InvalidCronError extends MonqueError {
40
- constructor(expression, message) {
41
- super(message);
42
- this.expression = expression;
43
- this.name = "InvalidCronError";
44
- /* istanbul ignore next -- @preserve captureStackTrace is always available in Node.js */
45
- if (Error.captureStackTrace) Error.captureStackTrace(this, InvalidCronError);
46
- }
47
- };
48
- /**
49
- * Error thrown when there's a database connection issue.
50
- *
51
- * @example
52
- * ```typescript
53
- * try {
54
- * await monque.enqueue('job', data);
55
- * } catch (error) {
56
- * if (error instanceof ConnectionError) {
57
- * console.error('Database connection lost');
58
- * }
59
- * }
60
- * ```
61
- */
62
- var ConnectionError = class ConnectionError extends MonqueError {
63
- constructor(message, options) {
64
- super(message);
65
- this.name = "ConnectionError";
66
- if (options?.cause) this.cause = options.cause;
67
- /* istanbul ignore next -- @preserve captureStackTrace is always available in Node.js */
68
- if (Error.captureStackTrace) Error.captureStackTrace(this, ConnectionError);
69
- }
70
- };
71
- /**
72
- * Error thrown when graceful shutdown times out.
73
- * Includes information about jobs that were still in progress.
74
- *
75
- * @example
76
- * ```typescript
77
- * try {
78
- * await monque.stop();
79
- * } catch (error) {
80
- * if (error instanceof ShutdownTimeoutError) {
81
- * console.error('Incomplete jobs:', error.incompleteJobs.length);
82
- * }
83
- * }
84
- * ```
85
- */
86
- var ShutdownTimeoutError = class ShutdownTimeoutError extends MonqueError {
87
- constructor(message, incompleteJobs) {
88
- super(message);
89
- this.incompleteJobs = incompleteJobs;
90
- this.name = "ShutdownTimeoutError";
91
- /* istanbul ignore next -- @preserve captureStackTrace is always available in Node.js */
92
- if (Error.captureStackTrace) Error.captureStackTrace(this, ShutdownTimeoutError);
93
- }
94
- };
95
- /**
96
- * Error thrown when attempting to register a worker for a job name
97
- * that already has a registered worker, without explicitly allowing replacement.
98
- *
99
- * @example
100
- * ```typescript
101
- * try {
102
- * monque.register('send-email', handler1);
103
- * monque.register('send-email', handler2); // throws
104
- * } catch (error) {
105
- * if (error instanceof WorkerRegistrationError) {
106
- * console.error('Worker already registered for:', error.jobName);
107
- * }
108
- * }
109
- *
110
- * // To intentionally replace a worker:
111
- * monque.register('send-email', handler2, { replace: true });
112
- * ```
113
- */
114
- var WorkerRegistrationError = class WorkerRegistrationError extends MonqueError {
115
- constructor(message, jobName) {
116
- super(message);
117
- this.jobName = jobName;
118
- this.name = "WorkerRegistrationError";
119
- /* istanbul ignore next -- @preserve captureStackTrace is always available in Node.js */
120
- if (Error.captureStackTrace) Error.captureStackTrace(this, WorkerRegistrationError);
121
- }
122
- };
123
-
124
- //#endregion
125
- Object.defineProperty(exports, 'ConnectionError', {
126
- enumerable: true,
127
- get: function () {
128
- return ConnectionError;
129
- }
130
- });
131
- Object.defineProperty(exports, 'InvalidCronError', {
132
- enumerable: true,
133
- get: function () {
134
- return InvalidCronError;
135
- }
136
- });
137
- Object.defineProperty(exports, 'MonqueError', {
138
- enumerable: true,
139
- get: function () {
140
- return MonqueError;
141
- }
142
- });
143
- Object.defineProperty(exports, 'ShutdownTimeoutError', {
144
- enumerable: true,
145
- get: function () {
146
- return ShutdownTimeoutError;
147
- }
148
- });
149
- Object.defineProperty(exports, 'WorkerRegistrationError', {
150
- enumerable: true,
151
- get: function () {
152
- return WorkerRegistrationError;
153
- }
154
- });
155
- //# sourceMappingURL=errors-D5ZGG2uI.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"errors-D5ZGG2uI.cjs","names":[],"sources":["../src/shared/errors.ts"],"sourcesContent":["import type { Job } from '@/jobs';\n\n/**\n * Base error class for all Monque-related errors.\n *\n * @example\n * ```typescript\n * try {\n * await monque.enqueue('job', data);\n * } catch (error) {\n * if (error instanceof MonqueError) {\n * console.error('Monque error:', error.message);\n * }\n * }\n * ```\n */\nexport class MonqueError extends Error {\n\tconstructor(message: string) {\n\t\tsuper(message);\n\t\tthis.name = 'MonqueError';\n\t\t// Maintains proper stack trace for where our error was thrown (only available on V8)\n\t\t/* istanbul ignore next -- @preserve captureStackTrace is always available in Node.js */\n\t\tif (Error.captureStackTrace) {\n\t\t\tError.captureStackTrace(this, MonqueError);\n\t\t}\n\t}\n}\n\n/**\n * Error thrown when an invalid cron expression is provided.\n *\n * @example\n * ```typescript\n * try {\n * await monque.schedule('invalid cron', 'job', data);\n * } catch (error) {\n * if (error instanceof InvalidCronError) {\n * console.error('Invalid expression:', error.expression);\n * }\n * }\n * ```\n */\nexport class InvalidCronError extends MonqueError {\n\tconstructor(\n\t\tpublic readonly expression: string,\n\t\tmessage: string,\n\t) {\n\t\tsuper(message);\n\t\tthis.name = 'InvalidCronError';\n\t\t/* istanbul ignore next -- @preserve captureStackTrace is always available in Node.js */\n\t\tif (Error.captureStackTrace) {\n\t\t\tError.captureStackTrace(this, InvalidCronError);\n\t\t}\n\t}\n}\n\n/**\n * Error thrown when there's a database connection issue.\n *\n * @example\n * ```typescript\n * try {\n * await monque.enqueue('job', data);\n * } catch (error) {\n * if (error instanceof ConnectionError) {\n * console.error('Database connection lost');\n * }\n * }\n * ```\n */\nexport class ConnectionError extends MonqueError {\n\tconstructor(message: string, options?: { cause?: Error }) {\n\t\tsuper(message);\n\t\tthis.name = 'ConnectionError';\n\t\tif (options?.cause) {\n\t\t\tthis.cause = options.cause;\n\t\t}\n\t\t/* istanbul ignore next -- @preserve captureStackTrace is always available in Node.js */\n\t\tif (Error.captureStackTrace) {\n\t\t\tError.captureStackTrace(this, ConnectionError);\n\t\t}\n\t}\n}\n\n/**\n * Error thrown when graceful shutdown times out.\n * Includes information about jobs that were still in progress.\n *\n * @example\n * ```typescript\n * try {\n * await monque.stop();\n * } catch (error) {\n * if (error instanceof ShutdownTimeoutError) {\n * console.error('Incomplete jobs:', error.incompleteJobs.length);\n * }\n * }\n * ```\n */\nexport class ShutdownTimeoutError extends MonqueError {\n\tconstructor(\n\t\tmessage: string,\n\t\tpublic readonly incompleteJobs: Job[],\n\t) {\n\t\tsuper(message);\n\t\tthis.name = 'ShutdownTimeoutError';\n\t\t/* istanbul ignore next -- @preserve captureStackTrace is always available in Node.js */\n\t\tif (Error.captureStackTrace) {\n\t\t\tError.captureStackTrace(this, ShutdownTimeoutError);\n\t\t}\n\t}\n}\n\n/**\n * Error thrown when attempting to register a worker for a job name\n * that already has a registered worker, without explicitly allowing replacement.\n *\n * @example\n * ```typescript\n * try {\n * monque.register('send-email', handler1);\n * monque.register('send-email', handler2); // throws\n * } catch (error) {\n * if (error instanceof WorkerRegistrationError) {\n * console.error('Worker already registered for:', error.jobName);\n * }\n * }\n *\n * // To intentionally replace a worker:\n * monque.register('send-email', handler2, { replace: true });\n * ```\n */\nexport class WorkerRegistrationError extends MonqueError {\n\tconstructor(\n\t\tmessage: string,\n\t\tpublic readonly jobName: string,\n\t) {\n\t\tsuper(message);\n\t\tthis.name = 'WorkerRegistrationError';\n\t\t/* istanbul ignore next -- @preserve captureStackTrace is always available in Node.js */\n\t\tif (Error.captureStackTrace) {\n\t\t\tError.captureStackTrace(this, WorkerRegistrationError);\n\t\t}\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAgBA,IAAa,cAAb,MAAa,oBAAoB,MAAM;CACtC,YAAY,SAAiB;AAC5B,QAAM,QAAQ;AACd,OAAK,OAAO;;AAGZ,MAAI,MAAM,kBACT,OAAM,kBAAkB,MAAM,YAAY;;;;;;;;;;;;;;;;;AAmB7C,IAAa,mBAAb,MAAa,yBAAyB,YAAY;CACjD,YACC,AAAgB,YAChB,SACC;AACD,QAAM,QAAQ;EAHE;AAIhB,OAAK,OAAO;;AAEZ,MAAI,MAAM,kBACT,OAAM,kBAAkB,MAAM,iBAAiB;;;;;;;;;;;;;;;;;AAmBlD,IAAa,kBAAb,MAAa,wBAAwB,YAAY;CAChD,YAAY,SAAiB,SAA6B;AACzD,QAAM,QAAQ;AACd,OAAK,OAAO;AACZ,MAAI,SAAS,MACZ,MAAK,QAAQ,QAAQ;;AAGtB,MAAI,MAAM,kBACT,OAAM,kBAAkB,MAAM,gBAAgB;;;;;;;;;;;;;;;;;;AAoBjD,IAAa,uBAAb,MAAa,6BAA6B,YAAY;CACrD,YACC,SACA,AAAgB,gBACf;AACD,QAAM,QAAQ;EAFE;AAGhB,OAAK,OAAO;;AAEZ,MAAI,MAAM,kBACT,OAAM,kBAAkB,MAAM,qBAAqB;;;;;;;;;;;;;;;;;;;;;;AAwBtD,IAAa,0BAAb,MAAa,gCAAgC,YAAY;CACxD,YACC,SACA,AAAgB,SACf;AACD,QAAM,QAAQ;EAFE;AAGhB,OAAK,OAAO;;AAEZ,MAAI,MAAM,kBACT,OAAM,kBAAkB,MAAM,wBAAwB"}
@@ -1,3 +0,0 @@
1
- import { a as WorkerRegistrationError, i as ShutdownTimeoutError, n as InvalidCronError, r as MonqueError, t as ConnectionError } from "./errors-DQ2_gprw.mjs";
2
-
3
- export { ShutdownTimeoutError };
@@ -1,125 +0,0 @@
1
- //#region src/shared/errors.ts
2
- /**
3
- * Base error class for all Monque-related errors.
4
- *
5
- * @example
6
- * ```typescript
7
- * try {
8
- * await monque.enqueue('job', data);
9
- * } catch (error) {
10
- * if (error instanceof MonqueError) {
11
- * console.error('Monque error:', error.message);
12
- * }
13
- * }
14
- * ```
15
- */
16
- var MonqueError = class MonqueError extends Error {
17
- constructor(message) {
18
- super(message);
19
- this.name = "MonqueError";
20
- /* istanbul ignore next -- @preserve captureStackTrace is always available in Node.js */
21
- if (Error.captureStackTrace) Error.captureStackTrace(this, MonqueError);
22
- }
23
- };
24
- /**
25
- * Error thrown when an invalid cron expression is provided.
26
- *
27
- * @example
28
- * ```typescript
29
- * try {
30
- * await monque.schedule('invalid cron', 'job', data);
31
- * } catch (error) {
32
- * if (error instanceof InvalidCronError) {
33
- * console.error('Invalid expression:', error.expression);
34
- * }
35
- * }
36
- * ```
37
- */
38
- var InvalidCronError = class InvalidCronError extends MonqueError {
39
- constructor(expression, message) {
40
- super(message);
41
- this.expression = expression;
42
- this.name = "InvalidCronError";
43
- /* istanbul ignore next -- @preserve captureStackTrace is always available in Node.js */
44
- if (Error.captureStackTrace) Error.captureStackTrace(this, InvalidCronError);
45
- }
46
- };
47
- /**
48
- * Error thrown when there's a database connection issue.
49
- *
50
- * @example
51
- * ```typescript
52
- * try {
53
- * await monque.enqueue('job', data);
54
- * } catch (error) {
55
- * if (error instanceof ConnectionError) {
56
- * console.error('Database connection lost');
57
- * }
58
- * }
59
- * ```
60
- */
61
- var ConnectionError = class ConnectionError extends MonqueError {
62
- constructor(message, options) {
63
- super(message);
64
- this.name = "ConnectionError";
65
- if (options?.cause) this.cause = options.cause;
66
- /* istanbul ignore next -- @preserve captureStackTrace is always available in Node.js */
67
- if (Error.captureStackTrace) Error.captureStackTrace(this, ConnectionError);
68
- }
69
- };
70
- /**
71
- * Error thrown when graceful shutdown times out.
72
- * Includes information about jobs that were still in progress.
73
- *
74
- * @example
75
- * ```typescript
76
- * try {
77
- * await monque.stop();
78
- * } catch (error) {
79
- * if (error instanceof ShutdownTimeoutError) {
80
- * console.error('Incomplete jobs:', error.incompleteJobs.length);
81
- * }
82
- * }
83
- * ```
84
- */
85
- var ShutdownTimeoutError = class ShutdownTimeoutError extends MonqueError {
86
- constructor(message, incompleteJobs) {
87
- super(message);
88
- this.incompleteJobs = incompleteJobs;
89
- this.name = "ShutdownTimeoutError";
90
- /* istanbul ignore next -- @preserve captureStackTrace is always available in Node.js */
91
- if (Error.captureStackTrace) Error.captureStackTrace(this, ShutdownTimeoutError);
92
- }
93
- };
94
- /**
95
- * Error thrown when attempting to register a worker for a job name
96
- * that already has a registered worker, without explicitly allowing replacement.
97
- *
98
- * @example
99
- * ```typescript
100
- * try {
101
- * monque.register('send-email', handler1);
102
- * monque.register('send-email', handler2); // throws
103
- * } catch (error) {
104
- * if (error instanceof WorkerRegistrationError) {
105
- * console.error('Worker already registered for:', error.jobName);
106
- * }
107
- * }
108
- *
109
- * // To intentionally replace a worker:
110
- * monque.register('send-email', handler2, { replace: true });
111
- * ```
112
- */
113
- var WorkerRegistrationError = class WorkerRegistrationError extends MonqueError {
114
- constructor(message, jobName) {
115
- super(message);
116
- this.jobName = jobName;
117
- this.name = "WorkerRegistrationError";
118
- /* istanbul ignore next -- @preserve captureStackTrace is always available in Node.js */
119
- if (Error.captureStackTrace) Error.captureStackTrace(this, WorkerRegistrationError);
120
- }
121
- };
122
-
123
- //#endregion
124
- export { WorkerRegistrationError as a, ShutdownTimeoutError as i, InvalidCronError as n, MonqueError as r, ConnectionError as t };
125
- //# sourceMappingURL=errors-DQ2_gprw.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"errors-DQ2_gprw.mjs","names":[],"sources":["../src/shared/errors.ts"],"sourcesContent":["import type { Job } from '@/jobs';\n\n/**\n * Base error class for all Monque-related errors.\n *\n * @example\n * ```typescript\n * try {\n * await monque.enqueue('job', data);\n * } catch (error) {\n * if (error instanceof MonqueError) {\n * console.error('Monque error:', error.message);\n * }\n * }\n * ```\n */\nexport class MonqueError extends Error {\n\tconstructor(message: string) {\n\t\tsuper(message);\n\t\tthis.name = 'MonqueError';\n\t\t// Maintains proper stack trace for where our error was thrown (only available on V8)\n\t\t/* istanbul ignore next -- @preserve captureStackTrace is always available in Node.js */\n\t\tif (Error.captureStackTrace) {\n\t\t\tError.captureStackTrace(this, MonqueError);\n\t\t}\n\t}\n}\n\n/**\n * Error thrown when an invalid cron expression is provided.\n *\n * @example\n * ```typescript\n * try {\n * await monque.schedule('invalid cron', 'job', data);\n * } catch (error) {\n * if (error instanceof InvalidCronError) {\n * console.error('Invalid expression:', error.expression);\n * }\n * }\n * ```\n */\nexport class InvalidCronError extends MonqueError {\n\tconstructor(\n\t\tpublic readonly expression: string,\n\t\tmessage: string,\n\t) {\n\t\tsuper(message);\n\t\tthis.name = 'InvalidCronError';\n\t\t/* istanbul ignore next -- @preserve captureStackTrace is always available in Node.js */\n\t\tif (Error.captureStackTrace) {\n\t\t\tError.captureStackTrace(this, InvalidCronError);\n\t\t}\n\t}\n}\n\n/**\n * Error thrown when there's a database connection issue.\n *\n * @example\n * ```typescript\n * try {\n * await monque.enqueue('job', data);\n * } catch (error) {\n * if (error instanceof ConnectionError) {\n * console.error('Database connection lost');\n * }\n * }\n * ```\n */\nexport class ConnectionError extends MonqueError {\n\tconstructor(message: string, options?: { cause?: Error }) {\n\t\tsuper(message);\n\t\tthis.name = 'ConnectionError';\n\t\tif (options?.cause) {\n\t\t\tthis.cause = options.cause;\n\t\t}\n\t\t/* istanbul ignore next -- @preserve captureStackTrace is always available in Node.js */\n\t\tif (Error.captureStackTrace) {\n\t\t\tError.captureStackTrace(this, ConnectionError);\n\t\t}\n\t}\n}\n\n/**\n * Error thrown when graceful shutdown times out.\n * Includes information about jobs that were still in progress.\n *\n * @example\n * ```typescript\n * try {\n * await monque.stop();\n * } catch (error) {\n * if (error instanceof ShutdownTimeoutError) {\n * console.error('Incomplete jobs:', error.incompleteJobs.length);\n * }\n * }\n * ```\n */\nexport class ShutdownTimeoutError extends MonqueError {\n\tconstructor(\n\t\tmessage: string,\n\t\tpublic readonly incompleteJobs: Job[],\n\t) {\n\t\tsuper(message);\n\t\tthis.name = 'ShutdownTimeoutError';\n\t\t/* istanbul ignore next -- @preserve captureStackTrace is always available in Node.js */\n\t\tif (Error.captureStackTrace) {\n\t\t\tError.captureStackTrace(this, ShutdownTimeoutError);\n\t\t}\n\t}\n}\n\n/**\n * Error thrown when attempting to register a worker for a job name\n * that already has a registered worker, without explicitly allowing replacement.\n *\n * @example\n * ```typescript\n * try {\n * monque.register('send-email', handler1);\n * monque.register('send-email', handler2); // throws\n * } catch (error) {\n * if (error instanceof WorkerRegistrationError) {\n * console.error('Worker already registered for:', error.jobName);\n * }\n * }\n *\n * // To intentionally replace a worker:\n * monque.register('send-email', handler2, { replace: true });\n * ```\n */\nexport class WorkerRegistrationError extends MonqueError {\n\tconstructor(\n\t\tmessage: string,\n\t\tpublic readonly jobName: string,\n\t) {\n\t\tsuper(message);\n\t\tthis.name = 'WorkerRegistrationError';\n\t\t/* istanbul ignore next -- @preserve captureStackTrace is always available in Node.js */\n\t\tif (Error.captureStackTrace) {\n\t\t\tError.captureStackTrace(this, WorkerRegistrationError);\n\t\t}\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;AAgBA,IAAa,cAAb,MAAa,oBAAoB,MAAM;CACtC,YAAY,SAAiB;AAC5B,QAAM,QAAQ;AACd,OAAK,OAAO;;AAGZ,MAAI,MAAM,kBACT,OAAM,kBAAkB,MAAM,YAAY;;;;;;;;;;;;;;;;;AAmB7C,IAAa,mBAAb,MAAa,yBAAyB,YAAY;CACjD,YACC,AAAgB,YAChB,SACC;AACD,QAAM,QAAQ;EAHE;AAIhB,OAAK,OAAO;;AAEZ,MAAI,MAAM,kBACT,OAAM,kBAAkB,MAAM,iBAAiB;;;;;;;;;;;;;;;;;AAmBlD,IAAa,kBAAb,MAAa,wBAAwB,YAAY;CAChD,YAAY,SAAiB,SAA6B;AACzD,QAAM,QAAQ;AACd,OAAK,OAAO;AACZ,MAAI,SAAS,MACZ,MAAK,QAAQ,QAAQ;;AAGtB,MAAI,MAAM,kBACT,OAAM,kBAAkB,MAAM,gBAAgB;;;;;;;;;;;;;;;;;;AAoBjD,IAAa,uBAAb,MAAa,6BAA6B,YAAY;CACrD,YACC,SACA,AAAgB,gBACf;AACD,QAAM,QAAQ;EAFE;AAGhB,OAAK,OAAO;;AAEZ,MAAI,MAAM,kBACT,OAAM,kBAAkB,MAAM,qBAAqB;;;;;;;;;;;;;;;;;;;;;;AAwBtD,IAAa,0BAAb,MAAa,gCAAgC,YAAY;CACxD,YACC,SACA,AAAgB,SACf;AACD,QAAM,QAAQ;EAFE;AAGhB,OAAK,OAAO;;AAEZ,MAAI,MAAM,kBACT,OAAM,kBAAkB,MAAM,wBAAwB"}
@@ -1,3 +0,0 @@
1
- const require_errors = require('./errors-D5ZGG2uI.cjs');
2
-
3
- exports.ShutdownTimeoutError = require_errors.ShutdownTimeoutError;