fix(plugin-workflow): fix constant schedule trigger time (#956)

This commit is contained in:
Junyi 2022-10-24 23:45:53 +08:00 committed by chenos
parent f2be42ac62
commit 75ecab4d3e
2 changed files with 25 additions and 12 deletions

View File

@ -159,6 +159,8 @@ export default class WorkflowPlugin extends Plugin {
transaction: transaction.id transaction: transaction.id
}, { transaction }); }, { transaction });
console.log('workflow triggered:', new Date(), workflow.id, execution.id);
const executed = await workflow.countExecutions({ transaction }); const executed = await workflow.countExecutions({ transaction });
// NOTE: not to trigger afterUpdate hook here // NOTE: not to trigger afterUpdate hook here

View File

@ -35,12 +35,17 @@ interface ScheduleMode {
const ScheduleModes = new Map<number, ScheduleMode>(); const ScheduleModes = new Map<number, ScheduleMode>();
function parseDateWithoutMs(date: string) {
return Math.floor(Date.parse(date) / 1000) * 1000;
}
ScheduleModes.set(SCHEDULE_MODE.CONSTANT, { ScheduleModes.set(SCHEDULE_MODE.CONSTANT, {
shouldCache(workflow, now) { shouldCache(workflow, now) {
const { startsOn, endsOn, repeat } = workflow.config; const { startsOn, endsOn, repeat } = workflow.config;
const timestamp = now.getTime(); const timestamp = now.getTime();
const startTime = Date.parse(startsOn); // NOTE: align to second start
const startTime = parseDateWithoutMs(startsOn);
if (!startTime || (startTime > timestamp + this.cacheCycle)) { if (!startTime || (startTime > timestamp + this.cacheCycle)) {
return false; return false;
} }
@ -54,13 +59,13 @@ ScheduleModes.set(SCHEDULE_MODE.CONSTANT, {
} }
if (endsOn) { if (endsOn) {
const endTime = Date.parse(endsOn); const endTime = parseDateWithoutMs(endsOn);
if (!endTime || endTime <= timestamp) { if (!endTime || endTime <= timestamp) {
return false; return false;
} }
} }
} else { } else {
if (startTime < timestamp) { if (startTime <= timestamp) {
return false; return false;
} }
} }
@ -70,7 +75,13 @@ ScheduleModes.set(SCHEDULE_MODE.CONSTANT, {
trigger(workflow, now) { trigger(workflow, now) {
const { startsOn, endsOn, repeat } = workflow.config; const { startsOn, endsOn, repeat } = workflow.config;
const timestamp = now.getTime(); const timestamp = now.getTime();
const startTime = Math.floor(Date.parse(startsOn) / 1000) * 1000; // NOTE: align to second start
const startTime = parseDateWithoutMs(startsOn);
if (!startTime || startTime > timestamp) {
return;
}
if (repeat) { if (repeat) {
if (typeof repeat === 'number') { if (typeof repeat === 'number') {
if (Math.round(timestamp - startTime) % repeat) { if (Math.round(timestamp - startTime) % repeat) {
@ -79,9 +90,9 @@ ScheduleModes.set(SCHEDULE_MODE.CONSTANT, {
} }
if (endsOn) { if (endsOn) {
const endTime = Date.parse(endsOn); const endTime = parseDateWithoutMs(endsOn);
if (!endTime || endTime < timestamp) { if (!endTime || endTime < timestamp) {
return false; return;
} }
} }
} else { } else {
@ -97,7 +108,7 @@ ScheduleModes.set(SCHEDULE_MODE.CONSTANT, {
function getOnTimestampWithOffset(on, now: Date) { function getOnTimestampWithOffset(on, now: Date) {
switch (typeof on) { switch (typeof on) {
case 'string': case 'string':
return Date.parse(on); return parseDateWithoutMs(on);
case 'object': case 'object':
const { field, offset = 0, unit = 1000 } = on; const { field, offset = 0, unit = 1000 } = on;
if (!field) { if (!field) {
@ -115,7 +126,7 @@ function getOnTimestampWithOffset(on, now: Date) {
function getDataOptionTime(data, on, dir = 1) { function getDataOptionTime(data, on, dir = 1) {
switch (typeof on) { switch (typeof on) {
case 'string': case 'string':
const time = Date.parse(on); const time = parseDateWithoutMs(on);
return time ? time : null; return time ? time : null;
case 'object': case 'object':
const { field, offset = 0, unit = 1000 } = on; const { field, offset = 0, unit = 1000 } = on;
@ -327,10 +338,6 @@ ScheduleModes.set(SCHEDULE_MODE.COLLECTION_FIELD, {
} }
}); });
if (instances.length) {
console.log(instances.length, 'rows trigger at', now);
}
instances.forEach(item => { instances.forEach(item => {
this.plugin.trigger(workflow, { this.plugin.trigger(workflow, {
date: now, date: now,
@ -486,6 +493,10 @@ export default class ScheduleTrigger extends Trigger {
workflows.forEach(async (workflow) => { workflows.forEach(async (workflow) => {
const should = await this.shouldCache(workflow, now); const should = await this.shouldCache(workflow, now);
if (should) {
console.log('caching schedule workflow:', workflow.id);
}
this.setCache(workflow, !should); this.setCache(workflow, !should);
}); });
} }