Skip to content

Commit

Permalink
Merge pull request #89 from ericfennis/introduce-worker-pools
Browse files Browse the repository at this point in the history
Introduce worker pools
  • Loading branch information
Ghustavh97 authored Sep 16, 2023
2 parents 23d4ff8 + ba6fb49 commit 721f865
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cd.release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
if: ${{ steps.release.outputs.release_created }}
- uses: actions/setup-node@v1
with:
node-version: 12
node-version: 18
registry-url: 'https://registry.npmjs.org'
if: ${{ steps.release.outputs.release_created }}
# if you are using Yarn, substitute the command below with `yarn install --frozen-lockfile`
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/ci.coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ jobs:

- uses: actions/checkout@v1

- name: Use Node.js 10.x
- name: Use Node.js 18.x
uses: actions/setup-node@v1
with:
node-version: 10.x
node-version: 18

- name: npm install, npm test, npm run coverage
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci.test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
fail-fast: false
matrix:
os: [windows-latest, ubuntu-latest, macos-latest]
node-version: [10.x, 12.x, 14.x]
node-version: [16.x, 18.x, 20.x]

steps:
- uses: actions/checkout@v2
Expand Down
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
engine-strict=true
147 changes: 141 additions & 6 deletions package-lock.json

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

10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
],
"scripts": {
"debug": "node scripts/debug.js",
"lint": "node_modules/.bin/eslint -- src test ./",
"lint:fix": "node_modules/.bin/eslint --fix src test ./",
"lint": "eslint -- src test ./",
"lint:fix": "eslint --fix src test ./",
"test:only": "nyc mocha test/main.test.js && mocha test/output.test.js",
"test": "nyc mocha test/main.test.js && mocha test/output.test.js && npm run lint",
"coverage": "node_modules/.bin/nyc report --reporter=lcovonly"
"coverage": "nyc report --reporter=lcovonly"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -48,6 +48,7 @@
"oslllo-potrace": "^2.0.1",
"oslllo-svg2": "^2.0.2",
"oslllo-validator": "^3.1.0",
"piscina": "^4.1.0",
"yargs": "^16.2.0"
},
"devDependencies": {
Expand All @@ -62,5 +63,8 @@
"mocha-lcov-reporter": "^1.3.0",
"nyc": "^15.1.0",
"stopwatch-node": "^1.1.0"
},
"engines": {
"node": ">=16.0.0"
}
}
37 changes: 18 additions & 19 deletions src/processor.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
"use strict";

const fs = require("fs");
const path = require("path");
const Svg = require("./svg");
const is = require("oslllo-validator");
const Progress = require("./progress");
const Piscina = require("piscina");

const workerPool = new Piscina({
filename: path.resolve(__dirname, "tracer.js")
});

const Processor = function (fixer) {
this.fixer = fixer;
Expand Down Expand Up @@ -34,14 +37,23 @@ Processor.prototype = {
try {
this.setup();
var svgs = this.source;

const resolution = this.fixer.options.get("traceResolution");

svgs = svgs.map((source) => {
var destination = path.join(this.destination, path.basename(source));

return { source, destination };
return { source, destination, resolution };
});
for (var i = 0; i < svgs.length; i++) {
await this.instance(svgs[i]);
}

const workerPromises = svgs.map(async (svg) => {
await workerPool.run(svg);
// eslint-disable-next-line no-empty-function
this.tick(() => {});
});

await Promise.all(workerPromises);

this.teardown();
resolve(this.fixer);
} catch (err) {
Expand All @@ -68,19 +80,6 @@ Processor.prototype = {
this.progress.stop();
}
},
instance: function ({ source, destination }) {
return new Promise(async (resolve, reject) => {
try {
var resolution = this.fixer.options.get("traceResolution");
var svg = new Svg(source, resolution);
var fixed = await svg.process();
fs.writeFileSync(destination, fixed);
this.tick(() => resolve());
} catch (err) {
reject(err);
}
});
},
};

module.exports = Processor;
10 changes: 10 additions & 0 deletions src/tracer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"use strict";

const fs = require("fs/promises");
const Svg = require("./svg");

module.exports = async ({ source, destination, resolution }) => {
const svg = new Svg(source, resolution);
const fixed = await svg.process();
await fs.writeFile(destination, fixed);
};
2 changes: 1 addition & 1 deletion test/src/test.cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { emptyDir, isEmptyDir } = require(".");
const { exec } = require("child_process");
const { assert, path2 } = require("./helper");

const timeout = 20000;
const timeout = 30000;
var destination = "temp";

if (!fs.existsSync(destination)) {
Expand Down

0 comments on commit 721f865

Please sign in to comment.