-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
job.ts
242 lines (216 loc) · 6.88 KB
/
job.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
import { JobState } from '@vendure/common/lib/generated-types';
import { isClassInstance, isObject } from '@vendure/common/lib/shared-utils';
import { JobConfig, JobData } from './types';
/**
* @description
* An event raised by a Job.
*
* @docsCategory JobQueue
* @docsPage Job
*/
export type JobEventType = 'progress';
/**
* @description
* The signature of the event handler expected by the `Job.on()` method.
*
* @docsCategory JobQueue
* @docsPage Job
*/
export type JobEventListener<T extends JobData<T>> = (job: Job<T>) => void;
/**
* @description
* A Job represents a piece of work to be run in the background, i.e. outside the request-response cycle.
* It is intended to be used for long-running work triggered by API requests. Jobs should now generally
* be directly instantiated. Rather, the {@link JobQueue} `add()` method should be used to create and
* add a new Job to a queue.
*
* @docsCategory JobQueue
* @docsPage Job
* @docsWeight 0
*/
export class Job<T extends JobData<T> = any> {
readonly id: number | string | null;
readonly queueName: string;
readonly retries: number;
readonly createdAt: Date;
private readonly _data: T;
private _state: JobState;
private _progress: number;
private _result?: any;
private _error?: any;
private _attempts: number;
private _startedAt?: Date;
private _settledAt?: Date;
private readonly eventListeners: { [type in JobEventType]: Array<JobEventListener<T>> } = {
progress: [],
};
get name(): string {
return this.queueName;
}
get data(): T {
return this._data;
}
get state(): JobState {
return this._state;
}
get progress(): number {
return this._progress;
}
get result(): any {
return this._result;
}
get error(): any {
return this._error;
}
get isSettled(): boolean {
return (
!!this._settledAt &&
(this._state === JobState.COMPLETED ||
this._state === JobState.FAILED ||
this._state === JobState.CANCELLED)
);
}
get startedAt(): Date | undefined {
return this._startedAt;
}
get settledAt(): Date | undefined {
return this._settledAt;
}
get duration(): number {
const end = this._settledAt || new Date();
return +end - +(this._startedAt || end);
}
get attempts(): number {
return this._attempts;
}
constructor(config: JobConfig<T>) {
this.queueName = config.queueName;
this._data = this.ensureDataIsSerializable(config.data);
this.id = config.id || null;
this._state = config.state || JobState.PENDING;
this.retries = config.retries || 0;
this._attempts = config.attempts || 0;
this._progress = config.progress || 0;
this.createdAt = config.createdAt || new Date();
this._result = config.result;
this._error = config.error;
this._startedAt = config.startedAt;
this._settledAt = config.settledAt;
}
/**
* @description
* Calling this signifies that the job work has started. This method should be
* called in the {@link JobQueueStrategy} `next()` method.
*/
start() {
if (this._state === JobState.PENDING || this._state === JobState.RETRYING) {
this._state = JobState.RUNNING;
this._startedAt = new Date();
this._attempts++;
}
}
/**
* @description
* Sets the progress (0 - 100) of the job.
*/
setProgress(percent: number) {
this._progress = Math.min(percent || 0, 100);
this.fireEvent('progress');
}
/**
* @description
* Calling this method signifies that the job succeeded. The result
* will be stored in the `Job.result` property.
*/
complete(result?: any) {
this._result = result;
this._progress = 100;
this._state = JobState.COMPLETED;
this._settledAt = new Date();
}
/**
* @description
* Calling this method signifies that the job failed.
*/
fail(err?: any) {
this._error = err?.message ? err.message : String(err);
this._progress = 0;
if (this.retries >= this._attempts) {
this._state = JobState.RETRYING;
} else {
this._state = JobState.FAILED;
this._settledAt = new Date();
}
}
cancel() {
this._progress = 0;
this._settledAt = new Date();
this._state = JobState.CANCELLED;
}
/**
* @description
* Sets a RUNNING job back to PENDING. Should be used when the JobQueue is being
* destroyed before the job has been completed.
*/
defer() {
if (this._state === JobState.RUNNING) {
this._state = JobState.PENDING;
this._attempts = 0;
}
}
/**
* @description
* Used to register event handlers for job events
*/
on(eventType: JobEventType, listener: JobEventListener<T>) {
this.eventListeners[eventType].push(listener);
}
off(eventType: JobEventType, listener: JobEventListener<T>) {
const idx = this.eventListeners[eventType].indexOf(listener);
if (idx !== -1) {
this.eventListeners[eventType].splice(idx, 1);
}
}
private fireEvent(eventType: JobEventType) {
for (const listener of this.eventListeners[eventType]) {
listener(this);
}
}
/**
* All data in a job must be serializable. This method handles certain problem cases such as when
* the data is a class instance with getters. Even though technically the "data" object should
* already be serializable per the TS type, in practice data can slip through due to loss of
* type safety.
*/
private ensureDataIsSerializable(data: any, output?: any): any {
if (data instanceof Date) {
return data.toISOString();
} else if (isObject(data)) {
if (!output) {
output = {};
}
for (const key of Object.keys(data)) {
output[key] = this.ensureDataIsSerializable((data as any)[key]);
}
if (isClassInstance(data)) {
const descriptors = Object.getOwnPropertyDescriptors(Object.getPrototypeOf(data));
for (const name of Object.keys(descriptors)) {
const descriptor = descriptors[name];
if (typeof descriptor.get === 'function') {
output[name] = (data as any)[name];
}
}
}
} else if (Array.isArray(data)) {
if (!output) {
output = [];
}
data.forEach((item, i) => {
output[i] = this.ensureDataIsSerializable(item);
});
} else {
return data;
}
return output;
}
}