From a19a18b1f7285bf51645588564b30a282c830f51 Mon Sep 17 00:00:00 2001 From: Junyi Date: Wed, 22 Feb 2023 18:01:43 +0800 Subject: [PATCH] fix(plugin-workflow): fix customized job status (#1484) --- .../plugins/workflow/src/server/Processor.ts | 2 +- .../src/server/__tests__/Processor.test.ts | 36 +++++++++++++++++++ .../workflow/src/server/__tests__/index.ts | 26 +++++++++++--- 3 files changed, 58 insertions(+), 6 deletions(-) diff --git a/packages/plugins/workflow/src/server/Processor.ts b/packages/plugins/workflow/src/server/Processor.ts index ebbb59a75..00cad8d68 100644 --- a/packages/plugins/workflow/src/server/Processor.ts +++ b/packages/plugins/workflow/src/server/Processor.ts @@ -205,7 +205,7 @@ export default class Processor { } async exit(job: JobModel | null) { - const status = job ? (this.constructor).StatusMap[job.status] : EXECUTION_STATUS.RESOLVED; + const status = job ? (this.constructor).StatusMap[job.status] ?? Math.sign(job.status) : EXECUTION_STATUS.RESOLVED; await this.execution.update({ status }, { transaction: this.transaction }); return null; } diff --git a/packages/plugins/workflow/src/server/__tests__/Processor.test.ts b/packages/plugins/workflow/src/server/__tests__/Processor.test.ts index a751119d8..42c460078 100644 --- a/packages/plugins/workflow/src/server/__tests__/Processor.test.ts +++ b/packages/plugins/workflow/src/server/__tests__/Processor.test.ts @@ -129,6 +129,42 @@ describe('workflow > Processor', () => { expect(status).toEqual(JOB_STATUS.ERROR); expect(result.message).toBe('definite error'); }); + + it('workflow with customized success node', async () => { + await workflow.createNode({ + type: 'customizedSuccess' + }); + + const post = await PostRepo.create({ values: { title: 't1' } }); + + await sleep(500); + + const [execution] = await workflow.getExecutions(); + expect(execution.status).toEqual(EXECUTION_STATUS.RESOLVED); + + const jobs = await execution.getJobs(); + expect(jobs.length).toEqual(1); + const { status, result } = jobs[0].get(); + expect(status).toEqual(100); + }); + + it('workflow with customized error node', async () => { + await workflow.createNode({ + type: 'customizedError' + }); + + const post = await PostRepo.create({ values: { title: 't1' } }); + + await sleep(500); + + const [execution] = await workflow.getExecutions(); + expect(execution.status).toEqual(EXECUTION_STATUS.FAILED); + + const jobs = await execution.getJobs(); + expect(jobs.length).toEqual(1); + const { status, result } = jobs[0].get(); + expect(status).toEqual(-100); + }); }); describe('manual nodes', () => { diff --git a/packages/plugins/workflow/src/server/__tests__/index.ts b/packages/plugins/workflow/src/server/__tests__/index.ts index 7c859096e..f6598d32f 100644 --- a/packages/plugins/workflow/src/server/__tests__/index.ts +++ b/packages/plugins/workflow/src/server/__tests__/index.ts @@ -23,7 +23,7 @@ export async function getApp({ manual, ...options }: MockAppOptions = {}): Promi name: 'workflow', instructions: { echo: { - run(node, { result }, execution) { + run(node, { result }, processor) { return { status: JOB_STATUS.RESOLVED, result, @@ -32,21 +32,37 @@ export async function getApp({ manual, ...options }: MockAppOptions = {}): Promi }, error: { - run(node, input, execution) { + run(node, input, processor) { throw new Error('definite error'); }, }, 'prompt->error': { - run(node, input, execution) { + run(node, input, processor) { return { status: JOB_STATUS.PENDING, }; }, - resume(node, input, execution) { + resume(node, input, processor) { throw new Error('input failed'); } - } + }, + + customizedSuccess: { + run(node, input, processor) { + return { + status: 100 + } + } + }, + + customizedError: { + run(node, input, processor) { + return { + status: -100 + } + } + }, }, functions: { no1: () => 1