Add new cron service, useful in dev to automatically run scheduled tasks and can be used in single-server deployments to avoid an external dependency
This commit is contained in:
24
server/services/cron.ts
Normal file
24
server/services/cron.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { Day, Hour, Second } from "@shared/utils/time";
|
||||
import tasks from "@server/queues/tasks";
|
||||
import { TaskSchedule } from "@server/queues/tasks/BaseTask";
|
||||
|
||||
export default function init() {
|
||||
async function run(schedule: TaskSchedule) {
|
||||
for (const name in tasks) {
|
||||
const TaskClass = tasks[name];
|
||||
if (TaskClass.cron === schedule) {
|
||||
await TaskClass.schedule({ limit: 10000 });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
setInterval(() => run(TaskSchedule.Daily), Day);
|
||||
setInterval(() => run(TaskSchedule.Hourly), Hour);
|
||||
|
||||
// Just give everything time to startup before running the first time. Not
|
||||
// _technically_ required to function.
|
||||
setTimeout(() => {
|
||||
run(TaskSchedule.Daily);
|
||||
run(TaskSchedule.Hourly);
|
||||
}, 30 * Second);
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import admin from "./admin";
|
||||
import collaboration from "./collaboration";
|
||||
import cron from "./cron";
|
||||
import web from "./web";
|
||||
import websockets from "./websockets";
|
||||
import worker from "./worker";
|
||||
@@ -10,4 +11,5 @@ export default {
|
||||
admin,
|
||||
web,
|
||||
worker,
|
||||
cron,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user