@quave/agenda 1.0.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/LICENSE.md +25 -0
- package/README.md +380 -0
- package/dist/Job.d.ts +190 -0
- package/dist/Job.js +689 -0
- package/dist/Job.js.map +1 -0
- package/dist/JobProcessingQueue.d.ts +47 -0
- package/dist/JobProcessingQueue.js +107 -0
- package/dist/JobProcessingQueue.js.map +1 -0
- package/dist/JobProcessor.d.ts +72 -0
- package/dist/JobProcessor.js +512 -0
- package/dist/JobProcessor.js.map +1 -0
- package/dist/backends/index.d.ts +18 -0
- package/dist/backends/index.js +20 -0
- package/dist/backends/index.js.map +1 -0
- package/dist/cjs/Job.d.ts +190 -0
- package/dist/cjs/Job.js +696 -0
- package/dist/cjs/Job.js.map +1 -0
- package/dist/cjs/JobProcessingQueue.d.ts +47 -0
- package/dist/cjs/JobProcessingQueue.js +111 -0
- package/dist/cjs/JobProcessingQueue.js.map +1 -0
- package/dist/cjs/JobProcessor.d.ts +72 -0
- package/dist/cjs/JobProcessor.js +519 -0
- package/dist/cjs/JobProcessor.js.map +1 -0
- package/dist/cjs/backends/index.d.ts +17 -0
- package/dist/cjs/backends/index.js +20 -0
- package/dist/cjs/backends/index.js.map +1 -0
- package/dist/cjs/decorators/Define.d.ts +33 -0
- package/dist/cjs/decorators/Define.js +47 -0
- package/dist/cjs/decorators/Define.js.map +1 -0
- package/dist/cjs/decorators/Every.d.ts +43 -0
- package/dist/cjs/decorators/Every.js +58 -0
- package/dist/cjs/decorators/Every.js.map +1 -0
- package/dist/cjs/decorators/JobsController.d.ts +43 -0
- package/dist/cjs/decorators/JobsController.js +40 -0
- package/dist/cjs/decorators/JobsController.js.map +1 -0
- package/dist/cjs/decorators/index.d.ts +41 -0
- package/dist/cjs/decorators/index.js +57 -0
- package/dist/cjs/decorators/index.js.map +1 -0
- package/dist/cjs/decorators/metadata.d.ts +50 -0
- package/dist/cjs/decorators/metadata.js +41 -0
- package/dist/cjs/decorators/metadata.js.map +1 -0
- package/dist/cjs/decorators/register.d.ts +52 -0
- package/dist/cjs/decorators/register.js +145 -0
- package/dist/cjs/decorators/register.js.map +1 -0
- package/dist/cjs/index.d.ts +498 -0
- package/dist/cjs/index.js +776 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/notifications/BaseNotificationChannel.d.ts +47 -0
- package/dist/cjs/notifications/BaseNotificationChannel.js +112 -0
- package/dist/cjs/notifications/BaseNotificationChannel.js.map +1 -0
- package/dist/cjs/notifications/InMemoryNotificationChannel.d.ts +13 -0
- package/dist/cjs/notifications/InMemoryNotificationChannel.js +37 -0
- package/dist/cjs/notifications/InMemoryNotificationChannel.js.map +1 -0
- package/dist/cjs/notifications/index.d.ts +2 -0
- package/dist/cjs/notifications/index.js +8 -0
- package/dist/cjs/notifications/index.js.map +1 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/types/AgendaBackend.d.ts +61 -0
- package/dist/cjs/types/AgendaBackend.js +3 -0
- package/dist/cjs/types/AgendaBackend.js.map +1 -0
- package/dist/cjs/types/AgendaConfig.d.ts +10 -0
- package/dist/cjs/types/AgendaConfig.js +3 -0
- package/dist/cjs/types/AgendaConfig.js.map +1 -0
- package/dist/cjs/types/AgendaStatus.d.ts +32 -0
- package/dist/cjs/types/AgendaStatus.js +3 -0
- package/dist/cjs/types/AgendaStatus.js.map +1 -0
- package/dist/cjs/types/DbOptions.d.ts +4 -0
- package/dist/cjs/types/DbOptions.js +3 -0
- package/dist/cjs/types/DbOptions.js.map +1 -0
- package/dist/cjs/types/DrainOptions.d.ts +37 -0
- package/dist/cjs/types/DrainOptions.js +3 -0
- package/dist/cjs/types/DrainOptions.js.map +1 -0
- package/dist/cjs/types/JobDefinition.d.ts +49 -0
- package/dist/cjs/types/JobDefinition.js +3 -0
- package/dist/cjs/types/JobDefinition.js.map +1 -0
- package/dist/cjs/types/JobLogger.d.ts +111 -0
- package/dist/cjs/types/JobLogger.js +3 -0
- package/dist/cjs/types/JobLogger.js.map +1 -0
- package/dist/cjs/types/JobParameters.d.ts +106 -0
- package/dist/cjs/types/JobParameters.js +21 -0
- package/dist/cjs/types/JobParameters.js.map +1 -0
- package/dist/cjs/types/JobQuery.d.ts +79 -0
- package/dist/cjs/types/JobQuery.js +36 -0
- package/dist/cjs/types/JobQuery.js.map +1 -0
- package/dist/cjs/types/JobRepository.d.ts +101 -0
- package/dist/cjs/types/JobRepository.js +3 -0
- package/dist/cjs/types/JobRepository.js.map +1 -0
- package/dist/cjs/types/NotificationChannel.d.ts +120 -0
- package/dist/cjs/types/NotificationChannel.js +3 -0
- package/dist/cjs/types/NotificationChannel.js.map +1 -0
- package/dist/cjs/utils/backoff.d.ts +152 -0
- package/dist/cjs/utils/backoff.js +175 -0
- package/dist/cjs/utils/backoff.js.map +1 -0
- package/dist/cjs/utils/dateConstraints.d.ts +48 -0
- package/dist/cjs/utils/dateConstraints.js +127 -0
- package/dist/cjs/utils/dateConstraints.js.map +1 -0
- package/dist/cjs/utils/isValidDate.d.ts +1 -0
- package/dist/cjs/utils/isValidDate.js +8 -0
- package/dist/cjs/utils/isValidDate.js.map +1 -0
- package/dist/cjs/utils/nextRunAt.d.ts +10 -0
- package/dist/cjs/utils/nextRunAt.js +119 -0
- package/dist/cjs/utils/nextRunAt.js.map +1 -0
- package/dist/cjs/utils/priority.d.ts +13 -0
- package/dist/cjs/utils/priority.js +23 -0
- package/dist/cjs/utils/priority.js.map +1 -0
- package/dist/cjs/utils/processEvery.d.ts +1 -0
- package/dist/cjs/utils/processEvery.js +13 -0
- package/dist/cjs/utils/processEvery.js.map +1 -0
- package/dist/cjs/utils/stack.d.ts +1 -0
- package/dist/cjs/utils/stack.js +21 -0
- package/dist/cjs/utils/stack.js.map +1 -0
- package/dist/cjs/version.d.ts +9 -0
- package/dist/cjs/version.js +13 -0
- package/dist/cjs/version.js.map +1 -0
- package/dist/decorators/Define.d.ts +33 -0
- package/dist/decorators/Define.js +44 -0
- package/dist/decorators/Define.js.map +1 -0
- package/dist/decorators/Every.d.ts +43 -0
- package/dist/decorators/Every.js +55 -0
- package/dist/decorators/Every.js.map +1 -0
- package/dist/decorators/JobsController.d.ts +43 -0
- package/dist/decorators/JobsController.js +37 -0
- package/dist/decorators/JobsController.js.map +1 -0
- package/dist/decorators/index.d.ts +41 -0
- package/dist/decorators/index.js +46 -0
- package/dist/decorators/index.js.map +1 -0
- package/dist/decorators/metadata.d.ts +50 -0
- package/dist/decorators/metadata.js +35 -0
- package/dist/decorators/metadata.js.map +1 -0
- package/dist/decorators/register.d.ts +52 -0
- package/dist/decorators/register.js +139 -0
- package/dist/decorators/register.js.map +1 -0
- package/dist/index.d.ts +498 -0
- package/dist/index.js +744 -0
- package/dist/index.js.map +1 -0
- package/dist/notifications/BaseNotificationChannel.d.ts +47 -0
- package/dist/notifications/BaseNotificationChannel.js +108 -0
- package/dist/notifications/BaseNotificationChannel.js.map +1 -0
- package/dist/notifications/InMemoryNotificationChannel.d.ts +13 -0
- package/dist/notifications/InMemoryNotificationChannel.js +33 -0
- package/dist/notifications/InMemoryNotificationChannel.js.map +1 -0
- package/dist/notifications/index.d.ts +2 -0
- package/dist/notifications/index.js +3 -0
- package/dist/notifications/index.js.map +1 -0
- package/dist/types/AgendaBackend.d.ts +61 -0
- package/dist/types/AgendaBackend.js +2 -0
- package/dist/types/AgendaBackend.js.map +1 -0
- package/dist/types/AgendaConfig.d.ts +10 -0
- package/dist/types/AgendaConfig.js +2 -0
- package/dist/types/AgendaConfig.js.map +1 -0
- package/dist/types/AgendaStatus.d.ts +32 -0
- package/dist/types/AgendaStatus.js +2 -0
- package/dist/types/AgendaStatus.js.map +1 -0
- package/dist/types/DbOptions.d.ts +4 -0
- package/dist/types/DbOptions.js +2 -0
- package/dist/types/DbOptions.js.map +1 -0
- package/dist/types/DrainOptions.d.ts +37 -0
- package/dist/types/DrainOptions.js +2 -0
- package/dist/types/DrainOptions.js.map +1 -0
- package/dist/types/JobDefinition.d.ts +49 -0
- package/dist/types/JobDefinition.js +2 -0
- package/dist/types/JobDefinition.js.map +1 -0
- package/dist/types/JobLogger.d.ts +111 -0
- package/dist/types/JobLogger.js +2 -0
- package/dist/types/JobLogger.js.map +1 -0
- package/dist/types/JobParameters.d.ts +106 -0
- package/dist/types/JobParameters.js +17 -0
- package/dist/types/JobParameters.js.map +1 -0
- package/dist/types/JobQuery.d.ts +79 -0
- package/dist/types/JobQuery.js +33 -0
- package/dist/types/JobQuery.js.map +1 -0
- package/dist/types/JobRepository.d.ts +101 -0
- package/dist/types/JobRepository.js +2 -0
- package/dist/types/JobRepository.js.map +1 -0
- package/dist/types/NotificationChannel.d.ts +120 -0
- package/dist/types/NotificationChannel.js +2 -0
- package/dist/types/NotificationChannel.js.map +1 -0
- package/dist/utils/backoff.d.ts +152 -0
- package/dist/utils/backoff.js +167 -0
- package/dist/utils/backoff.js.map +1 -0
- package/dist/utils/dateConstraints.d.ts +48 -0
- package/dist/utils/dateConstraints.js +117 -0
- package/dist/utils/dateConstraints.js.map +1 -0
- package/dist/utils/isValidDate.d.ts +1 -0
- package/dist/utils/isValidDate.js +5 -0
- package/dist/utils/isValidDate.js.map +1 -0
- package/dist/utils/nextRunAt.d.ts +10 -0
- package/dist/utils/nextRunAt.js +110 -0
- package/dist/utils/nextRunAt.js.map +1 -0
- package/dist/utils/priority.d.ts +13 -0
- package/dist/utils/priority.js +20 -0
- package/dist/utils/priority.js.map +1 -0
- package/dist/utils/processEvery.d.ts +1 -0
- package/dist/utils/processEvery.js +7 -0
- package/dist/utils/processEvery.js.map +1 -0
- package/dist/utils/stack.d.ts +1 -0
- package/dist/utils/stack.js +18 -0
- package/dist/utils/stack.js.map +1 -0
- package/dist/version.d.ts +9 -0
- package/dist/version.js +10 -0
- package/dist/version.js.map +1 -0
- package/package.json +79 -0
- package/test/fixtures/forkHelper.js +40 -0
- package/test/fixtures/someJobDefinition.ts +12 -0
- package/test/shared/agenda-test-suite.ts +1876 -0
- package/test/shared/backoff-test-suite.ts +408 -0
- package/test/shared/debounce-test-suite.ts +377 -0
- package/test/shared/full-test-suite.ts +161 -0
- package/test/shared/index.ts +50 -0
- package/test/shared/joblogger-test-suite.ts +305 -0
- package/test/shared/jobprocessor-test-suite.ts +266 -0
- package/test/shared/notification-channel-test-suite.ts +471 -0
- package/test/shared/removeoncomplete-test-suite.ts +228 -0
- package/test/shared/repository-test-suite.ts +708 -0
- package/test/shared/retry-test-suite.ts +174 -0
- package/test/shared/test-utils.ts +100 -0
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import type { Agenda } from './index.js';
|
|
2
|
+
import { JobParameters, JobId } from './types/JobParameters.js';
|
|
3
|
+
import { JobPriority } from './utils/priority.js';
|
|
4
|
+
/**
|
|
5
|
+
* @class
|
|
6
|
+
*/
|
|
7
|
+
export declare class Job<DATA = unknown | void> {
|
|
8
|
+
readonly agenda: Agenda;
|
|
9
|
+
private readonly byJobProcessor;
|
|
10
|
+
readonly attrs: JobParameters<DATA>;
|
|
11
|
+
/** this flag is set to true, if a job got canceled (e.g. due to a timeout or other exception),
|
|
12
|
+
* you can use it for long running tasks to periodically check if canceled is true,
|
|
13
|
+
* also touch will check if and throws that the job got canceled
|
|
14
|
+
*/
|
|
15
|
+
private canceled?;
|
|
16
|
+
getCanceledMessage(): string | true | Error | undefined;
|
|
17
|
+
private forkedChild?;
|
|
18
|
+
cancel(error?: Error): void;
|
|
19
|
+
/** internal variable to ensure a job does not set unlimited numbers of setTimeouts if the job is not processed
|
|
20
|
+
* immediately */
|
|
21
|
+
gotTimerToExecute: boolean;
|
|
22
|
+
/**
|
|
23
|
+
* creates a new job object
|
|
24
|
+
* @param agenda
|
|
25
|
+
* @param args
|
|
26
|
+
* @param byJobProcessor
|
|
27
|
+
*/
|
|
28
|
+
constructor(agenda: Agenda, args: Partial<JobParameters<void>> & {
|
|
29
|
+
name: string;
|
|
30
|
+
type: 'normal' | 'single';
|
|
31
|
+
}, byJobProcessor?: boolean);
|
|
32
|
+
constructor(agenda: Agenda, args: Partial<JobParameters<DATA>> & {
|
|
33
|
+
name: string;
|
|
34
|
+
type: 'normal' | 'single';
|
|
35
|
+
data: DATA;
|
|
36
|
+
}, byJobProcessor?: boolean);
|
|
37
|
+
/**
|
|
38
|
+
* Fields managed by the job processor that should not be saved by user code.
|
|
39
|
+
* These are excluded in save() to prevent race conditions between user code and the processor.
|
|
40
|
+
*/
|
|
41
|
+
private static readonly PROCESSOR_MANAGED_FIELDS;
|
|
42
|
+
/**
|
|
43
|
+
* Given a job, turn it into an JobParameters object
|
|
44
|
+
* @param excludeProcessorFields - If true, excludes fields managed by the job processor
|
|
45
|
+
*/
|
|
46
|
+
toJson(excludeProcessorFields?: boolean): JobParameters;
|
|
47
|
+
/**
|
|
48
|
+
* Sets a job to repeat every X amount of time
|
|
49
|
+
* @param interval
|
|
50
|
+
* @param options
|
|
51
|
+
*/
|
|
52
|
+
repeatEvery(interval: string | number, options?: {
|
|
53
|
+
timezone?: string;
|
|
54
|
+
skipImmediate?: boolean;
|
|
55
|
+
}): this;
|
|
56
|
+
/**
|
|
57
|
+
* Sets a job to repeat at a specific time
|
|
58
|
+
* @param time
|
|
59
|
+
*/
|
|
60
|
+
repeatAt(time: string): this;
|
|
61
|
+
/**
|
|
62
|
+
* Sets the start date for the job.
|
|
63
|
+
* The job will not run before this date.
|
|
64
|
+
* @param date - The start date (Date object or string parseable by Date constructor)
|
|
65
|
+
*/
|
|
66
|
+
startDate(dateInput: Date | string): this;
|
|
67
|
+
/**
|
|
68
|
+
* Sets the end date for the job.
|
|
69
|
+
* The job will not run after this date (nextRunAt will be set to null).
|
|
70
|
+
* @param date - The end date (Date object or string parseable by Date constructor)
|
|
71
|
+
*/
|
|
72
|
+
endDate(dateInput: Date | string): this;
|
|
73
|
+
/**
|
|
74
|
+
* Sets the days of the week to skip.
|
|
75
|
+
* The job will not run on these days.
|
|
76
|
+
* @param days - Array of days to skip (0 = Sunday, 1 = Monday, ..., 6 = Saturday)
|
|
77
|
+
*/
|
|
78
|
+
skipDays(days: number[]): this;
|
|
79
|
+
/**
|
|
80
|
+
* if set, a job is forked via node child process and runs in a seperate/own
|
|
81
|
+
* thread
|
|
82
|
+
* @param enableForkMode
|
|
83
|
+
*/
|
|
84
|
+
forkMode(enableForkMode: boolean): this;
|
|
85
|
+
/**
|
|
86
|
+
* Prevents the job from running
|
|
87
|
+
*/
|
|
88
|
+
disable(): this;
|
|
89
|
+
/**
|
|
90
|
+
* Allows job to run
|
|
91
|
+
*/
|
|
92
|
+
enable(): this;
|
|
93
|
+
/**
|
|
94
|
+
* Data to ensure is unique for job to be created
|
|
95
|
+
* @param unique
|
|
96
|
+
* @param opts
|
|
97
|
+
*/
|
|
98
|
+
unique(unique: Required<JobParameters<DATA>>['unique'], opts?: JobParameters['uniqueOpts']): this;
|
|
99
|
+
/**
|
|
100
|
+
* Configure debounce behavior for this job.
|
|
101
|
+
* Debouncing delays job execution and resets the timer on subsequent saves,
|
|
102
|
+
* ensuring the job only runs once after a quiet period.
|
|
103
|
+
*
|
|
104
|
+
* IMPORTANT: Requires a unique constraint to be set via `.unique()`.
|
|
105
|
+
* The unique constraint identifies which jobs should be debounced together.
|
|
106
|
+
*
|
|
107
|
+
* @param delay - Debounce window in milliseconds
|
|
108
|
+
* @param options - Optional configuration (maxWait, strategy)
|
|
109
|
+
*
|
|
110
|
+
* @example
|
|
111
|
+
* ```ts
|
|
112
|
+
* // Basic trailing debounce - execute 2s after last save
|
|
113
|
+
* await agenda.create('updateIndex', { entityId: 123 })
|
|
114
|
+
* .unique({ 'data.entityId': 123 })
|
|
115
|
+
* .debounce(2000)
|
|
116
|
+
* .save();
|
|
117
|
+
*
|
|
118
|
+
* // With maxWait - guarantee execution within 30s
|
|
119
|
+
* await agenda.create('syncUser', { userId: 456 })
|
|
120
|
+
* .unique({ 'data.userId': 456 })
|
|
121
|
+
* .debounce(5000, { maxWait: 30000 })
|
|
122
|
+
* .save();
|
|
123
|
+
*
|
|
124
|
+
* // Leading strategy - execute immediately, ignore subsequent calls
|
|
125
|
+
* await agenda.create('notify', { channel: '#alerts' })
|
|
126
|
+
* .unique({ 'data.channel': '#alerts' })
|
|
127
|
+
* .debounce(60000, { strategy: 'leading' })
|
|
128
|
+
* .save();
|
|
129
|
+
* ```
|
|
130
|
+
*/
|
|
131
|
+
debounce(delay: number, options?: {
|
|
132
|
+
maxWait?: number;
|
|
133
|
+
strategy?: 'trailing' | 'leading';
|
|
134
|
+
}): this;
|
|
135
|
+
/**
|
|
136
|
+
* Schedules a job to run at specified time.
|
|
137
|
+
* Date constraints (startDate, endDate, skipDays) are applied if set.
|
|
138
|
+
* @param time
|
|
139
|
+
*/
|
|
140
|
+
schedule(time: string | Date): this;
|
|
141
|
+
/**
|
|
142
|
+
* Sets priority of the job
|
|
143
|
+
* @param priority priority of when job should be queued
|
|
144
|
+
*/
|
|
145
|
+
priority(priority: JobPriority): this;
|
|
146
|
+
/**
|
|
147
|
+
* Fails the job with a reason (error) specified
|
|
148
|
+
*
|
|
149
|
+
* @param reason
|
|
150
|
+
*/
|
|
151
|
+
fail(reason: Error | string): this;
|
|
152
|
+
private fetchStatus;
|
|
153
|
+
/**
|
|
154
|
+
* A job is running if:
|
|
155
|
+
* (lastRunAt exists AND lastFinishedAt does not exist)
|
|
156
|
+
* OR
|
|
157
|
+
* (lastRunAt exists AND lastFinishedAt exists but the lastRunAt is newer [in time] than lastFinishedAt)
|
|
158
|
+
* @returns Whether or not job is running at the moment (true for running)
|
|
159
|
+
*/
|
|
160
|
+
isRunning(): Promise<boolean>;
|
|
161
|
+
/**
|
|
162
|
+
* Saves a job to database
|
|
163
|
+
*/
|
|
164
|
+
save(): Promise<Job>;
|
|
165
|
+
/**
|
|
166
|
+
* Remove the job from database
|
|
167
|
+
*/
|
|
168
|
+
remove(): Promise<number>;
|
|
169
|
+
isDead(): Promise<boolean>;
|
|
170
|
+
isExpired(): Promise<boolean>;
|
|
171
|
+
/**
|
|
172
|
+
* Updates "lockedAt" time so the job does not get picked up again
|
|
173
|
+
* @param progress 0 to 100
|
|
174
|
+
*/
|
|
175
|
+
touch(progress?: number): Promise<void>;
|
|
176
|
+
private computeNextRunAt;
|
|
177
|
+
/**
|
|
178
|
+
* Handle automatic retry with backoff strategy
|
|
179
|
+
* @param error - The error that caused the failure
|
|
180
|
+
*/
|
|
181
|
+
private handleRetry;
|
|
182
|
+
run(): Promise<void>;
|
|
183
|
+
runJob(): Promise<void>;
|
|
184
|
+
private isPromise;
|
|
185
|
+
}
|
|
186
|
+
export type JobWithId = Job & {
|
|
187
|
+
attrs: JobParameters & {
|
|
188
|
+
_id: JobId;
|
|
189
|
+
};
|
|
190
|
+
};
|