refactor(plugin-workflow): improve code of schedule trigger (#6589)

This commit is contained in:
Junyi 2025-04-01 08:34:06 +08:00 committed by GitHub
parent 4ef6b9037d
commit ce28c68018
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 83 additions and 69 deletions

View File

@ -42,7 +42,7 @@ function getRepeatTypeValue(v) {
return 'none';
}
function CommonRepeatField({ value, onChange }) {
function CommonRepeatField({ value, onChange, disabled }) {
const { t } = useWorkflowTranslation();
const option = getNumberOption(value);
@ -59,11 +59,12 @@ function CommonRepeatField({ value, onChange }) {
addonBefore={t('Every')}
addonAfter={t(option.unitText)}
className="auto-width"
disabled={disabled}
/>
);
}
export function RepeatField({ value = null, onChange }) {
export function RepeatField({ value = null, onChange, disabled }) {
const { t } = useWorkflowTranslation();
const typeValue = getRepeatTypeValue(value);
const onTypeChange = useCallback(
@ -114,20 +115,23 @@ export function RepeatField({ value = null, onChange }) {
}
`}
>
<Select value={typeValue} onChange={onTypeChange} className="auto-width">
<Select value={typeValue} onChange={onTypeChange} className="auto-width" disabled={disabled}>
{RepeatOptions.map((item) => (
<Select.Option key={item.value} value={item.value}>
{t(item.text)}
</Select.Option>
))}
</Select>
{typeof typeValue === 'number' ? <CommonRepeatField value={value} onChange={onChange} /> : null}
{typeof typeValue === 'number' ? (
<CommonRepeatField value={value} onChange={onChange} disabled={disabled} />
) : null}
{typeValue === 'cron' ? (
<Cron
value={value.trim().split(/\s+/).slice(1).join(' ')}
setValue={(v) => onChange(`0 ${v}`)}
clearButton={false}
locale={window['cronLocale']}
disabled={disabled}
/>
) : null}
</fieldset>

View File

@ -366,11 +366,16 @@ export default class PluginWorkflowServer extends Plugin {
const prev = workflow.previous();
if (prev.config) {
trigger.off({ ...workflow.get(), ...prev });
this.getLogger(workflow.id).info(`toggle OFF workflow ${workflow.id} based on configuration before updated`);
}
trigger.on(workflow);
this.getLogger(workflow.id).info(`toggle ON workflow ${workflow.id}`);
this.enabledCache.set(workflow.id, workflow);
} else {
trigger.off(workflow);
this.getLogger(workflow.id).info(`toggle OFF workflow ${workflow.id}`);
this.enabledCache.delete(workflow.id);
}
if (!silent) {

View File

@ -104,8 +104,7 @@ export default class DateFieldScheduleTrigger {
// caching workflows in range, default to 5min
cacheCycle = 300_000;
constructor(public workflow: Plugin) {
workflow.app.on('afterStart', async () => {
onAfterStart = () => {
if (this.timer) {
return;
}
@ -113,9 +112,9 @@ export default class DateFieldScheduleTrigger {
this.timer = setInterval(() => this.reload(), this.cacheCycle);
this.reload();
});
};
workflow.app.on('beforeStop', () => {
onBeforeStop = () => {
if (this.timer) {
clearInterval(this.timer);
}
@ -124,31 +123,36 @@ export default class DateFieldScheduleTrigger {
clearTimeout(timer);
this.cache.delete(key);
}
});
};
constructor(public workflow: Plugin) {
workflow.app.on('afterStart', this.onAfterStart);
workflow.app.on('beforeStop', this.onBeforeStop);
}
reload() {
for (const [key, timer] of this.cache.entries()) {
clearTimeout(timer);
this.cache.delete(key);
}
async reload() {
const workflows = Array.from(this.workflow.enabledCache.values()).filter(
(item) => item.type === 'schedule' && item.config.mode === SCHEDULE_MODE.DATE_FIELD,
);
// NOTE: clear cached jobs in last cycle
this.cache = new Map();
this.inspect(workflows);
workflows.forEach((workflow) => {
this.inspect(workflow);
});
}
inspect(workflows: WorkflowModel[]) {
async inspect(workflow: WorkflowModel) {
const now = new Date();
workflows.forEach(async (workflow) => {
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) => {
const nextTime = this.getRecordNextTime(workflow, record);
this.schedule(workflow, record, nextTime, Boolean(nextTime));
});
});
}
// 1. startsOn in range -> yes
@ -233,8 +237,6 @@ export default class DateFieldScheduleTrigger {
[Op.gte]: new Date(endTimestamp),
},
});
} else {
this.workflow.getLogger(id).warn(`[Schedule on date field] "endsOn.field" is not configured`);
}
}
}
@ -367,7 +369,7 @@ export default class DateFieldScheduleTrigger {
}
on(workflow: WorkflowModel) {
this.inspect([workflow]);
this.inspect(workflow);
const { collection } = workflow.config;
const [dataSourceName, collectionName] = parseCollectionName(collection);

View File

@ -18,26 +18,30 @@ const MAX_SAFE_INTERVAL = 2147483647;
export default class StaticScheduleTrigger {
private timers: Map<string, NodeJS.Timeout | null> = new Map();
constructor(public workflow: Plugin) {
workflow.app.on('afterStart', async () => {
onAfterStart = () => {
const workflows = Array.from(this.workflow.enabledCache.values()).filter(
(item) => item.type === 'schedule' && item.config.mode === SCHEDULE_MODE.STATIC,
);
this.inspect(workflows);
workflows.forEach((workflow) => {
this.inspect(workflow);
});
};
workflow.app.on('beforeStop', () => {
onBeforeStop = () => {
for (const timer of this.timers.values()) {
clearInterval(timer);
}
});
};
constructor(public workflow: Plugin) {
workflow.app.on('afterStart', this.onAfterStart);
workflow.app.on('beforeStop', this.onBeforeStop);
}
inspect(workflows: WorkflowModel[]) {
inspect(workflow: WorkflowModel) {
const now = new Date();
workflows.forEach((workflow) => {
const nextTime = this.getNextTime(workflow, now);
if (nextTime) {
this.workflow
@ -47,7 +51,6 @@ export default class StaticScheduleTrigger {
this.workflow.getLogger(workflow.id).info('workflow will not be scheduled');
}
this.schedule(workflow, nextTime, nextTime >= now.getTime());
});
}
getNextTime({ config, allExecuted }: WorkflowModel, currentDate: Date, nextSecond = false) {
@ -130,7 +133,7 @@ export default class StaticScheduleTrigger {
}
on(workflow) {
this.inspect([workflow]);
this.inspect(workflow);
}
off(workflow) {