Comparing version 0.0.9 to 0.0.10
@@ -400,2 +400,7 @@ const { Worker } = require('worker_threads'); | ||
this.config.logger.info(`${prefix} sent a message`, { message }); | ||
if (message === 'done') { | ||
this.workers[name].off('message'); | ||
this.workers[name].terminate(); | ||
delete this.workers[name]; | ||
} | ||
}); | ||
@@ -402,0 +407,0 @@ this.workers[name].on('messageerror', (err) => { |
{ | ||
"name": "bree", | ||
"description": "The best job scheduler for Node.js with support for cron, ms, and human-friendly strings. Uses workers and spawns sandboxed processes. Supports async/await, retries, throttling, concurrency, and cancelable promises (graceful shutdown). Simple, fast, and the most lightweight tool for the job. Made for Lad.", | ||
"version": "0.0.9", | ||
"version": "0.0.10", | ||
"author": "Nick Baugh <niftylettuce@gmail.com> (http://niftylettuce.com/)", | ||
@@ -6,0 +6,0 @@ "ava": { |
@@ -21,2 +21,4 @@ # [**bree**](https://github.com/breejs/bree) | ||
* [Interval, Timeout, and Cron Validation](#interval-timeout-and-cron-validation) | ||
* [Writing jobs with Promises and async-await](#writing-jobs-with-promises-and-async-await) | ||
* [Callbacks, Done, and Completion States](#callbacks-done-and-completion-states) | ||
* [Long-running jobs](#long-running-jobs) | ||
@@ -269,2 +271,36 @@ * [Complex timeouts and intervals](#complex-timeouts-and-intervals) | ||
## Writing jobs with Promises and async-await | ||
Until Node LTS has a stable release with top-level async-await support, here is the working alternative: | ||
```js | ||
const { parentPort } = require('worker_threads'); | ||
const delay = require('delay'); | ||
const ms = require('ms'); | ||
(async () => { | ||
// wait for a promise to finish | ||
await delay(ms('10s')); | ||
// signal to parent that the job is done | ||
parentPort.postMessage('done'); | ||
// you could also `process.exit(0);` when done | ||
})(); | ||
``` | ||
## Callbacks, Done, and Completion States | ||
To close out the worker and signal that it is done, you can simply `parentPort.postMessage('done');` OR `process.exit(0)`. | ||
While writing your jobs (which will run in [worker][workers] threads), you should do one of the following: | ||
* Signal to the main thread that the process has completed by sending a "done" message (per the example above in [Writing jobs with Promises and async-await](#writing-jobs-with-promises-and-async-await)) | ||
* Exit the process if there is NOT an error with code `0` (e.g. `process.exit(0);`) | ||
* Throw an error if an error occurs (this will bubble up to the worker event error listener and terminate it) | ||
* Exit the process if there IS an error with code `1` (e.g. `process.exit(1)`) | ||
## Long-running jobs | ||
@@ -271,0 +307,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
41999
565
412