diff --git a/src/classes/child-processor.ts b/src/classes/child-processor.ts index 97ca5dc94a..1cda8d5ebd 100644 --- a/src/classes/child-processor.ts +++ b/src/classes/child-processor.ts @@ -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, @@ -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, -): 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, + ): 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, + }); + }, + }; + } }