From 31af818053ace067ee37f66ee11df8467169a597 Mon Sep 17 00:00:00 2001 From: Joe Clark Date: Tue, 16 Apr 2024 14:10:15 +0100 Subject: [PATCH 1/6] engine: restore stout logging on inner thread --- packages/engine-multi/src/worker/pool.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/engine-multi/src/worker/pool.ts b/packages/engine-multi/src/worker/pool.ts index 5e94f05b7..80d5d58da 100644 --- a/packages/engine-multi/src/worker/pool.ts +++ b/packages/engine-multi/src/worker/pool.ts @@ -87,7 +87,14 @@ function createPool(script: string, options: PoolOptions = {}, logger: Logger) { env: options.env || {}, // This pipes the stderr stream onto the child, so we can read it later - stdio: ['ipc', 'ignore', 'pipe'], + stdio: ['ipc', 'pipe', 'pipe'], + }); + + // Note: Ok, now I have visibility on the stdout stream + // I don't think I want to send this to gpc + // This might be strictly local debug + child.stdout!.on('data', (data) => { + console.log(' >>>', data.toString()); }); logger.debug('pool: Created new child process', child.pid); @@ -158,7 +165,6 @@ function createPool(script: string, options: PoolOptions = {}, logger: Logger) { } catch (e) { // do nothing } - reject(new ExitError(code)); finish(worker); } From b2138092ba2bf5801c3c1f8752993dadb6050fa1 Mon Sep 17 00:00:00 2001 From: Joe Clark Date: Tue, 16 Apr 2024 15:25:10 +0100 Subject: [PATCH 2/6] engine: on error, ensure that the pool task rejects properly --- packages/engine-multi/src/api/call-worker.ts | 2 +- packages/engine-multi/src/api/execute.ts | 17 +++++-- packages/engine-multi/src/api/lifecycle.ts | 1 - packages/engine-multi/src/engine.ts | 3 +- packages/engine-multi/src/worker/pool.ts | 6 +-- .../engine-multi/src/worker/thread/helpers.ts | 43 ++++++++-------- packages/engine-multi/test/errors.test.ts | 49 ++++++++++++++++--- .../engine-multi/test/worker/helper.test.ts | 44 ++++++++++++++++- 8 files changed, 126 insertions(+), 39 deletions(-) diff --git a/packages/engine-multi/src/api/call-worker.ts b/packages/engine-multi/src/api/call-worker.ts index 9306b7101..4215091c9 100644 --- a/packages/engine-multi/src/api/call-worker.ts +++ b/packages/engine-multi/src/api/call-worker.ts @@ -51,5 +51,5 @@ export default function initWorkers( const closeWorkers = async (instant?: boolean) => workers.destroy(instant); - return { callWorker, closeWorkers }; + return { callWorker, closeWorkers, workers }; } diff --git a/packages/engine-multi/src/api/execute.ts b/packages/engine-multi/src/api/execute.ts index f86c996ba..dfc713b24 100644 --- a/packages/engine-multi/src/api/execute.ts +++ b/packages/engine-multi/src/api/execute.ts @@ -88,6 +88,7 @@ const execute = async (context: ExecutionContext) => { }); } + let didError = false; const events = { [workerEvents.WORKFLOW_START]: (evt: workerEvents.WorkflowStartEvent) => { workflowStart(context, evt); @@ -112,6 +113,7 @@ const execute = async (context: ExecutionContext) => { }, // TODO this is also untested [workerEvents.ERROR]: (evt: workerEvents.ErrorEvent) => { + didError = true; error(context, { workflowId: state.plan.id, error: evt.error }); }, }; @@ -122,11 +124,16 @@ const execute = async (context: ExecutionContext) => { events, workerOptions ).catch((e: any) => { - // TODO are timeout errors being handled nicely here? - // actually I think the occur outside of here, in the pool - - error(context, { workflowId: state.plan.id, error: e }); - logger.error(`Critical error thrown by ${state.plan.id}`, e); + // An error should: + // a) emit an error event (and so be handled by the error() function + // b) reject the task in the pool + // This guard just ensures that error logging is not duplicated + // if both the above are true (as expected), but that there's still some + // fallback handling if the error event wasn't issued + if (!didError) { + error(context, { workflowId: state.plan.id, error: e }); + logger.error(`Critical error thrown by ${state.plan.id}`, e); + } }); } catch (e: any) { if (!e.severity) { diff --git a/packages/engine-multi/src/api/lifecycle.ts b/packages/engine-multi/src/api/lifecycle.ts index 7b66ce6ae..d6b52fd6f 100644 --- a/packages/engine-multi/src/api/lifecycle.ts +++ b/packages/engine-multi/src/api/lifecycle.ts @@ -148,7 +148,6 @@ export const error = ( event: internalEvents.ErrorEvent ) => { const { threadId = '-', error } = event; - context.emit(externalEvents.WORKFLOW_ERROR, { threadId, // @ts-ignore diff --git a/packages/engine-multi/src/engine.ts b/packages/engine-multi/src/engine.ts index ad868a40e..7a3c4596e 100644 --- a/packages/engine-multi/src/engine.ts +++ b/packages/engine-multi/src/engine.ts @@ -121,7 +121,7 @@ const createEngine = async ( const engine = new Engine() as EngineAPI; - const { callWorker, closeWorkers } = initWorkers( + const { callWorker, closeWorkers, workers } = initWorkers( resolvedWorkerPath, { maxWorkers: options.maxWorkers, @@ -239,6 +239,7 @@ const createEngine = async ( execute: executeWrapper, listen, destroy, + workers, }); }; diff --git a/packages/engine-multi/src/worker/pool.ts b/packages/engine-multi/src/worker/pool.ts index 80d5d58da..630a4bd42 100644 --- a/packages/engine-multi/src/worker/pool.ts +++ b/packages/engine-multi/src/worker/pool.ts @@ -93,9 +93,9 @@ function createPool(script: string, options: PoolOptions = {}, logger: Logger) { // Note: Ok, now I have visibility on the stdout stream // I don't think I want to send this to gpc // This might be strictly local debug - child.stdout!.on('data', (data) => { - console.log(' >>>', data.toString()); - }); + // child.stdout!.on('data', (data) => { + // console.log(data.toString()); + // }); logger.debug('pool: Created new child process', child.pid); allWorkers[child.pid!] = child; diff --git a/packages/engine-multi/src/worker/thread/helpers.ts b/packages/engine-multi/src/worker/thread/helpers.ts index 06c924a44..492b2674d 100644 --- a/packages/engine-multi/src/worker/thread/helpers.ts +++ b/packages/engine-multi/src/worker/thread/helpers.ts @@ -1,6 +1,5 @@ // utilities to run inside the worker // This is designed to minimize the amount of code we have to mock - import process from 'node:process'; import stringify from 'fast-safe-stringify'; import createLogger, { SanitizePolicies } from '@openfn/logger'; @@ -66,17 +65,23 @@ export const createLoggers = ( // Execute wrapper function export const execute = async ( workflowId: string, - executeFn: () => Promise | undefined + executeFn: () => Promise | undefined, + publishFn = publish ) => { const handleError = (err: any) => { - publish(workerEvents.ERROR, { + publishFn(workerEvents.ERROR, { // @ts-ignore workflowId, // Map the error out of the thread in a serializable format error: serializeError(err), - stack: err?.stack + stack: err?.stack, // TODO job id maybe }); + + // Explicitly send a reject task error, to ensure the worker closes down + publish(workerEvents.ENGINE_REJECT_TASK, { + error: serializeError(err), + }); }; process.on('exit', (code: number) => { @@ -91,15 +96,19 @@ export const execute = async ( // it'll be ignored (ie the workerEmit call will fail) process.on('uncaughtException', async (err: any) => { // Log this error to local stdout. This won't be sent out of the worker thread. - console.debug(`Uncaught exception in worker thread (workflow ${workflowId} )`) - console.debug(err) - + console.debug( + `Uncaught exception in worker thread (workflow ${workflowId} )` + ); + console.debug(err); + // Also try and log to the workflow's logger try { - console.error(`Uncaught exception in worker thread (workflow ${workflowId} )`) - console.error(err) - } catch(e) { - console.error(`Uncaught exception in worker thread`) + console.error( + `Uncaught exception in worker thread (workflow ${workflowId} )` + ); + console.error(err); + } catch (e) { + console.error(`Uncaught exception in worker thread`); } // For now, we'll write this off as a crash-level generic execution error @@ -107,23 +116,15 @@ export const execute = async ( const e = new ExecutionError(err); e.severity = 'crash'; // Downgrade this to a crash because it's likely not our fault handleError(e); - - // Close down the process just to be 100% sure that all async code stops - // This is in a timeout to give the emitted message time to escape - // There is a TINY WINDOW in which async code can still run and affect the next run - // This should all go away when we replace workerpool - setTimeout(() => { - process.exit(HANDLED_EXIT_CODE); - }, 2); }); - publish(workerEvents.WORKFLOW_START, { + publishFn(workerEvents.WORKFLOW_START, { workflowId, }); try { const result = await executeFn(); - publish(workerEvents.WORKFLOW_COMPLETE, { workflowId, state: result }); + publishFn(workerEvents.WORKFLOW_COMPLETE, { workflowId, state: result }); // For tests return result; diff --git a/packages/engine-multi/test/errors.test.ts b/packages/engine-multi/test/errors.test.ts index 26849678d..8ba54cd20 100644 --- a/packages/engine-multi/test/errors.test.ts +++ b/packages/engine-multi/test/errors.test.ts @@ -3,7 +3,7 @@ import path from 'node:path'; import { createMockLogger } from '@openfn/logger'; import createEngine, { EngineOptions } from '../src/engine'; -import { WORKFLOW_ERROR } from '../src/events'; +import { WORKFLOW_ERROR, WORKFLOW_COMPLETE } from '../src/events'; import type { RuntimeEngine } from '../src/types'; let engine: RuntimeEngine; @@ -138,10 +138,7 @@ test.serial.skip('vm oom error', (t) => { }); }); -// https://github.com/OpenFn/kit/issues/509 -// TODO this passes standalone, but will trigger an exception in the next test -// This should start working again once we spin up the worker thread -test.serial.skip('execution error from async code', (t) => { +test.serial('execution error from async code', (t) => { return new Promise((done) => { const plan = { id: 'e', @@ -153,7 +150,7 @@ test.serial.skip('execution error from async code', (t) => { // In which case it'll be ignored // Also note that the wrapping promise will never resolve expression: `export default [(s) => new Promise((r) => { - setTimeout(() => { throw new Error(\"e1324\"); r() }, 10) + setTimeout(() => { throw new Error(\"err\"); r() }, 10) })]`, }, ], @@ -170,6 +167,46 @@ test.serial.skip('execution error from async code', (t) => { }); }); +test.serial('after uncaught exception, free up the pool', (t) => { + const plan1 = { + id: 'e', + workflow: { + steps: [ + { + expression: `export default [(s) => new Promise((r) => { + setTimeout(() => { throw new Error(\"err\"); r() }, 10) + })]`, + }, + ], + }, + options: {}, + }; + const plan2 = { + id: 'a', + workflow: { + steps: [ + { + expression: `export default [(s) => s]`, + }, + ], + }, + options: {}, + }; + + return new Promise((done) => { + engine.execute(plan1, {}).on(WORKFLOW_ERROR, (evt) => { + t.log('First workflow failed'); + t.is(evt.type, 'ExecutionError'); + t.is(evt.severity, 'crash'); + + engine.execute(plan2, {}).on(WORKFLOW_COMPLETE, () => { + t.log('Second workflow completed'); + done(); + }); + }); + }); +}); + test.serial('emit a crash error on process.exit()', (t) => { return new Promise((done) => { const plan = { diff --git a/packages/engine-multi/test/worker/helper.test.ts b/packages/engine-multi/test/worker/helper.test.ts index 445fac3ed..69ff74502 100644 --- a/packages/engine-multi/test/worker/helper.test.ts +++ b/packages/engine-multi/test/worker/helper.test.ts @@ -1,6 +1,7 @@ import test from 'ava'; +import * as workerEvents from '../../src/worker/events'; -import { createLoggers } from '../../src/worker/thread/helpers'; +import { execute, createLoggers } from '../../src/worker/thread/helpers'; test('createLogger: runtime logger should emit an event on log', (t) => { const message = 'testing1234'; @@ -40,3 +41,44 @@ test('createLogger: runtime logger should emit a nicely serialised error on log' logger.log(message); }); + +test('execute: should call the run function', (t) => { + let didCallRun = false; + + const run = async () => { + // do something + didCallRun = true; + }; + + execute('abc', run); + + t.true(didCallRun); +}); + +test('execute: should publish workflow-start', async (t) => { + let event; + + const publish = (eventName: string, payload: any) => { + if (eventName === workerEvents.WORKFLOW_START) { + event = payload; + } + }; + + await execute('abc', async () => {}, publish); + + t.deepEqual(event, { workflowId: 'abc' }); +}); + +test('execute: should publish workflow-complete', async (t) => { + let event; + + const publish = (eventName: string, payload: any) => { + if (eventName === workerEvents.WORKFLOW_COMPLETE) { + event = payload; + } + }; + + await execute('abc', async () => ({}), publish); + + t.deepEqual(event, { workflowId: 'abc', state: {} }); +}); From 0528519c28502aa93eef3d66b63802fd3a988ecb Mon Sep 17 00:00:00 2001 From: Joe Clark Date: Tue, 16 Apr 2024 15:59:57 +0100 Subject: [PATCH 3/6] tests: integration test for uncaught exception --- .../worker/test/integration.test.ts | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/integration-tests/worker/test/integration.test.ts b/integration-tests/worker/test/integration.test.ts index 3781a3bdf..7c112bcf5 100644 --- a/integration-tests/worker/test/integration.test.ts +++ b/integration-tests/worker/test/integration.test.ts @@ -506,6 +506,68 @@ test.serial("Don't send adaptor logs to stdout", (t) => { }); }); +// https://github.com/OpenFn/kit/pull/668 +// This test relies on a capacity of 1 +test.serial( + 'keep claiming work after a run with an uncaught exception', + (t) => { + return new Promise(async (done) => { + const finished: Record = {}; + + const onComplete = (evt) => { + const id = evt.runId; + finished[id] = true; + + if (id === 'a20') { + t.is(evt.payload.reason, 'crash'); + } + if (id === 'a21') { + t.is(evt.payload.reason, 'success'); + } + + if (finished.a20 && finished.a21) { + t.pass('both runs completed'); + done(); + } + }; + + lightning.on('run:complete', onComplete); + + const body = ` +fn( + () => new Promise(() => { + setTimeout(() => { + throw new Error('uncaught') + }, 1) + }) +) +`; + + lightning.enqueueRun({ + id: 'a20', + jobs: [ + { + id: 'j1', + adaptor: '@openfn/language-common@latest', + body, + }, + ], + }); + + lightning.enqueueRun({ + id: 'a21', + jobs: [ + { + id: 'j2', + adaptor: '@openfn/language-common@latest', + body: 'fn(() => ({ data: { answer: 42} }))', + }, + ], + }); + }); + } +); + test.serial( 'stateful adaptor should create a new client for each attempt', (t) => { From 4ad992b2d0b1d910073eabaee7c71478276ce80c Mon Sep 17 00:00:00 2001 From: Joe Clark Date: Tue, 16 Apr 2024 16:03:13 +0100 Subject: [PATCH 4/6] tests: add another test for process.exit --- .../worker/test/integration.test.ts | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/integration-tests/worker/test/integration.test.ts b/integration-tests/worker/test/integration.test.ts index 7c112bcf5..a979f2f8b 100644 --- a/integration-tests/worker/test/integration.test.ts +++ b/integration-tests/worker/test/integration.test.ts @@ -568,6 +568,65 @@ fn( } ); +// https://github.com/OpenFn/kit/pull/668 +// This test relies on a capacity of 1 +test.serial('keep claiming work after a run with a process.exit', (t) => { + return new Promise(async (done) => { + const finished: Record = {}; + + const onComplete = (evt) => { + const id = evt.runId; + finished[id] = true; + + if (id === 'a20') { + t.is(evt.payload.reason, 'crash'); + } + if (id === 'a21') { + t.is(evt.payload.reason, 'success'); + } + + if (finished.a20 && finished.a21) { + t.pass('both runs completed'); + done(); + } + }; + + lightning.on('run:complete', onComplete); + + const body = ` +fn( + () => new Promise(() => { + setTimeout(() => { + process.exit() + }, 1) + }) +) +`; + + lightning.enqueueRun({ + id: 'a20', + jobs: [ + { + id: 'j1', + adaptor: '@openfn/language-common@latest', + body, + }, + ], + }); + + lightning.enqueueRun({ + id: 'a21', + jobs: [ + { + id: 'j2', + adaptor: '@openfn/language-common@latest', + body: 'fn(() => ({ data: { answer: 42} }))', + }, + ], + }); + }); +}); + test.serial( 'stateful adaptor should create a new client for each attempt', (t) => { From 2c6a59e8957dd778ae7bd184da145c9671dfce31 Mon Sep 17 00:00:00 2001 From: Joe Clark Date: Tue, 16 Apr 2024 16:08:55 +0100 Subject: [PATCH 5/6] version: worker@1.1.5 --- integration-tests/worker/CHANGELOG.md | 9 +++++++++ integration-tests/worker/package.json | 2 +- packages/engine-multi/CHANGELOG.md | 6 ++++++ packages/engine-multi/package.json | 2 +- packages/lightning-mock/CHANGELOG.md | 7 +++++++ packages/lightning-mock/package.json | 2 +- packages/ws-worker/CHANGELOG.md | 7 +++++++ packages/ws-worker/package.json | 2 +- 8 files changed, 33 insertions(+), 4 deletions(-) diff --git a/integration-tests/worker/CHANGELOG.md b/integration-tests/worker/CHANGELOG.md index e306fbc72..17a115a94 100644 --- a/integration-tests/worker/CHANGELOG.md +++ b/integration-tests/worker/CHANGELOG.md @@ -1,5 +1,14 @@ # @openfn/integration-tests-worker +## 1.0.40 + +### Patch Changes + +- Updated dependencies + - @openfn/engine-multi@1.1.5 + - @openfn/lightning-mock@2.0.5 + - @openfn/ws-worker@1.1.5 + ## 1.0.39 ### Patch Changes diff --git a/integration-tests/worker/package.json b/integration-tests/worker/package.json index 23829c1e5..bb65549e5 100644 --- a/integration-tests/worker/package.json +++ b/integration-tests/worker/package.json @@ -1,7 +1,7 @@ { "name": "@openfn/integration-tests-worker", "private": true, - "version": "1.0.39", + "version": "1.0.40", "description": "Lightning WOrker integration tests", "author": "Open Function Group ", "license": "ISC", diff --git a/packages/engine-multi/CHANGELOG.md b/packages/engine-multi/CHANGELOG.md index 2702919c0..d0dc5114d 100644 --- a/packages/engine-multi/CHANGELOG.md +++ b/packages/engine-multi/CHANGELOG.md @@ -1,5 +1,11 @@ # engine-multi +## 1.1.5 + +### Patch Changes + +- Fix an issue where failed steps might not error correctly, stopping the pool from reclaiming the slot + ## 1.1.4 ### Patch Changes diff --git a/packages/engine-multi/package.json b/packages/engine-multi/package.json index 8eeebf2b6..d9f15ceb2 100644 --- a/packages/engine-multi/package.json +++ b/packages/engine-multi/package.json @@ -1,6 +1,6 @@ { "name": "@openfn/engine-multi", - "version": "1.1.4", + "version": "1.1.5", "description": "Multi-process runtime engine", "main": "dist/index.js", "type": "module", diff --git a/packages/lightning-mock/CHANGELOG.md b/packages/lightning-mock/CHANGELOG.md index 605ea2805..80a4e9f25 100644 --- a/packages/lightning-mock/CHANGELOG.md +++ b/packages/lightning-mock/CHANGELOG.md @@ -1,5 +1,12 @@ # @openfn/lightning-mock +## 2.0.5 + +### Patch Changes + +- Updated dependencies + - @openfn/engine-multi@1.1.5 + ## 2.0.4 ### Patch Changes diff --git a/packages/lightning-mock/package.json b/packages/lightning-mock/package.json index 99079beba..8bdc5ab3d 100644 --- a/packages/lightning-mock/package.json +++ b/packages/lightning-mock/package.json @@ -1,6 +1,6 @@ { "name": "@openfn/lightning-mock", - "version": "2.0.4", + "version": "2.0.5", "private": true, "description": "A mock Lightning server", "main": "dist/index.js", diff --git a/packages/ws-worker/CHANGELOG.md b/packages/ws-worker/CHANGELOG.md index 10f2a900f..8c4003924 100644 --- a/packages/ws-worker/CHANGELOG.md +++ b/packages/ws-worker/CHANGELOG.md @@ -1,5 +1,12 @@ # ws-worker +## 1.1.5 + +### Patch Changes + +- Updated dependencies + - @openfn/engine-multi@1.1.5 + ## 1.1.4 ### Patch Changes diff --git a/packages/ws-worker/package.json b/packages/ws-worker/package.json index f9efd0fd5..908bd5ec5 100644 --- a/packages/ws-worker/package.json +++ b/packages/ws-worker/package.json @@ -1,6 +1,6 @@ { "name": "@openfn/ws-worker", - "version": "1.1.4", + "version": "1.1.5", "description": "A Websocket Worker to connect Lightning to a Runtime Engine", "main": "dist/index.js", "type": "module", From 781a75de7785b6464050d6051079c89090c310ec Mon Sep 17 00:00:00 2001 From: Joe Clark Date: Tue, 16 Apr 2024 17:54:51 +0100 Subject: [PATCH 6/6] tests: reorder --- .../worker/test/integration.test.ts | 182 +++++++----------- 1 file changed, 74 insertions(+), 108 deletions(-) diff --git a/integration-tests/worker/test/integration.test.ts b/integration-tests/worker/test/integration.test.ts index a979f2f8b..b55f23614 100644 --- a/integration-tests/worker/test/integration.test.ts +++ b/integration-tests/worker/test/integration.test.ts @@ -403,109 +403,6 @@ test.serial('an OOM error should still call step-complete', (t) => { }); }); -// test.serial('run a job with complex behaviours (initial state, branching)', (t) => { -// const attempt = { -// id: 'a1', -// initialState: 's1 -// jobs: [ -// { -// id: 'j1', -// body: 'const fn = (f) => (state) => f(state); fn(() => ({ data: { answer: 42} }))', -// }, -// ], -// } - -// initLightning(); -// lightning.on('run:complete', (evt) => { -// // This will fetch the final dataclip from the attempt -// const result = lightning.getResult('a1'); -// t.deepEqual(result, { data: { answer: 42 } }); - -// t.pass('completed attempt'); -// done(); -// }); -// initWorker(); - -// lightning.enqueueRun({ -// id: 'a1', -// jobs: [ -// { -// id: 'j1', -// body: 'const fn = (f) => (state) => f(state); fn(() => ({ data: { answer: 42} }))', -// }, -// ], -// }); -// }); -// }); -test.serial("Don't send job logs to stdout", (t) => { - return new Promise(async (done) => { - const attempt = { - id: crypto.randomUUID(), - jobs: [ - { - adaptor: '@openfn/language-common@latest', - body: 'fn((s) => { console.log("@@@"); return s })', - }, - ], - }; - - lightning.once('run:complete', () => { - const jsonLogs = engineLogger._history; - // The engine logger shouldn't print out any job logs - const jobLog = jsonLogs.find((l) => l.name === 'JOB'); - t.falsy(jobLog); - const jobLog2 = jsonLogs.find((l) => l.message[0] === '@@@'); - t.falsy(jobLog2); - - // But it SHOULD log engine stuff - const runtimeLog = jsonLogs.find( - (l) => l.name === 'engine' && l.message[0].match(/complete workflow/i) - ); - t.truthy(runtimeLog); - done(); - }); - - lightning.enqueueRun(attempt); - }); -}); - -test.serial("Don't send adaptor logs to stdout", (t) => { - return new Promise(async (done) => { - // We have to create a new worker with a different repo for this one - await worker.destroy(); - ({ worker, engineLogger } = await createDummyWorker()); - - const message = 've have been expecting you meester bond'; - const attempt = { - id: crypto.randomUUID(), - jobs: [ - { - adaptor: '@openfn/test-adaptor@1.0.0', - body: `import { log } from '@openfn/test-adaptor'; log("${message}")`, - }, - ], - }; - - lightning.once('run:complete', () => { - const jsonLogs = engineLogger._history; - // The engine logger shouldn't print out any adaptor logs - const jobLog = jsonLogs.find((l) => l.name === 'ADA'); - t.falsy(jobLog); - const jobLog2 = jsonLogs.find((l) => l.message[0] === message); - t.falsy(jobLog2); - - // But it SHOULD log engine stuff - const runtimeLog = jsonLogs.find( - (l) => l.name === 'engine' && l.message[0].match(/complete workflow/i) - ); - t.truthy(runtimeLog); - done(); - }); - - lightning.enqueueRun(attempt); - }); -}); - // https://github.com/OpenFn/kit/pull/668 // This test relies on a capacity of 1 test.serial( @@ -578,14 +475,14 @@ test.serial('keep claiming work after a run with a process.exit', (t) => { const id = evt.runId; finished[id] = true; - if (id === 'a20') { + if (id === 'a30') { t.is(evt.payload.reason, 'crash'); } - if (id === 'a21') { + if (id === 'a31') { t.is(evt.payload.reason, 'success'); } - if (finished.a20 && finished.a21) { + if (finished.a30 && finished.a31) { t.pass('both runs completed'); done(); } @@ -604,7 +501,7 @@ fn( `; lightning.enqueueRun({ - id: 'a20', + id: 'a30', jobs: [ { id: 'j1', @@ -615,7 +512,7 @@ fn( }); lightning.enqueueRun({ - id: 'a21', + id: 'a31', jobs: [ { id: 'j2', @@ -627,6 +524,75 @@ fn( }); }); +test.serial("Don't send job logs to stdout", (t) => { + return new Promise(async (done) => { + const attempt = { + id: crypto.randomUUID(), + jobs: [ + { + adaptor: '@openfn/language-common@latest', + body: 'fn((s) => { console.log("@@@"); return s })', + }, + ], + }; + + lightning.once('run:complete', () => { + const jsonLogs = engineLogger._history; + // The engine logger shouldn't print out any job logs + const jobLog = jsonLogs.find((l) => l.name === 'JOB'); + t.falsy(jobLog); + const jobLog2 = jsonLogs.find((l) => l.message[0] === '@@@'); + t.falsy(jobLog2); + + // But it SHOULD log engine stuff + const runtimeLog = jsonLogs.find( + (l) => l.name === 'engine' && l.message[0].match(/complete workflow/i) + ); + t.truthy(runtimeLog); + done(); + }); + + lightning.enqueueRun(attempt); + }); +}); + +test.serial("Don't send adaptor logs to stdout", (t) => { + return new Promise(async (done) => { + // We have to create a new worker with a different repo for this one + await worker.destroy(); + ({ worker, engineLogger } = await createDummyWorker()); + + const message = 've have been expecting you meester bond'; + const attempt = { + id: crypto.randomUUID(), + jobs: [ + { + adaptor: '@openfn/test-adaptor@1.0.0', + body: `import { log } from '@openfn/test-adaptor'; log("${message}")`, + }, + ], + }; + + lightning.once('run:complete', () => { + const jsonLogs = engineLogger._history; + // The engine logger shouldn't print out any adaptor logs + const jobLog = jsonLogs.find((l) => l.name === 'ADA'); + t.falsy(jobLog); + const jobLog2 = jsonLogs.find((l) => l.message[0] === message); + t.falsy(jobLog2); + + // But it SHOULD log engine stuff + const runtimeLog = jsonLogs.find( + (l) => l.name === 'engine' && l.message[0].match(/complete workflow/i) + ); + t.truthy(runtimeLog); + done(); + }); + + lightning.enqueueRun(attempt); + }); +}); + test.serial( 'stateful adaptor should create a new client for each attempt', (t) => {