-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathworker.ts
224 lines (203 loc) · 8.37 KB
/
worker.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
import { spawn } from 'node:child_process';
import process from 'node:process';
import * as os from 'os';
import pidusage from 'pidusage';
import { Duration } from 'ts-duration';
import { type Logger } from 'winston';
import { Child } from './child.js';
import { setupLogging } from './logger.js';
import { WakaQPing } from './message.js';
import { deserialize } from './serializer.js';
import { WakaQ } from './wakaq.js';
export class WakaQWorker {
public wakaq: WakaQ;
public childWorkerCommand: string;
public childWorkerArgs: string[];
public children: Child[] = [];
private _stopProcessing: boolean = false;
public logger: Logger;
constructor(wakaq: WakaQ, childWorkerCommand: string[]) {
this.wakaq = wakaq;
if (childWorkerCommand.length == 0) throw Error('Missing child worker command.');
this.childWorkerCommand = childWorkerCommand.shift() ?? '';
this.childWorkerArgs = childWorkerCommand;
this.logger = setupLogging(this.wakaq);
this.wakaq.logger = this.logger;
}
async start() {
this.logger.info(`concurrency=${this.wakaq.concurrency}`);
this.logger.info(`soft_timeout=${this.wakaq.softTimeout.seconds}`);
this.logger.info(`hard_timeout=${this.wakaq.hardTimeout.seconds}`);
this.logger.info(`wait_timeout=${this.wakaq.waitTimeout.seconds}`);
this.logger.info(`exclude_queues=${this.wakaq.excludeQueues}`);
this.logger.info(`max_retries=${this.wakaq.maxRetries}`);
this.logger.info(`max_mem_percent=${this.wakaq.maxMemPercent}`);
this.logger.info(`max_tasks_per_worker=${this.wakaq.maxTasksPerWorker}`);
this.logger.info(`worker_log_file=${this.wakaq.workerLogFile}`);
this.logger.info(`scheduler_log_file=${this.wakaq.schedulerLogFile}`);
this.logger.info(`worker_log_level=${this.wakaq.workerLogLevel}`);
this.logger.info(`scheduler_log_level=${this.wakaq.schedulerLogLevel}`);
this.logger.info(`starting ${this.wakaq.concurrency} workers...`);
const _this = this;
process.on('SIGINT', () => _this._onExitParent());
process.on('SIGTERM', () => _this._onExitParent());
process.on('SIGQUIT', () => _this._onExitParent());
// spawn child processes
for (let i = 0; i < this.wakaq.concurrency; i++) {
this._spawnChild();
}
this.logger.info('finished spawning all workers');
try {
await this.wakaq.connect();
const pubsub = await this.wakaq.pubsub();
pubsub.on('message', this._handleBroadcastTask);
await pubsub.subscribe(this.wakaq.broadcastKey, (err) => {
if (err) this.logger.error(`Failed to subscribe to broadcast tasks: ${err.message}`);
});
while (!this._stopProcessing) {
this._respawnMissingChildren();
await this._enqueueReadyEtaTasks();
this._checkChildRuntimes();
await this.wakaq.sleep(Duration.millisecond(500));
}
this.logger.info('shutting down...');
if (this.children.length > 0) {
while (this.children.length > 0) {
this._stop();
await this._checkChildMemoryUsages();
this._checkChildRuntimes();
await this.wakaq.sleep(Duration.millisecond(500));
}
}
} catch (error) {
this.logger.error(error);
this._stop();
} finally {
this.wakaq.disconnect();
}
}
private _spawnChild() {
const t = this;
this.logger.info(`spawning child worker: ${this.childWorkerCommand} ${this.childWorkerArgs.join(' ')}`);
const p = spawn(this.childWorkerCommand, this.childWorkerArgs, {
stdio: [null, null, null, 'ipc'],
// stdio: 'pipe',
});
const child = new Child(this.wakaq, p);
p.on('close', (code: number) => {
t._onChildExited(child, code);
});
p.on('message', (message: string | Buffer) => {
t._onMessageReceivedFromChild(child, message);
});
p.stdout?.on('data', (data: string | Buffer) => {
t._onOutputReceivedFromChild(child, data);
});
p.stderr?.on('data', (data: string | Buffer) => {
t._onOutputReceivedFromChild(child, data);
});
this.children.push(child);
}
private _stop() {
this._stopProcessing = true;
this._stopAllChildren();
}
private _stopAllChildren() {
this.children.forEach((child) => {
child.sigterm();
});
}
private _onExitParent() {
this._stop();
}
private _onChildExited(child: Child, code: number) {
this.logger.debug(`child process ${child.process.pid} exited: ${code}`);
this.children = this.children.filter((c) => c !== child);
}
private _onOutputReceivedFromChild(child: Child, data: string | Buffer) {
this.logger.debug(`received output from child process ${child.process.pid}`);
if (data instanceof Buffer) data = data.toString();
if (!data) return;
console.log(data);
}
private _onMessageReceivedFromChild(child: Child, message: unknown) {
if (typeof message !== 'object') return;
const payload = message as WakaQPing;
if (payload.type !== 'wakaq-ping') return;
child.lastPing = Math.round(Date.now() / 1000);
this.logger.debug(`received ping from child process ${child.process.pid}`);
const taskName = payload.task;
const task = taskName ? this.wakaq.tasks.get(taskName) : undefined;
const queueName = payload.queue;
const queue = this.wakaq.queues.find((q) => {
return q.name === queueName;
});
child.setTimeouts(this.wakaq, task, queue);
child.softTimeoutReached = false;
}
private async _enqueueReadyEtaTasks() {
await Promise.all(
this.wakaq.queues.map(async (q) => {
const results = await this.wakaq.broker.getetatasks(q.brokerEtaKey, String(Math.round(Date.now() / 1000)));
await Promise.all(
results.map(async (result) => {
const payload = deserialize(result);
const taskName = payload.name;
const args = payload.args;
await this.wakaq.enqueueAtFront(taskName, args, q);
}),
);
}),
);
}
private async _checkChildMemoryUsages() {
if (!this.wakaq.maxMemPercent) return;
const totalMem = os.totalmem();
const percent = ((totalMem - os.freemem()) / totalMem) * 100;
if (percent < this.wakaq.maxMemPercent) return;
const usages = await Promise.all(this.children.map(async (child) => (await pidusage(child.process.pid ?? 0)).memory || 0));
const maxIndex = usages.reduce((iMax, x, i, arr) => (x > (arr[iMax] ?? 0) ? i : iMax), 0);
const child = this.children.at(maxIndex);
if (!child) return;
this.logger.info(`killing child ${child.process.pid} from too much ram usage`);
child.sigterm();
}
private _checkChildRuntimes() {
this.children.forEach((child) => {
const softTimeout = child.softTimeout || this.wakaq.softTimeout;
const hardTimeout = child.hardTimeout || this.wakaq.hardTimeout;
if (softTimeout || hardTimeout) {
const now = Math.round(Date.now() / 1000);
const runtime = Duration.second(now - child.lastPing);
if (hardTimeout && runtime.seconds > hardTimeout.seconds) {
//this.logger.debug(`child process ${child.process.pid} runtime ${runtime} reached hard timeout, sending sigkill`);
this.logger.info(`child process ${child.process.pid} runtime ${runtime} reached hard timeout, sending sigkill`);
child.sigkill();
} else if (!child.softTimeoutReached && softTimeout && runtime.seconds > softTimeout.seconds) {
//this.logger.debug(`child process ${child.process.pid} runtime ${runtime} reached soft timeout, sending sigquit`);
this.logger.info(`child process ${child.process.pid} runtime ${runtime} reached soft timeout, sending sigquit`);
child.softTimeoutReached = true;
child.sigquit();
}
}
});
}
private _handleBroadcastTask(channel: string, message: string) {
const child = this.children.at(0);
if (!child) {
this.logger.error(`Unable to run broadcast task because no available child workers: ${message}`);
return;
}
this.logger.debug(`run broadcast task: ${message}`);
child.process.stdin?.write(`${message}\n`, (err) => {
if (err) this.logger.error(`Unable to run broadcast task because writing to child stdin encountered an error: ${err}`);
});
}
private _respawnMissingChildren() {
if (this._stopProcessing) return;
for (let i = this.wakaq.concurrency - this.children.length; i > 0; i--) {
this.logger.debug('restarting a crashed worker');
this._spawnChild();
}
}
}