Skip to content

Commit

Permalink
feat: 使用重试机制,防止提前kill
Browse files Browse the repository at this point in the history
  • Loading branch information
ximing committed Aug 3, 2019
1 parent 77f650e commit e2b6b55
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
19 changes: 19 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mpbuild",
"version": "1.2.7",
"version": "1.2.8",
"description": "",
"main": "src/index.js",
"scripts": {
Expand Down Expand Up @@ -58,6 +58,7 @@
"micromatch": "^3.1.10",
"moment": "^2.18.1",
"p-queue": "^6.0.2",
"p-retry": "^4.1.0",
"tapable": "^1.1.3",
"tsc-watch": "^2.2.1",
"watchpack": "^1.6.0"
Expand Down
21 changes: 19 additions & 2 deletions src/plugin/minifyPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,20 @@
// const jsonminify = require('jsonminify');
// const htmlmin = require('html-minifier');
const workerpool = require('workerpool');
const pRetry = require('p-retry');

const pool = workerpool.pool();

function clearPool() {
const poolStats = pool.stats();
console.log(poolStats);
if (poolStats.busyWorkers > 0) {
throw new Error(`busyWorkers count is ${poolStats.busyWorkers}`);
}
pool.terminate(true);
return 0;
}

function minifyJS(contents) {
const UglifyJS = require('uglify-js');
const result = UglifyJS.minify(contents);
Expand Down Expand Up @@ -77,8 +88,14 @@ module.exports = class MinifyPlugin {
});
mpb.hooks.afterCompile.tapPromise('MinifyPlugin', async () => {
if (!mpb.isWatch) {
console.log(pool.stats());
pool.terminate(true);
await pRetry(clearPool, {
onFailedAttempt: (error) => {
console.log(
`Attempt clear pool ${error.attemptNumber} failed. There are ${error.retriesLeft} retries left.`
);
},
retries: Number.MAX_SAFE_INTEGER
});
}
});
}
Expand Down

0 comments on commit e2b6b55

Please sign in to comment.