Skip to content

Commit

Permalink
feat: add ability to pass custom worker options
Browse files Browse the repository at this point in the history
  • Loading branch information
niftylettuce committed Jul 9, 2020
1 parent f42c2e6 commit ff8c92f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
33 changes: 30 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* [Callbacks, Done, and Completion States](#callbacks-done-and-completion-states)
* [Long-running jobs](#long-running-jobs)
* [Complex timeouts and intervals](#complex-timeouts-and-intervals)
* [Custom Worker Options](#custom-worker-options)
* [Concurrency](#concurrency)
* [Real-world usage](#real-world-usage)
* [Alternatives that are not production-ready](#alternatives-that-are-not-production-ready)
Expand Down Expand Up @@ -101,13 +102,15 @@ const bree = new Bree({

// runs `./jobs/foo-bar.js` on start
{
name: 'foo-bar'
name: 'foo-bar',
timeout: 0
},

// runs `./jobs/some-other-path.js` on start
{
name: 'beep',
path: path.join(__dirname, 'jobs', 'some-other-path')
name: 'beep',
path: path.join(__dirname, 'jobs', 'some-other-path'),
timeout: 0
},

// runs `./jobs/worker-1.js` on the last day of the month
Expand Down Expand Up @@ -197,6 +200,19 @@ const bree = new Bree({
name: 'worker-13',
timeout: 0,
interval: '2m'
},

// runs `./jobs/worker-14.js` on start with custom `new Worker` options (see below)
{
name: 'worker-14',
timeout: 0,
// <https://nodejs.org/api/worker_threads.html#worker_threads_new_worker_filename_options>
worker: {
workerData: {
foo: 'bar',
beep: 'boop'
}
}
}
]
});
Expand Down Expand Up @@ -325,6 +341,17 @@ Since we use [later][], you can pass an instance of `later.schedule` as the `tim
You can also use [dayjs][] to construct dates (e.g. from now or a certain date) to millisecond differences using `dayjs().diff(new Date(), 'milliseconds')`. You would then pass that returned Number value as `timeout` or `interval` as needed.


## Custom Worker Options

You can pass a default worker configuration object as `new Bree({ worker: { ... } });`.

These options are passed to the `options` argument when we internally invoke `new Worker(path, options)`.

Additionally, you can pass custom worker options on a per-job basis through a `worker` property Object on the job definition.

See complete documentation here for options <https://nodejs.org/api/worker_threads.html#worker_thre> (but you usually don't have to modify these).


## Concurrency

We recommend using the following packages in your workers for handling concurrency:
Expand Down
14 changes: 13 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ class Bree {
closeWorkerAfterMs: 0,
// could also be mjs if desired (?)
defaultExtension: 'js',
// default worker options to pass to `new Worker`
// (can be overriden on a per job basis)
// <https://nodejs.org/api/worker_threads.html#worker_threads_new_worker_filename_options>
worker: {},
...config
};

Expand Down Expand Up @@ -391,7 +395,15 @@ class Bree {
);
debug('starting worker', name);
this.workers[name] = new Worker(job.path, {
workerData: { job }
...(this.config.worker ? this.config.worker : {}),
...(job.worker ? job.worker : {}),
workerData: {
job,
...(this.config.worker && this.config.worker.workerData
? this.config.worker.workerData
: {}),
...(job.worker && job.worker.workerData ? job.worker.workerData : {})
}
});
debug('worker started', name);

Expand Down

0 comments on commit ff8c92f

Please sign in to comment.