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

test: try getChildrenValues in sandbox #2738

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions src/classes/child-processor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ParentCommand } from '../enums';
import { SandboxedJob } from '../interfaces';
import { ChildCommand, ParentCommand } from '../enums';
import { SandboxedJob, ParentMessage } from '../interfaces';
import { JobJsonSandbox } from '../types';
import { errorToJSON } from '../utils';

Expand Down Expand Up @@ -160,6 +160,43 @@ export class ChildProcessor {
});
wrappedJob.data = 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, reject) => {
msgHandler = async (msg: ParentMessage) => {
switch (msg.cmd) {
case ChildCommand.GetChildrenValues: {
resolve(msg.value);
break;
}
case ChildCommand.GetChildrenValuesError: {
const err = new Error();
Object.assign(err, msg.value);
reject(err);
break;
}
}
};
});

process.on('message', msgHandler);
await send({
cmd: ParentCommand.GetChildrenValues,
});

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

return childrenValues;
},
};

return wrappedJob;
Expand Down
18 changes: 18 additions & 0 deletions src/classes/sandbox.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { errorToJSON } from '../utils';
import { ChildCommand, ParentCommand } from '../enums';
import { ChildMessage } from '../interfaces';
import { ChildPool } from './child-pool';
Expand Down Expand Up @@ -43,6 +44,23 @@ const sandbox = <T, R, N extends string>(
case ParentCommand.Update:
await job.updateData(msg.value);
break;
case ParentCommand.GetChildrenValues: {
try {
const value = await job.getChildrenValues();

await child.send({
cmd: ChildCommand.GetChildrenValues,
value,
});
break;
} catch (error) {
await child.send({
cmd: ChildCommand.GetChildrenValuesError,
value: errorToJSON(error),
});
}
break;
}
}
};

Expand Down
2 changes: 2 additions & 0 deletions src/enums/child-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ export enum ChildCommand {
Init,
Start,
Stop,
GetChildrenValues,
GetChildrenValuesError,
}
1 change: 1 addition & 0 deletions src/enums/parent-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ export enum ParentCommand {
MoveToDelayed,
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 @@ -7,6 +7,7 @@ export interface SandboxedJob<T = any, R = any>
extends Omit<JobJsonSandbox, 'data' | 'opts' | 'returnValue'> {
data: T;
opts: JobsOptions;
getChildrenValues: <CT = any>() => Promise<{ [jobKey: string]: CT }>;
moveToDelayed: (timestamp: number, token?: string) => Promise<void>;
log: (row: any) => void;
updateData: (data: any) => Promise<void>;
Expand Down
10 changes: 10 additions & 0 deletions tests/fixtures/fixture_processor_getChildrenValues.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* A processor file to be used in tests.
*
*/
'use strict';

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

module.exports = function (job) {
return { childResult: 'bar' };
};
53 changes: 53 additions & 0 deletions tests/test_sandboxed_process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,59 @@ function sandboxProcessTests(
await worker.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,
prefix,
drainDelay: 1,
useWorkerThreads,
});

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

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, prefix });
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 move to delayed', async () => {
const processFile =
__dirname + '/fixtures/fixture_processor_move_to_delayed.js';
Expand Down
Loading