Skip to content

Commit

Permalink
feat: scheduled run for plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
myuon committed Jan 21, 2023
1 parent a7585ef commit 0bec88e
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions server/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -690,20 +690,22 @@ export const newRouter = (options?: IRouterOptions) => {
await Promise.all(
schedules.map(async (schedule) => {
if (dayjs.unix(schedule.lastExecutedAt).add(30, "m") < dayjs()) {
// run schedule
await Promise.all(
Object.entries(ctx.state.app.plugins).map(
async ([name, plugin]) => {
ctx.log.info(`Running scheduled job: ${name}...`);

await plugin.onScheduledRun(ctx.state.app);
}
)
);
if (schedule.type === "plugin") {
const plugin = Object.entries(ctx.state.app.plugins).find(
([name]) => name === schedule.name
);
if (!plugin) {
return;
}

ctx.log.info(`Running scheduled job: ${plugin[0]}...`);

schedule.lastExecutedAt = dayjs().unix();
await plugin[1].onScheduledRun(ctx.state.app);

await ctx.state.app.jobScheduleRepository.save(schedule);
schedule.lastExecutedAt = dayjs().unix();

await ctx.state.app.jobScheduleRepository.save(schedule);
}
}
})
);
Expand Down

0 comments on commit 0bec88e

Please sign in to comment.