-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathpollQueue.ts
More file actions
250 lines (219 loc) · 7.01 KB
/
pollQueue.ts
File metadata and controls
250 lines (219 loc) · 7.01 KB
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
import { EmitsEvent } from '../../events/index.js';
import { Obj } from '../../utils/index.js';
import { logTime } from '../../utils/helpers.js';
import { JobStatusEnum } from '../constants.js';
import { JobFailed, JobProcessed, JobProcessing } from '../events/index.js';
import { JobMaxRetriesExceeed } from '../events/jobMaxRetries.js';
import { ListenerOptions } from '../interfaces/index.js';
import { JobRunner } from '../jobRunners/base.js';
import { QueueMetadata } from '../metadata.js';
import { Dispatch } from '../queue.js';
import { QueueService } from '../service.js';
import { DriverJob, InternalMessage } from '../strategy/index.js';
import { PollQueueDriver } from '../strategy/pollQueueDriver.js';
import { BaseQueueWorker } from './baseWorker.js';
export class PollQueueWorker extends BaseQueueWorker {
declare protected options: ListenerOptions;
protected jobInProgress: boolean;
protected killSigReceived: boolean;
constructor(options?: ListenerOptions) {
super();
const defaultOptions = QueueMetadata.getDefaultOptions();
this.options = options || {};
this.options = {
...defaultOptions,
schedulerInterval: 10000,
queue: undefined,
logger: true,
...this.options,
};
this.jobInProgress = false;
this.killSigReceived = false;
if (!this.options.queue) {
const data = QueueMetadata.getData();
this.options['queue'] = data.connections[
this.options.connection || defaultOptions.connection
].queue as string;
}
this.attachDeamonListeners();
}
static init(options?: ListenerOptions): PollQueueWorker {
return new PollQueueWorker(options);
}
private async poll(connection: PollQueueDriver): Promise<DriverJob[] | null> {
return await connection.pull({ queue: this.options.queue });
}
/**
* Listen to the queue
*/
async listen() {
this.logInfo('Poll Queue Worker Initialised');
this.logInfo('Listening for messages...');
const { client } = QueueService.makeDriver<PollQueueDriver>(
this.options.connection,
);
// perform scheduled task of the driver
if (client.scheduledTask) this.performScheduledTask(client);
const runner = new JobRunner(this.options, client);
// eslint-disable-next-line no-constant-condition
while (1 && !this.killSigReceived) {
const jobs = await this.poll(client);
if (!jobs.length) {
await new Promise(resolve => setTimeout(resolve, this.options.sleep));
continue;
}
this.jobInProgress = true;
for (const job of jobs) {
await this.handleJob(runner, job);
}
this.jobInProgress = false;
}
}
async handleJob(runner: JobRunner, job: DriverJob): Promise<void> {
const now = Date.now();
const message = this.fetchMessage(job);
this.emitEvent(new JobProcessing(message, job));
const { status, error } = await runner.run(message);
await this.handleStatus(status, message, job, error, now);
}
emitEvent(event: EmitsEvent) {
event.emit();
}
async handleStatus(
status: JobStatusEnum,
message: InternalMessage,
job: DriverJob,
error: Error,
startTime: number,
): Promise<void> {
if (status === JobStatusEnum.jobNotFound) {
this.logWarn(
`Job [${message.job}] not found. Please ensure that you have a job running for this connection`,
true,
);
return;
}
if (status === JobStatusEnum.success) {
await this.success(message, job);
this.logSuccess(
`[${message.job}] Job Processed ... ${logTime(Date.now() - startTime)}`,
true,
);
return;
}
if (status === JobStatusEnum.retry) {
await this.retry(message, job);
this.logError(
`[${message.job}] Job Failed... | ${error['message']}`,
true,
);
}
}
private async performScheduledTask(connection: PollQueueDriver) {
if (!connection || !connection.scheduledTask) return;
setInterval(
async () =>
connection.scheduledTask
? await connection.scheduledTask(this.options)
: null,
this.options.schedulerInterval || 30000,
);
}
async purge(): Promise<void> {
const { client } = QueueService.makeDriver<PollQueueDriver>(
this.options.connection,
);
await client.purge({ queue: this.options.queue });
}
async count(): Promise<number> {
const { client } = QueueService.makeDriver<PollQueueDriver>(
this.options.connection,
);
return await client.count({ queue: this.options.queue });
}
/**
* Job processed succesfully method
* @param message
* @param job
*/
async success(message: InternalMessage, job: DriverJob): Promise<void> {
await this.removeJobFromQueue(job);
this.emitEvent(new JobProcessed(message, job));
}
/**
* Retry job after it has failed
* @param message
* @param job
*/
async retry(message: InternalMessage, job: DriverJob): Promise<void> {
await this.removeJobFromQueue(job);
message.attemptCount += 1;
if (+message.attemptCount === message.tries) {
this.logError(`[${message.job}] exceeded retries...`);
this.emitEvent(new JobMaxRetriesExceeed(message, job));
return;
}
await Dispatch({ ...message, delay: message.netDelayInSeconds });
this.emitEvent(new JobFailed(message, job));
}
/**
* Remove job from the queue method
* @param job
*/
async removeJobFromQueue(job: DriverJob): Promise<void> {
const { client } = QueueService.makeDriver<PollQueueDriver>(
this.options.connection,
);
await client.remove(job, this.options);
}
/**
* Fetch message out of the driver message
* @param job
*/
fetchMessage(job: DriverJob): InternalMessage {
const message = job.getMessage();
return Obj.isObj(message)
? (message as unknown as InternalMessage)
: JSON.parse(job.getMessage());
}
attachDeamonListeners() {
process.on('SIGINT', async () => {
await this.closeConnections();
});
process.on('SIGQUIT', async () => {
await this.closeConnections();
});
process.on('SIGTERM', async () => {
await this.closeConnections();
});
process.on('message', async (msg: any) => {
if (msg === 'shutdown' || msg.type === 'shutdown') {
await this.closeConnections();
}
});
}
async closeConnections() {
this.killSigReceived = true;
// Wait for job completion with timeout
const maxWaitTime = 30000; // 30 seconds timeout
const startTime = Date.now();
while (this.jobInProgress) {
this.logInfo('Waiting for current batch to be completed first...');
if (Date.now() - startTime > maxWaitTime) {
break;
}
await new Promise(resolve => setImmediate(resolve)); // Check every second
}
try {
console.log(
`Successfully disconnected broker: ${this.options.connection}`,
);
} catch (error) {
console.error(
`Error disconnecting broker ${this.options.connection}:`,
error,
);
}
process.exit(this.jobInProgress ? 1 : 0);
}
}