Skip to content

Commit

Permalink
feat: omit job env in workerData (fixes #171)
Browse files Browse the repository at this point in the history
  • Loading branch information
titanism committed Jun 27, 2022
1 parent c05d17a commit 6c3ad7c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ const {
const buildJob = require('./job-builder');
const validateJob = require('./job-validator');

function omit(obj, props) {
obj = { ...obj };
for (const prop of props) delete obj[prop];
return obj;
}

const debug = debuglog('bree');

class ImportError extends Error {}
Expand Down Expand Up @@ -361,7 +367,10 @@ class Bree extends EventEmitter {
...(this.config.worker ? this.config.worker : {}),
...(job.worker ? job.worker : {}),
workerData: {
job,
job: {
...job,
...(job.worker ? { worker: omit(job.worker, ['env']) } : {})
},
...(this.config.worker && this.config.worker.workerData
? this.config.worker.workerData
: {}),
Expand Down
45 changes: 45 additions & 0 deletions test/issues/issue-171.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const path = require('path');
const { SHARE_ENV } = require('worker_threads');

const test = require('ava');

const Bree = require('../../src');

const root = path.join(__dirname, '../jobs');

test('works with SHARE_ENV using top-level', async (t) => {
const bree = new Bree({
root,
worker: {
env: SHARE_ENV
},
jobs: [{ name: 'long', closeWorkerAfterMs: 2000 }]
});

await t.notThrowsAsync(bree.run('long'));
});

test('works with SHARE_ENV using job-specific', async (t) => {
const bree = new Bree({
root,
jobs: [
{ name: 'long', closeWorkerAfterMs: 2000, worker: { env: SHARE_ENV } }
]
});

await t.notThrowsAsync(bree.run('long'));
});

test('works with SHARE_ENV using both', async (t) => {
const bree = new Bree({
root,
worker: {
env: SHARE_ENV
},
jobs: [
{ name: 'long', closeWorkerAfterMs: 2000, worker: { env: SHARE_ENV } }
]
});

await t.notThrowsAsync(bree.run('long'));
});

0 comments on commit 6c3ad7c

Please sign in to comment.