Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix threads terminating before caching complete for multi-edge queries #351

Merged
merged 2 commits into from
Nov 8, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions src/utils/threadWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,31 @@ module.exports = function runWorker(task) {
const worker = new Worker(path.resolve(__dirname, "../server.js"), {
workerData: { req: task.req, route: task.route },
});
let reqDone, cacheDone = false;
let reqDone = false;
let cacheInProgress = 0;
worker.on("message", (...args) => {
const workerID = worker.threadId;
if (args[0].cacheInProgress) {
cacheInProgress += 1;
}
if (args[0].cacheDone) {
cacheDone = true;
} else {
if (typeof args[0].cacheDone === 'number') {
cacheInProgress -= args[0].cacheDone;
} else {
cacheInProgress = 0;
}
} else if (args[0].msg) {
reqDone = true;
resolve(...args);
}
if (reqDone && cacheDone) {
if (reqDone && cacheInProgress <= 0) {
worker.terminate().then(() => debug(`Worker thread ${workerID} completed task, terminated successfully.`));
}
});
worker.on("error", reject);
worker.on("error", (...args) => {
reqDone = true; // allows caching to finish if any was started.
reject(...args);
});
worker.on("exit", code => {
if (code !== 0) {
reject(new Error(`Worker ${worker.threadId} exited with code ${code}`));
Expand Down