Skip to content

Commit

Permalink
Enforce cleanup termination in certain cases
Browse files Browse the repository at this point in the history
  • Loading branch information
d3xter666 committed Nov 10, 2023
1 parent dacc9e4 commit 7e8aa36
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
12 changes: 9 additions & 3 deletions lib/processors/minifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,28 @@ function getPool(taskUtil) {
workerType: "auto",
maxWorkers
});
taskUtil.registerCleanupTask(() => {
taskUtil.registerCleanupTask((options) => {
const attemptPoolTermination = () => {
log.verbose(`Attempt to terminate the workerpool...`);

if (!pool) {
return;
}

const forceTerminate = !!options && !!options.exitCode;

// There are many stats that could be used, but these ones seem the most
// convenient. When all the (available) workers are idle, then it's safe to terminate.
const {idleWorkers, totalWorkers} = pool.stats();
if (idleWorkers !== totalWorkers) {
if (idleWorkers !== totalWorkers && !forceTerminate) {
return new Promise((resolve) =>
setTimeout(() => resolve(attemptPoolTermination()), 100) // Retry after a while
);
}

const poolToBeTerminated = pool;
pool = null;
return poolToBeTerminated.terminate();
return poolToBeTerminated.terminate(forceTerminate);
};

return attemptPoolTermination();
Expand Down
12 changes: 9 additions & 3 deletions lib/tasks/buildThemes.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,28 @@ function getPool(taskUtil) {
workerType: "thread",
maxWorkers
});
taskUtil.registerCleanupTask(() => {
taskUtil.registerCleanupTask((options) => {
const attemptPoolTermination = () => {
log.verbose(`Attempt to terminate the workerpool...`);

if (!pool) {
return;
}

const forceTerminate = !!options && !!options.exitCode;

// There are many stats that could be used, but these ones seem the most
// convenient. When all the (available) workers are idle, then it's safe to terminate.
const {idleWorkers, totalWorkers} = pool.stats();
if (idleWorkers !== totalWorkers) {
if (idleWorkers !== totalWorkers && !forceTerminate) {
return new Promise((resolve) =>
setTimeout(() => resolve(attemptPoolTermination()), 100) // Retry after a while
);
}

const poolToBeTerminated = pool;
pool = null;
return poolToBeTerminated.terminate();
return poolToBeTerminated.terminate(forceTerminate);
};

return attemptPoolTermination();
Expand Down

0 comments on commit 7e8aa36

Please sign in to comment.