Fix(plugin workflow) interval (#1139)

* fix(plugin-workflow): use setTimeout with offset for more accuracy

* fix(plugin-workflow): locale
This commit is contained in:
Junyi 2022-11-24 00:13:47 -08:00 committed by GitHub
parent 70afa8c7e2
commit 62652145c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 13 deletions

View File

@ -119,7 +119,7 @@ calculators.register('concat', {
const calculatorGroups = [ const calculatorGroups = [
{ {
value: 'boolean', value: 'boolean',
title: `{{t("Comparision", { ns: "${NAMESPACE}" })}}`, title: '{{t("Comparision")}}',
}, },
{ {
value: 'number', value: 'number',
@ -318,7 +318,7 @@ export const VariableTypes = {
// calculation: Calculation // calculation: Calculation
}; };
export const VariableTypesContext = React.createContext(null); export const VariableTypesContext = React.createContext({});
export function useVariableTypes() { export function useVariableTypes() {
return React.useContext(VariableTypesContext); return React.useContext(VariableTypesContext);

View File

@ -168,7 +168,7 @@ ScheduleModes.set(SCHEDULE_MODE.COLLECTION_FIELD, {
const timestamp = now.getTime(); const timestamp = now.getTime();
const startTime = getDataOptionTime(data, startsOn); const startTime = getDataOptionTime(data, startsOn);
const endTime = getDataOptionTime(data, endsOn, -1); const endTime = getDataOptionTime(data, endsOn, -1);
if (!startTime && !repeat) { if (!startTime) {
return; return;
} }
if (startTime && startTime > timestamp + this.cacheCycle) { if (startTime && startTime > timestamp + this.cacheCycle) {
@ -225,7 +225,7 @@ ScheduleModes.set(SCHEDULE_MODE.COLLECTION_FIELD, {
// when repeat is number, means repeat after startsOn // when repeat is number, means repeat after startsOn
// (now - startsOn) % repeat <= cacheCycle // (now - startsOn) % repeat <= cacheCycle
if (repeat) { if (repeat) {
const tsFn = DialectTimestampFnMap[db.options.dialect]; const tsFn = DialectTimestampFnMap[db.options.dialect!];
if (typeof repeat === 'number' if (typeof repeat === 'number'
&& repeat > this.cacheCycle && repeat > this.cacheCycle
&& tsFn && 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) { if (typeof repeat === 'number' && tsFn) {
conditions.push(where( conditions.push(where(
fn('MOD', literal(`${Math.round(timestamp / 1000)} - ${tsFn(startsOn.field)}`), Math.round(repeat / 1000)), 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) { function(workflow, now) {
const { mode } = workflow.config; const { mode } = workflow.config;
const modeHandlers = ScheduleModes.get(mode); const modeHandlers = ScheduleModes.get(mode);
if (!modeHandlers) {
return false;
}
return modeHandlers.shouldCache.call(this, workflow, now); return modeHandlers.shouldCache.call(this, workflow, now);
} }
]; ];
@ -392,7 +395,7 @@ export default class ScheduleTrigger extends Trigger {
events = new Map(); events = new Map();
private timer: NodeJS.Timeout = null; private timer: NodeJS.Timeout | null = null;
private cache: Map<number | string, any> = new Map(); private cache: Map<number | string, any> = new Map();
@ -420,13 +423,7 @@ export default class ScheduleTrigger extends Trigger {
// NOTE: assign to this.timer to avoid duplicated initialization // NOTE: assign to this.timer to avoid duplicated initialization
this.timer = setTimeout( this.timer = setTimeout(
() => { this.run,
this.timer = setInterval(this.run, this.interval);
// initially trigger
// this.onTick(now);
},
// NOTE: // NOTE:
// try to align to system time on each second starts, // try to align to system time on each second starts,
// after at least 1 second initialized for anything to get ready. // after at least 1 second initialized for anything to get ready.
@ -437,6 +434,8 @@ export default class ScheduleTrigger extends Trigger {
run = () => { run = () => {
const now = new Date(); const now = new Date();
// 1001 to avoid 999
const nextInterval = 1_001 - now.getMilliseconds();
now.setMilliseconds(0); now.setMilliseconds(0);
// NOTE: trigger `onTick` for high interval jobs which are cached in last 1 min // 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)) { if (!(now.getTime() % this.cacheCycle)) {
this.reload(); this.reload();
} }
this.timer = setTimeout(this.run, nextInterval);
}; };
async onTick(now) { async onTick(now) {
@ -528,6 +529,9 @@ export default class ScheduleTrigger extends Trigger {
async trigger(workflow, date: Date) { async trigger(workflow, date: Date) {
const { mode } = workflow.config; const { mode } = workflow.config;
const modeHandlers = ScheduleModes.get(mode); const modeHandlers = ScheduleModes.get(mode);
if (!modeHandlers) {
return;
}
return modeHandlers.trigger.call(this, workflow, date); return modeHandlers.trigger.call(this, workflow, date);
} }