Skip to content

Commit a11642b

Browse files
fix: Do not kill node worker if working
1 parent 8244950 commit a11642b

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

src/nodeWorker.ts

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ import { Configuration } from './configuration.js';
55
import { getModuleDirname, getProjectDirname } from './getDirname.js';
66
import { Logger } from './logger.js';
77

8-
const workers = new Map<string, Worker>();
8+
interface MyWorker extends Worker {
9+
used?: boolean;
10+
toKill?: boolean;
11+
}
12+
13+
const workers = new Map<string, MyWorker>();
914

1015
/**
1116
* Run the function in a Node.js Worker Thread
@@ -22,7 +27,9 @@ async function runInWorker(input: {
2227
const func = await Configuration.getLambda(input.fuctionRequest.functionId);
2328

2429
return new Promise<void>((resolve, reject) => {
25-
let worker = workers.get(input.fuctionRequest.workerId);
30+
let worker: MyWorker | undefined = workers.get(
31+
input.fuctionRequest.workerId,
32+
);
2633

2734
if (!worker) {
2835
worker = startWorker({
@@ -33,6 +40,8 @@ async function runInWorker(input: {
3340
environment: input.environment,
3441
verbose: Configuration.config.verbose,
3542
});
43+
worker.used = false;
44+
worker.toKill = false;
3645
} else {
3746
Logger.verbose(
3847
`[Function ${input.fuctionRequest.functionId}] [Worker ${input.fuctionRequest.workerId}] Reusing worker`,
@@ -44,11 +53,18 @@ async function runInWorker(input: {
4453
`[Function ${input.fuctionRequest.functionId}] [Worker ${input.fuctionRequest.workerId}] Worker message`,
4554
JSON.stringify(msg),
4655
);
56+
57+
worker.used = false;
4758
if (msg?.errorType) {
4859
reject(msg);
4960
} else {
5061
resolve(msg);
5162
}
63+
64+
if (worker.toKill) {
65+
worker.toKill = false;
66+
void worker.terminate();
67+
}
5268
});
5369
worker.on('error', (err) => {
5470
Logger.error(
@@ -58,6 +74,7 @@ async function runInWorker(input: {
5874
reject(err);
5975
});
6076

77+
worker.used = true;
6178
worker.postMessage({
6279
env: input.fuctionRequest.env,
6380
event: input.fuctionRequest.event,
@@ -118,6 +135,7 @@ function startWorker(input: WorkerRequest) {
118135
);
119136
workers.delete(input.workerId);
120137
});
138+
121139
workers.set(input.workerId, worker);
122140

123141
return worker;
@@ -130,7 +148,18 @@ async function stopAllWorkers() {
130148
Logger.verbose('Stopping all workers');
131149
const promises: Promise<any>[] = [];
132150
for (const worker of workers.values()) {
133-
promises.push(worker.terminate());
151+
if (worker.used) {
152+
worker.toKill = true;
153+
// set timout for 5 minutes and kill the worker if it is still running
154+
setTimeout(() => {
155+
if (worker.toKill) {
156+
worker.toKill = false;
157+
void worker.terminate();
158+
}
159+
}, 300000);
160+
} else {
161+
promises.push(worker.terminate());
162+
}
134163
}
135164
workers.clear();
136165
await Promise.all(promises);

0 commit comments

Comments
 (0)