Skip to content

Commit

Permalink
feat(sandbox): convert wrapJob method as protected for extension
Browse files Browse the repository at this point in the history
  • Loading branch information
roggervalf committed Sep 15, 2023
1 parent 40c69fb commit a3cc9ee
Showing 1 changed file with 63 additions and 64 deletions.
127 changes: 63 additions & 64 deletions src/classes/child-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ export class ChildProcessor {
this.status = ChildStatus.Started;
this.currentJobPromise = (async () => {
try {
const job = wrapJob(jobJson, this.send);
//console.log('el token', token, job.id)
const job = this.wrapJob(jobJson, this.send);
const result = (await this.processor(job, token)) || {};
await this.send({
cmd: ParentCommand.Completed,
Expand Down Expand Up @@ -98,69 +97,69 @@ export class ChildProcessor {
process.exit(process.exitCode || 0);
}
}
}

/**
* Enhance the given job argument with some functions
* that can be called from the sandboxed job processor.
*
* Note, the `job` argument is a JSON deserialized message
* from the main node process to this forked child process,
* the functions on the original job object are not in tact.
* The wrapped job adds back some of those original functions.
*/
function wrapJob(
job: JobJson,
send: (msg: any) => Promise<void>,
): SandboxedJob {
let progressValue = job.progress;

const updateProgress = async (progress: number | object) => {
// Locally store reference to new progress value
// so that we can return it from this process synchronously.
progressValue = progress;
// Send message to update job progress.
await send({
cmd: ParentCommand.Progress,
value: progress,
});
};
/**
* Enhance the given job argument with some functions
* that can be called from the sandboxed job processor.
*
* Note, the `job` argument is a JSON deserialized message
* from the main node process to this forked child process,
* the functions on the original job object are not in tact.
* The wrapped job adds back some of those original functions.
*/
protected wrapJob(
job: JobJson,
send: (msg: any) => Promise<void>,
): SandboxedJob {
let progressValue = job.progress;

return {
...job,
data: JSON.parse(job.data || '{}'),
opts: job.opts,
returnValue: JSON.parse(job.returnvalue || '{}'),
/*
* Emulate the real job `updateProgress` function, should works as `progress` function.
*/
updateProgress,
/*
* Emulate the real job `log` function.
*/
log: async (row: any) => {
send({
cmd: ParentCommand.Log,
value: row,
});
},
/*
* Emulate the real job `moveToDelayed` function.
*/
moveToDelayed: async (timestamp: number, token?: string) => {
send({
cmd: ParentCommand.MoveToDelayed,
value: { timestamp, token },
});
},
/*
* Emulate the real job `updateData` function.
*/
updateData: async (data: any) => {
send({
cmd: ParentCommand.Update,
value: data,
const updateProgress = async (progress: number | object) => {
// Locally store reference to new progress value
// so that we can return it from this process synchronously.
progressValue = progress;
// Send message to update job progress.
await send({
cmd: ParentCommand.Progress,
value: progress,
});
},
};
};

return {
...job,
data: JSON.parse(job.data || '{}'),
opts: job.opts,
returnValue: JSON.parse(job.returnvalue || '{}'),
/*
* Emulate the real job `updateProgress` function, should works as `progress` function.
*/
updateProgress,
/*
* Emulate the real job `log` function.
*/
log: async (row: any) => {
send({
cmd: ParentCommand.Log,
value: row,
});
},
/*
* Emulate the real job `moveToDelayed` function.
*/
moveToDelayed: async (timestamp: number, token?: string) => {
send({
cmd: ParentCommand.MoveToDelayed,
value: { timestamp, token },
});
},
/*
* Emulate the real job `updateData` function.
*/
updateData: async (data: any) => {
send({
cmd: ParentCommand.Update,
value: data,
});
},
};
}
}

0 comments on commit a3cc9ee

Please sign in to comment.