fix(plugin-workflow): fix collection trigger in async mode after transaction committed (#4994)

This commit is contained in:
Junyi 2024-08-06 08:29:37 +08:00 committed by GitHub
parent d51559f9e2
commit c4acdebcaa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 51 additions and 3 deletions

View File

@ -161,7 +161,7 @@ export default class Processor {
// for uncaught error, set to error
this.logger.error(
`execution (${this.execution.id}) run instruction [${node.type}] for node (${node.id}) failed: `,
{ error: err },
err,
);
job = {
result:

View File

@ -7,7 +7,7 @@
* For more information, please refer to: https://www.nocobase.com/agreement.
*/
import { BelongsToRepository, MockDatabase } from '@nocobase/database';
import { BelongsToRepository, MockDatabase, Op } from '@nocobase/database';
import { getApp, sleep } from '@nocobase/plugin-workflow-test';
import { MockServer } from '@nocobase/test';
@ -652,6 +652,48 @@ describe('workflow > triggers > collection', () => {
});
});
describe('transaction', () => {
it('should trigger after transaction committed', async () => {
const workflow = await WorkflowModel.create({
enabled: true,
type: 'collection',
config: {
mode: 1,
collection: 'posts',
},
});
await workflow.createNode({
type: 'destroy',
config: {
collection: 'posts',
params: {
filter: {
id: {
$not: null,
},
},
},
},
});
await db.sequelize.transaction(async (transaction) => {
const p1 = await PostRepo.create({ values: { title: 't1' }, transaction });
await sleep(50);
const p2 = await PostRepo.create({ values: { title: 't2' }, transaction });
await sleep(50);
});
await sleep(500);
const executions = await workflow.getExecutions();
expect(executions.length).toBe(2);
const posts = await PostRepo.find();
expect(posts.length).toBe(0);
});
});
describe('cycling trigger', () => {
it('trigger should not be triggered more than once in same execution', async () => {
const workflow = await WorkflowModel.create({

View File

@ -113,7 +113,13 @@ async function handler(this: CollectionTrigger, workflow: WorkflowModel, data: M
},
);
} else {
this.workflow.trigger(workflow, { data: json, stack: context?.stack });
if (transaction) {
transaction.afterCommit(() => {
this.workflow.trigger(workflow, { data: json, stack: context?.stack });
});
} else {
this.workflow.trigger(workflow, { data: json, stack: context?.stack });
}
}
}