From ff8c92f129ad4a4c7b298ffc70980ef756375207 Mon Sep 17 00:00:00 2001 From: niftylettuce Date: Thu, 9 Jul 2020 02:57:15 -0500 Subject: [PATCH] feat: add ability to pass custom worker options --- README.md | 33 ++++++++++++++++++++++++++++++--- index.js | 14 +++++++++++++- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 68b9954..f2612c4 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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 @@ -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, + // + worker: { + workerData: { + foo: 'bar', + beep: 'boop' + } + } } ] }); @@ -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 (but you usually don't have to modify these). + + ## Concurrency We recommend using the following packages in your workers for handling concurrency: diff --git a/index.js b/index.js index 56f9c2e..e72a29e 100644 --- a/index.js +++ b/index.js @@ -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) + // + worker: {}, ...config }; @@ -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);