forked from pulsecron/pulse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
313 lines (253 loc) · 6.85 KB
/
index.ts
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
import * as mongodb from 'mongodb';
import { Pulse } from '../pulse';
import { JobPriority } from '../pulse/define';
import { parsePriority } from '../utils';
import { ComputeNextRunAtMethod, computeNextRunAt } from './compute-next-run-at';
import { DisableMethod, disable } from './disable';
import { EnableMethod, enable } from './enable';
import { FailMethod, fail } from './fail';
import { FetchStatusMethod, fetchStatus } from './fetch-status';
import { IsExpiredMethod, isExpired } from './is-expired';
import { IsRunningMethod, isRunning } from './is-running';
import { PriorityMethod, priority } from './priority';
import { RemoveMethod, remove } from './remove';
import { RepeatAtMethod, repeatAt } from './repeat-at';
import { RepeatEveryMethod, repeatEvery } from './repeat-every';
import { RunMethod, run } from './run';
import { SaveMethod, save } from './save';
import { ScheduleMethod, schedule } from './schedule';
import { SetShouldSaveResultMethod, setShouldSaveResult } from './set-shouldsaveresult';
import { ToJsonMethod, toJson } from './to-json';
import { TouchMethod, touch } from './touch';
import { UniqueMethod, unique } from './unique';
type Modify<T, R> = Omit<T, keyof R> & R;
export interface JobAttributesData {
[key: string]: any;
}
export interface JobAttributes<T extends JobAttributesData = JobAttributesData> {
/**
* The record identity.
*/
_id: mongodb.ObjectId;
pulse: Pulse;
/**
* The type of the job (single|normal).
*/
type: string;
/**
* The name of the job.
*/
name: string;
/**
* Job's state
*/
disabled?: boolean;
/**
* Job's progress state (0 to 100)
*/
progress?: number;
/**
* Date/time the job will run next.
*/
nextRunAt?: Date | null;
/**
* Date/time the job was locked.
*/
lockedAt?: Date | null;
/**
* The priority of the job.
*/
priority: number | string;
/**
* The job details.
*/
data: T;
uniqueQuery?: any;
uniqueOpts?: {
insertOnly: boolean;
};
/**
* How often the job is repeated using a human-readable or cron format.
*/
repeatInterval?: string;
/**
* The timezone that conforms to [moment-timezone](http://momentjs.com/timezone/).
*/
repeatTimezone?: string | null;
repeatAt?: string;
/**
* Date/time the job was last run.
*/
lastRunAt?: Date;
/*
* Count of the number of times the job has run.
*/
runCount?: number;
/**
* Date/time the job last finished running.
*/
lastFinishedAt?: Date;
startDate?: Date | number | null;
endDate?: Date | number | null;
skipDays?: string | null;
/**
* The number of times the job has finished running.
*/
finishedCount?: number;
/**
* The reason the job failed.
*/
failReason?: string;
/**
* The number of times the job has failed.
*/
failCount?: number;
/**
* The date/time the job last failed.
*/
failedAt?: Date;
/**
* Date/time the job was last modified.
*/
lastModifiedBy?: string;
/**
* Should the return value of the job be persisted.
*/
shouldSaveResult?: boolean;
/**
* Number of attempts to run the job.
*/
attempts?: number;
/**
* Backoff options.
*/
backoff?: {
/**
* Type of backoff to use.
*/
type: 'exponential' | 'fixed';
/**
* Delay in ms.
*/
delay: number;
};
/**
* Result of the finished job.
*/
result?: unknown;
}
/**
* @class
* @param {Object} args - Job Options
* @property {Object} pulse - The Pulse instance
* @property {Object} attrs
*/
class Job<T extends JobAttributesData = JobAttributesData> {
private _lazyBindings: Record<string, any> = {};
/**
* The pulse that created the job.
*/
pulse: Pulse;
/**
* The database record associated with the job.
*/
attrs: JobAttributes<T>;
constructor(options: Modify<JobAttributes<T>, { _id?: mongodb.ObjectId }>) {
const { pulse, type, nextRunAt, ...args } = options ?? {};
// Save Pulse instance
this.pulse = pulse;
// Set priority
args.priority = args.priority === undefined ? JobPriority.normal : parsePriority(args.priority);
// Set shouldSaveResult option
args.shouldSaveResult = args.shouldSaveResult || false;
// Set attrs to args
const attrs: any = {};
for (const key in args) {
if ({}.hasOwnProperty.call(args, key)) {
// @ts-expect-error
attrs[key] = args[key];
}
}
// Set defaults if undefined
this.attrs = {
...attrs,
name: attrs.name || '',
priority: attrs.priority,
type: type || 'once',
// if a job that's non-recurring has a lastFinishedAt (finished the job), do not default nextRunAt to now
// only if it will be defaulted either by explicitly setting it or by computing it computeNextRunAt
nextRunAt: nextRunAt || new Date(),
};
}
/**
***************************************
* Public methods
* *************************************
*/
get toJSON(): ToJsonMethod {
return this.bindMethod('toJSON', toJson);
}
get computeNextRunAt(): ComputeNextRunAtMethod {
return this.bindMethod('computeNextRunAt', computeNextRunAt);
}
get repeatEvery(): RepeatEveryMethod {
return this.bindMethod('repeatEvery', repeatEvery);
}
get repeatAt(): RepeatAtMethod {
return this.bindMethod('repeatAt', repeatAt);
}
get disable(): DisableMethod {
return this.bindMethod('disable', disable);
}
get enable(): EnableMethod {
return this.bindMethod('enable', enable);
}
get unique(): UniqueMethod {
return this.bindMethod('unique', unique);
}
get schedule(): ScheduleMethod {
return this.bindMethod('schedule', schedule);
}
get priority(): PriorityMethod {
return this.bindMethod('priority', priority);
}
get fail(): FailMethod {
return this.bindMethod('fail', fail);
}
get run(): RunMethod {
return this.bindMethod('run', run);
}
get isRunning(): IsRunningMethod {
return this.bindMethod('isRunning', isRunning);
}
get isExpired(): IsExpiredMethod {
return this.bindMethod('isExpired', isExpired);
}
get save(): SaveMethod {
return this.bindMethod('save', save);
}
get remove(): RemoveMethod {
return this.bindMethod('remove', remove);
}
get touch(): TouchMethod {
return this.bindMethod('touch', touch);
}
get setShouldSaveResult(): SetShouldSaveResultMethod {
return this.bindMethod('setShouldSaveResult', setShouldSaveResult);
}
get fetchStatus(): FetchStatusMethod {
return this.bindMethod('fetchStatus', fetchStatus);
}
/**
***************************************
* Private methods
* *************************************
*/
private bindMethod<T extends Function>(methodName: string, fn: T): T {
if (!this._lazyBindings[methodName]) {
this._lazyBindings[methodName] = fn.bind(this);
}
return this._lazyBindings[methodName] as T;
}
}
export { Job };