Files
outline/server/routes/api/cron.ts
Tom Moor 60101c507a Move bulk of webhook logic to plugin (#4866)
* Move bulk of webhook logic to plugin

* Re-enable cleanup task

* cron tasks
2023-02-12 16:28:11 -08:00

52 lines
1.2 KiB
TypeScript

import crypto from "crypto";
import { Context } from "koa";
import Router from "koa-router";
import env from "@server/env";
import { AuthenticationError } from "@server/errors";
import tasks from "@server/queues/tasks";
const router = new Router();
const cronHandler = async (ctx: Context) => {
const { token, limit = 500 } = (ctx.method === "POST"
? ctx.request.body
: ctx.request.query) as {
token?: string;
limit: number;
};
if (!token || typeof token !== "string") {
throw AuthenticationError("Token is required");
}
if (
token.length !== env.UTILS_SECRET.length ||
!crypto.timingSafeEqual(
Buffer.from(env.UTILS_SECRET),
Buffer.from(String(token))
)
) {
throw AuthenticationError("Invalid secret token");
}
for (const name in tasks) {
const TaskClass = tasks[name];
if (TaskClass.cron) {
await TaskClass.schedule({ limit });
}
}
ctx.body = {
success: true,
};
};
router.get("cron.:period", cronHandler);
router.post("cron.:period", cronHandler);
// For backwards compatibility
router.get("utils.gc", cronHandler);
router.post("utils.gc", cronHandler);
export default router;