-
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.
- Loading branch information
1 parent
e487987
commit 20d20db
Showing
9 changed files
with
627 additions
and
27 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,46 @@ | ||
const workerpool = require(".."); | ||
|
||
var workerCount = 0; | ||
|
||
// create a worker pool | ||
const pool = workerpool.pool(__dirname + "/workers/cleanupAbort.js", { | ||
// maximum time to wait for worker to cleanup it's resources | ||
// on termination before forcefully stopping the worker | ||
workerTerminateTimeout: 1000, | ||
onCreateWorker: (args) => { | ||
console.log("New worker created"); | ||
workerCount += 1; | ||
} | ||
}); | ||
|
||
function add (a, b) { | ||
return a + b; | ||
} | ||
|
||
const main = async () => { | ||
const cleanedUpTask = pool.exec('asyncTimeout', []).timeout(1_000).catch((err) => { | ||
console.log("task timeout"); | ||
console.log("timeout occured: ", err.message); | ||
console.log("worker count ", workerCount); | ||
return pool.exec(add, [1, 2]).then((sum) => { | ||
console.log('add result', sum); | ||
console.log("worker count: ", workerCount); | ||
}); | ||
}); | ||
await cleanedUpTask; | ||
|
||
const canceledTask = pool.exec('asyncAbortHandlerNeverResolves').cancel().catch((err) => { | ||
console.log("task canceled"); | ||
console.log("cancel occured: ", err.message); | ||
console.log("worker count ", workerCount); | ||
return pool.exec(add, [1, 2]).then((sum) => { | ||
console.log('add result', sum); | ||
console.log("worker count: ", workerCount); | ||
}); | ||
}); | ||
|
||
await canceledTask; | ||
} | ||
|
||
|
||
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,55 @@ | ||
var workerpool = require("../.."); | ||
|
||
function asyncTimeout() { | ||
var me = this; | ||
return new Promise(function (resolve) { | ||
let timeout = setTimeout(() => { | ||
resolve(); | ||
}, 5000); | ||
|
||
// An abort listener allows for cleanup for a given worker | ||
// such that it may be resused for future tasks | ||
// if an execption is thrown within scope of the handler | ||
// the worker instance will be destroyed. | ||
me.worker.addAbortListener(async function () { | ||
clearTimeout(timeout); | ||
resolve(); | ||
}); | ||
}); | ||
} | ||
|
||
function asyncAbortHandlerNeverResolves() { | ||
var me = this; | ||
return new Promise((resolve) => { | ||
let timeout = setTimeout(() => { | ||
resolve(); | ||
}, 5000); | ||
|
||
// An abort listener allows for cleanup for a given worker | ||
// such that it may be resused for future tasks | ||
// if an execption is thrown within scope of the handler | ||
// the worker instance will be destroyed. | ||
me.worker.addAbortListener(function () { | ||
clearTimeout(timeout); | ||
return new Promise((res) => { | ||
setTimeout(() => { | ||
res(); | ||
resolve(); | ||
// set the timeout high so it will not resolve before the external | ||
// timeout triggers and exits the worker | ||
}, 1_000_000_000); | ||
}); | ||
}); | ||
}); | ||
} | ||
|
||
// create a worker and register public functions | ||
workerpool.worker( | ||
{ | ||
asyncTimeout: asyncTimeout, | ||
asyncAbortHandlerNeverResolves: asyncAbortHandlerNeverResolves, | ||
}, | ||
{ | ||
abortListenerTimeout: 1000 | ||
} | ||
); |
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
Oops, something went wrong.