Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sandboxed-job): add support for getChildrenValues #1417

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 43 additions & 1 deletion src/classes/child-processor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { promisify } from 'util';
import { JobJson, ParentCommand, SandboxedJob } from '../interfaces';
import {
ChildCommand,
JobJson,
ParentCommand,
ParentMessage,
SandboxedJob,
} from '../interfaces';
import { childSend } from '../utils';

enum ChildStatus {
Expand Down Expand Up @@ -187,5 +193,41 @@ function wrapJob(job: JobJson): SandboxedJob {
value: data,
});
},
/*
* Emulate the real job `getChildrenValues` function.
*/
getChildrenValues: async <CT = any>(): Promise<{
[jobKey: string]: CT;
}> => {
let msgHandler: any;

const done = new Promise<{
[jobKey: string]: CT;
}>(resolve => {
msgHandler = async (msg: ParentMessage) => {
switch (msg.cmd) {
case ChildCommand.GetChildrenValues: {
resolve(msg.value);
break;
}
}
};
});

process.on('message', msgHandler);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we took a look but no sure if this is the best way to handle this case. We should revisit this pr


try {
childSend(process, {
cmd: ParentCommand.GetChildrenValues,
});

const childrenValues = await done;
process.removeListener('message', msgHandler);

return childrenValues;
} catch (err) {
// TODO: how to handle this error? Is this try/catch needed at all?
}
},
};
}
10 changes: 10 additions & 0 deletions src/classes/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ const sandbox = <T, R, N extends string>(
break;
case ParentCommand.Update:
await job.update(msg.value);
break;
case ParentCommand.GetChildrenValues:
{
const value = await job.getChildrenValues();
await parentSend(child, {
cmd: ChildCommand.GetChildrenValues,
value,
});
}

break;
}
};
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/child-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export enum ChildCommand {
Init,
Start,
Stop,
GetChildrenValues,
}
1 change: 1 addition & 0 deletions src/interfaces/parent-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export enum ParentCommand {
Log,
Progress,
Update,
GetChildrenValues,
}
1 change: 1 addition & 0 deletions src/interfaces/sandboxed-job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ export interface SandboxedJob<T = any, R = any>
updateProgress: (value: object | number) => Promise<void>;
log: (row: any) => void;
update: (data: any) => Promise<void>;
getChildrenValues: <CT = any>() => Promise<{ [jobKey: string]: CT }>;
returnValue: R;
}
11 changes: 11 additions & 0 deletions tests/fixtures/fixture_processor_getChildrenValues.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* A processor file to be used in tests.
*
*/
'use strict';

const delay = require('./delay');

module.exports = async function (job) {
return job.getChildrenValues();
};
11 changes: 11 additions & 0 deletions tests/fixtures/fixture_processor_getChildrenValues_child.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* A processor file to be used in tests.
*
*/
'use strict';

const delay = require('./delay');

module.exports = function (job) {
return { childResult: 'bar' };
};
47 changes: 47 additions & 0 deletions tests/test_sandboxed_process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,53 @@ describe('sandboxed process', () => {
await flow.close();
});

it('can get children values by calling getChildrenValues', async () => {
const childJobId = 'child-job-id';
const childProcessFile =
__dirname + '/fixtures/fixture_processor_getChildrenValues_child.js';
const parentProcessFile =
__dirname + '/fixtures/fixture_processor_getChildrenValues.js';
const parentQueueName = `parent-queue-${v4()}`;

const parentWorker = new Worker(parentQueueName, parentProcessFile, {
connection,
drainDelay: 1,
});

const childWorker = new Worker(queueName, childProcessFile, {
connection,
drainDelay: 1,
});

const parentCompleting = new Promise<void>((resolve, reject) => {
parentWorker.on('completed', async (job: Job, value: any) => {
try {
expect(value).to.be.eql({
[`bull:${queueName}:${childJobId}`]: { childResult: 'bar' },
});
await parentWorker.close();
resolve();
} catch (err) {
await parentWorker.close();
reject(err);
}
});
});

const flow = new FlowProducer({ connection });
await flow.add({
name: 'parent-job',
queueName: parentQueueName,
opts: { jobId: 'job-id' },
children: [{ name: 'child-job', queueName, opts: { jobId: childJobId } }],
});

await parentCompleting;
await parentWorker.close();
await childWorker.close();
await flow.close();
});

it('should process and fail', async () => {
const processFile = __dirname + '/fixtures/fixture_processor_fail.js';

Expand Down