-
Notifications
You must be signed in to change notification settings - Fork 163
Add oncreated worker #481
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
base: master
Are you sure you want to change the base?
Add oncreated worker #481
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| const workerpool = require(".."); | ||
| const limiter = require("cpulimit"); // need npm install cpulimit | ||
|
|
||
| // create a worker pool | ||
| const pool = workerpool.pool(__dirname + "/workers/cpuIntensiveWorker.js", { | ||
| // cpu limit is supported for process worker | ||
| workerType: "process", | ||
| workerTerminateTimeout: 1000, | ||
| onCreatedWorker: (worker) => { | ||
| const cpuLimitOption = { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, this is a nice example! Can you please change the indentation to 2 spaces? (I know, I should set up a linter) |
||
| limit: 50, | ||
| includeChildren: true, | ||
| pid: worker.worker.pid | ||
| }; | ||
| limiter.createProcessFamily(cpuLimitOption, function(err, processFamily) { | ||
| if(err) { | ||
| console.error('Error:', err.message); | ||
| return; | ||
| } | ||
|
|
||
| limiter.limit(processFamily, cpuLimitOption, function(err) { | ||
| if(err) { | ||
| console.error('Error:', err.message); | ||
| } | ||
| else { | ||
| console.log('Done.'); | ||
| } | ||
| }); | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| const main = async () => { | ||
| try { | ||
| await pool | ||
| .exec("cpuIntensive", []) | ||
| .timeout(10000) | ||
| } catch(err) { | ||
| console.log('Timeout') | ||
| } | ||
| await pool.terminate() | ||
| }; | ||
|
|
||
| main(); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| // Example of a CPU-intensive worker that never shuts down | ||
|
|
||
| var workerpool = require('../..'); | ||
|
|
||
| function cpuIntensive () { | ||
| return new Promise(function (resolve, reject) { | ||
| while(true) { | ||
| process.stdout.write("."); | ||
| for (let i = 0; i < 1e8; i++) {} | ||
| } | ||
| resolve({}) | ||
| }); | ||
| } | ||
|
|
||
| workerpool.worker({ | ||
| cpuIntensive: cpuIntensive, | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -51,6 +51,8 @@ function Pool(script, options) { | |
| /** @readonly */ | ||
| this.onCreateWorker = options.onCreateWorker || (() => null); | ||
| /** @readonly */ | ||
| this.onCreatedWorker = options.onCreatedWorker || (() => null); | ||
| /** @readonly */ | ||
| this.onTerminateWorker = options.onTerminateWorker || (() => null); | ||
|
|
||
| /** @readonly */ | ||
|
|
@@ -421,7 +423,7 @@ Pool.prototype._createWorkerHandler = function () { | |
| script: this.script | ||
| }) || {}; | ||
|
|
||
| return new WorkerHandler(overriddenParams.script || this.script, { | ||
| const worker = new WorkerHandler(overriddenParams.script || this.script, { | ||
| forkArgs: overriddenParams.forkArgs || this.forkArgs, | ||
| forkOpts: overriddenParams.forkOpts || this.forkOpts, | ||
| workerOpts: overriddenParams.workerOpts || this.workerOpts, | ||
|
|
@@ -431,6 +433,8 @@ Pool.prototype._createWorkerHandler = function () { | |
| workerTerminateTimeout: this.workerTerminateTimeout, | ||
| emitStdStreams: this.emitStdStreams, | ||
| }); | ||
| this.onCreatedWorker(worker) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we pass the |
||
| return worker; | ||
| } | ||
|
|
||
| /** | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you write a unit test for the new option please?