Skip to content

Commit

Permalink
Merge pull request #351 from biothings/fix_348
Browse files Browse the repository at this point in the history
Fix threads terminating before caching complete for multi-edge queries
  • Loading branch information
tokebe authored Nov 8, 2021
2 parents a671ffe + 9c43500 commit 07ca3e6
Showing 1 changed file with 16 additions and 5 deletions.
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

0 comments on commit 07ca3e6

Please sign in to comment.