mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-05-08 06:59:26 +08:00
fix(plugin-workflow): fix schedule trigger conditions and logs (#6042)
* fix(plugin-workflow): fix schedule trigger conditions and logs * fix(plugin-workflow): fix resume dispatch
This commit is contained in:
parent
ab04e65e74
commit
095ee3b23c
@ -371,7 +371,7 @@ export default class PluginWorkflowServer extends Plugin {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
if (duplicated) {
|
if (duplicated) {
|
||||||
logger.warn(`event of workflow ${workflow.id} is duplicated, event will be ignored`);
|
logger.warn(`event of workflow ${workflow.id} is duplicated (${options.eventKey}), event will be ignored`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// `null` means not to trigger
|
// `null` means not to trigger
|
||||||
|
@ -192,6 +192,39 @@ describe('workflow > triggers > schedule > date field mode', () => {
|
|||||||
expect(d2 - 4000).toBe(startTime.getTime());
|
expect(d2 - 4000).toBe(startTime.getTime());
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('starts on post.createdAt and repeat by cron with endsOn by collection field but no field configured', async () => {
|
||||||
|
await sleepToEvenSecond();
|
||||||
|
const startTime = new Date();
|
||||||
|
startTime.setMilliseconds(0);
|
||||||
|
|
||||||
|
const workflow = await WorkflowModel.create({
|
||||||
|
enabled: true,
|
||||||
|
type: 'schedule',
|
||||||
|
config: {
|
||||||
|
mode: 1,
|
||||||
|
collection: 'posts',
|
||||||
|
startsOn: {
|
||||||
|
field: 'createdAt',
|
||||||
|
},
|
||||||
|
repeat: '*/2 * * * * *',
|
||||||
|
endsOn: {},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const post = await PostRepo.create({ values: { title: 't1' } });
|
||||||
|
|
||||||
|
await sleep(5000);
|
||||||
|
|
||||||
|
const executions = await workflow.getExecutions({ order: [['createdAt', 'ASC']] });
|
||||||
|
expect(executions.length).toBe(3);
|
||||||
|
const d0 = Date.parse(executions[0].context.date);
|
||||||
|
expect(d0).toBe(startTime.getTime());
|
||||||
|
const d1 = Date.parse(executions[1].context.date);
|
||||||
|
expect(d1 - 2000).toBe(startTime.getTime());
|
||||||
|
const d2 = Date.parse(executions[2].context.date);
|
||||||
|
expect(d2 - 4000).toBe(startTime.getTime());
|
||||||
|
});
|
||||||
|
|
||||||
it('starts on post.createdAt and repeat by cron with endsOn at certain time', async () => {
|
it('starts on post.createdAt and repeat by cron with endsOn at certain time', async () => {
|
||||||
await sleepToEvenSecond();
|
await sleepToEvenSecond();
|
||||||
const startTime = new Date();
|
const startTime = new Date();
|
||||||
|
@ -142,6 +142,7 @@ export default class ScheduleTrigger {
|
|||||||
|
|
||||||
workflows.forEach(async (workflow) => {
|
workflows.forEach(async (workflow) => {
|
||||||
const records = await this.loadRecordsToSchedule(workflow, now);
|
const records = await this.loadRecordsToSchedule(workflow, now);
|
||||||
|
this.workflow.getLogger(workflow.id).info(`[Schedule on date field] ${records.length} records to schedule`);
|
||||||
records.forEach((record) => {
|
records.forEach((record) => {
|
||||||
const nextTime = this.getRecordNextTime(workflow, record);
|
const nextTime = this.getRecordNextTime(workflow, record);
|
||||||
this.schedule(workflow, record, nextTime, Boolean(nextTime));
|
this.schedule(workflow, record, nextTime, Boolean(nextTime));
|
||||||
@ -157,20 +158,23 @@ export default class ScheduleTrigger {
|
|||||||
// i. endsOn after now -> yes
|
// i. endsOn after now -> yes
|
||||||
// ii. endsOn before now -> no
|
// ii. endsOn before now -> no
|
||||||
async loadRecordsToSchedule(
|
async loadRecordsToSchedule(
|
||||||
{ config: { collection, limit, startsOn, repeat, endsOn }, allExecuted }: WorkflowModel,
|
{ id, config: { collection, limit, startsOn, repeat, endsOn }, allExecuted }: WorkflowModel,
|
||||||
currentDate: Date,
|
currentDate: Date,
|
||||||
) {
|
) {
|
||||||
const { dataSourceManager } = this.workflow.app;
|
const { dataSourceManager } = this.workflow.app;
|
||||||
if (limit && allExecuted >= limit) {
|
if (limit && allExecuted >= limit) {
|
||||||
|
this.workflow.getLogger(id).warn(`[Schedule on date field] limit reached (all executed ${allExecuted})`);
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
if (!startsOn) {
|
if (!startsOn) {
|
||||||
|
this.workflow.getLogger(id).warn(`[Schedule on date field] "startsOn" is not configured`);
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
const timestamp = currentDate.getTime();
|
const timestamp = currentDate.getTime();
|
||||||
|
|
||||||
const startTimestamp = getOnTimestampWithOffset(startsOn, currentDate);
|
const startTimestamp = getOnTimestampWithOffset(startsOn, currentDate);
|
||||||
if (!startTimestamp) {
|
if (!startTimestamp) {
|
||||||
|
this.workflow.getLogger(id).warn(`[Schedule on date field] "startsOn.field" is not configured`);
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -216,21 +220,21 @@ export default class ScheduleTrigger {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (endsOn) {
|
if (endsOn) {
|
||||||
const now = new Date();
|
|
||||||
const endTimestamp = getOnTimestampWithOffset(endsOn, now);
|
|
||||||
if (!endTimestamp) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
if (typeof endsOn === 'string') {
|
if (typeof endsOn === 'string') {
|
||||||
if (endTimestamp <= timestamp) {
|
if (parseDateWithoutMs(endsOn) <= timestamp) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
conditions.push({
|
const endTimestamp = getOnTimestampWithOffset(endsOn, currentDate);
|
||||||
[endsOn.field]: {
|
if (endTimestamp) {
|
||||||
[Op.gte]: new Date(endTimestamp),
|
conditions.push({
|
||||||
},
|
[endsOn.field]: {
|
||||||
});
|
[Op.gte]: new Date(endTimestamp),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
this.workflow.getLogger(id).warn(`[Schedule on date field] "endsOn.field" is not configured`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -240,7 +244,7 @@ export default class ScheduleTrigger {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
this.workflow.getLogger(id).debug(`[Schedule on date field] conditions: `, { conditions });
|
||||||
return model.findAll({
|
return model.findAll({
|
||||||
where: {
|
where: {
|
||||||
[Op.and]: conditions,
|
[Op.and]: conditions,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user