From a3cc9ee5cb7c6957b0c7a5cf36e9f19332c5c1ae Mon Sep 17 00:00:00 2001 From: roggervalf Date: Thu, 14 Sep 2023 22:20:55 -0500 Subject: [PATCH 1/2] feat(sandbox): convert wrapJob method as protected for extension --- src/classes/child-processor.ts | 127 ++++++++++++++++----------------- 1 file changed, 63 insertions(+), 64 deletions(-) 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, + }); + }, + }; + } } From 1d0341c8f7b8e04953306bccac82e4b6a73a5294 Mon Sep 17 00:00:00 2001 From: roggervalf Date: Thu, 14 Sep 2023 23:00:04 -0500 Subject: [PATCH 2/2] chore: get DelayedError from classes --- tests/fixtures/fixture_processor_move_to_delayed.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/fixtures/fixture_processor_move_to_delayed.js b/tests/fixtures/fixture_processor_move_to_delayed.js index 3a385f8c6d..5115b81978 100644 --- a/tests/fixtures/fixture_processor_move_to_delayed.js +++ b/tests/fixtures/fixture_processor_move_to_delayed.js @@ -4,7 +4,7 @@ */ 'use strict'; -const { DelayedError } = require('../../dist/cjs/classes/delayed-error'); +const { DelayedError } = require('../../dist/cjs/classes'); const delay = require('./delay'); module.exports = function (job, token) {