-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master'
- Loading branch information
Showing
12 changed files
with
264 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
const workerpool = require(".."); | ||
|
||
// create a worker pool | ||
const pool = workerpool.pool(__dirname + "/workers/cleanupWorker.js", { | ||
// cleanup is only supported for threads or processes | ||
workerType: "thread", | ||
// maximum time to wait for worker to cleanup it's resources | ||
// on termination before forcefully stopping the worker | ||
workerTerminateTimeout: 1000, | ||
}); | ||
|
||
const main = async () => { | ||
const timeout = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); | ||
|
||
let c = 0; | ||
const task = () => | ||
pool | ||
.exec("asyncAdd", [c++, 4.1]) | ||
.then(function (result) { | ||
console.log(result); | ||
}) | ||
.catch(function (err) { | ||
console.error(err); | ||
}); | ||
|
||
const tasks = [ | ||
task(), | ||
timeout(50).then(() => task()), | ||
timeout(100).then(() => task()), | ||
]; | ||
|
||
// Will print `Inside worker cleanup finished (code = 0)` three times | ||
await Promise.all(tasks).then(() => pool.terminate()); | ||
}; | ||
|
||
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Example of a worker that cleans up when it is terminated. | ||
|
||
var workerpool = require("../.."); | ||
|
||
function asyncAdd(a, b) { | ||
return new Promise(function (resolve, reject) { | ||
setTimeout(function () { | ||
resolve(a + b); | ||
}, 500); | ||
}); | ||
} | ||
|
||
// create a worker and register public functions | ||
workerpool.worker( | ||
{ | ||
asyncAdd: asyncAdd, | ||
}, | ||
{ | ||
// This function is called when the worker is terminated. | ||
// It can be used to clean up any open connections or resources. | ||
// May return a promise, in such case make sure that pool's option | ||
// `workerTerminateTimeout` is set to a value larger than the time it takes to clean up. | ||
onTerminate: function (code) { | ||
return new Promise(function (resolve, reject) { | ||
setTimeout(function () { | ||
console.log("Inside worker cleanup finished (code = " + code + ")"); | ||
resolve(); | ||
}, 500); | ||
}); | ||
}, | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.