From 62652145c9437a4a1943e94b368500c58fd6ca7f Mon Sep 17 00:00:00 2001 From: Junyi Date: Thu, 24 Nov 2022 00:13:47 -0800 Subject: [PATCH] Fix(plugin workflow) interval (#1139) * fix(plugin-workflow): use setTimeout with offset for more accuracy * fix(plugin-workflow): locale --- .../workflow/src/client/calculators.tsx | 4 +-- .../workflow/src/server/triggers/schedule.ts | 26 +++++++++++-------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/packages/plugins/workflow/src/client/calculators.tsx b/packages/plugins/workflow/src/client/calculators.tsx index d8a81f92f..8d137ac84 100644 --- a/packages/plugins/workflow/src/client/calculators.tsx +++ b/packages/plugins/workflow/src/client/calculators.tsx @@ -119,7 +119,7 @@ calculators.register('concat', { const calculatorGroups = [ { value: 'boolean', - title: `{{t("Comparision", { ns: "${NAMESPACE}" })}}`, + title: '{{t("Comparision")}}', }, { value: 'number', @@ -318,7 +318,7 @@ export const VariableTypes = { // calculation: Calculation }; -export const VariableTypesContext = React.createContext(null); +export const VariableTypesContext = React.createContext({}); export function useVariableTypes() { return React.useContext(VariableTypesContext); diff --git a/packages/plugins/workflow/src/server/triggers/schedule.ts b/packages/plugins/workflow/src/server/triggers/schedule.ts index fecd0dea9..75d5c3789 100644 --- a/packages/plugins/workflow/src/server/triggers/schedule.ts +++ b/packages/plugins/workflow/src/server/triggers/schedule.ts @@ -168,7 +168,7 @@ ScheduleModes.set(SCHEDULE_MODE.COLLECTION_FIELD, { const timestamp = now.getTime(); const startTime = getDataOptionTime(data, startsOn); const endTime = getDataOptionTime(data, endsOn, -1); - if (!startTime && !repeat) { + if (!startTime) { return; } if (startTime && startTime > timestamp + this.cacheCycle) { @@ -225,7 +225,7 @@ ScheduleModes.set(SCHEDULE_MODE.COLLECTION_FIELD, { // when repeat is number, means repeat after startsOn // (now - startsOn) % repeat <= cacheCycle if (repeat) { - const tsFn = DialectTimestampFnMap[db.options.dialect]; + const tsFn = DialectTimestampFnMap[db.options.dialect!]; if (typeof repeat === 'number' && repeat > this.cacheCycle && tsFn @@ -295,7 +295,7 @@ ScheduleModes.set(SCHEDULE_MODE.COLLECTION_FIELD, { } }); - const tsFn = DialectTimestampFnMap[this.plugin.app.db.options.dialect]; + const tsFn = DialectTimestampFnMap[this.plugin.app.db.options.dialect!]; if (typeof repeat === 'number' && tsFn) { conditions.push(where( fn('MOD', literal(`${Math.round(timestamp / 1000)} - ${tsFn(startsOn.field)}`), Math.round(repeat / 1000)), @@ -379,6 +379,9 @@ export default class ScheduleTrigger extends Trigger { function(workflow, now) { const { mode } = workflow.config; const modeHandlers = ScheduleModes.get(mode); + if (!modeHandlers) { + return false; + } return modeHandlers.shouldCache.call(this, workflow, now); } ]; @@ -392,7 +395,7 @@ export default class ScheduleTrigger extends Trigger { events = new Map(); - private timer: NodeJS.Timeout = null; + private timer: NodeJS.Timeout | null = null; private cache: Map = new Map(); @@ -420,13 +423,7 @@ export default class ScheduleTrigger extends Trigger { // NOTE: assign to this.timer to avoid duplicated initialization this.timer = setTimeout( - () => { - this.timer = setInterval(this.run, this.interval); - - // initially trigger - // this.onTick(now); - - }, + this.run, // NOTE: // try to align to system time on each second starts, // after at least 1 second initialized for anything to get ready. @@ -437,6 +434,8 @@ export default class ScheduleTrigger extends Trigger { run = () => { const now = new Date(); + // 1001 to avoid 999 + const nextInterval = 1_001 - now.getMilliseconds(); now.setMilliseconds(0); // NOTE: trigger `onTick` for high interval jobs which are cached in last 1 min @@ -446,6 +445,8 @@ export default class ScheduleTrigger extends Trigger { if (!(now.getTime() % this.cacheCycle)) { this.reload(); } + + this.timer = setTimeout(this.run, nextInterval); }; async onTick(now) { @@ -528,6 +529,9 @@ export default class ScheduleTrigger extends Trigger { async trigger(workflow, date: Date) { const { mode } = workflow.config; const modeHandlers = ScheduleModes.get(mode); + if (!modeHandlers) { + return; + } return modeHandlers.trigger.call(this, workflow, date); }