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:
Tom Moor
2023-04-26 22:14:10 -04:00
parent 217d41332f
commit 4f019b7a99
3 changed files with 27 additions and 1 deletions

View File

@@ -10,7 +10,7 @@
"build:server": "node ./build.js",
"build": "yarn clean && yarn vite:build && yarn build:i18n && yarn build:server",
"start": "node ./build/server/index.js",
"dev": "NODE_ENV=development yarn concurrently -n api,collaboration -c \"blue,magenta\" \"node --inspect=0.0.0.0 build/server/index.js --services=collaboration,websockets,admin,web,worker\"",
"dev": "NODE_ENV=development yarn concurrently -n api,collaboration -c \"blue,magenta\" \"node --inspect=0.0.0.0 build/server/index.js --services=cron,collaboration,websockets,admin,web,worker\"",
"dev:backend": "NODE_ENV=development nodemon --exec \"yarn build:server && yarn dev\" -e js,ts,tsx --ignore build/ --ignore app/ --ignore shared/editor --ignore server/migrations",
"dev:watch": "NODE_ENV=development yarn concurrently -n backend,frontend \"yarn dev:backend\" \"yarn vite:dev\"",
"lint": "eslint app server shared plugins",

24
server/services/cron.ts Normal file
View 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);
}

View File

@@ -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,
};