From b8a02df7ba527ba7fdb2675dae314936c72d9fc9 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Mon, 16 May 2022 12:44:22 -0700 Subject: [PATCH] chore: utils.gc -> cron.daily (#3543) --- server/routes/api/{utils.test.ts => cron.test.ts} | 4 ++-- server/routes/api/{utils.ts => cron.ts} | 12 +++++++++--- server/routes/api/index.ts | 2 +- 3 files changed, 12 insertions(+), 6 deletions(-) rename server/routes/api/{utils.test.ts => cron.test.ts} (81%) rename server/routes/api/{utils.ts => cron.ts} (75%) diff --git a/server/routes/api/utils.test.ts b/server/routes/api/cron.test.ts similarity index 81% rename from server/routes/api/utils.test.ts rename to server/routes/api/cron.test.ts index 7376e5a53..6c82a4ff8 100644 --- a/server/routes/api/utils.test.ts +++ b/server/routes/api/cron.test.ts @@ -8,9 +8,9 @@ const server = new TestServer(app.callback()); beforeEach(() => flushdb()); afterAll(() => server.close()); -describe("#utils.gc", () => { +describe("#cron.daily", () => { it("should require authentication", async () => { - const res = await server.post("/api/utils.gc"); + const res = await server.post("/api/cron.daily"); expect(res.status).toEqual(401); }); }); diff --git a/server/routes/api/utils.ts b/server/routes/api/cron.ts similarity index 75% rename from server/routes/api/utils.ts rename to server/routes/api/cron.ts index 7e2d2f71d..6e0560cb9 100644 --- a/server/routes/api/utils.ts +++ b/server/routes/api/cron.ts @@ -1,3 +1,4 @@ +import { Context } from "koa"; import Router from "koa-router"; import { AuthenticationError } from "@server/errors"; import CleanupDeletedDocumentsTask from "@server/queues/tasks/CleanupDeletedDocumentsTask"; @@ -7,8 +8,8 @@ import InviteReminderTask from "@server/queues/tasks/InviteReminderTask"; const router = new Router(); -router.post("utils.gc", async (ctx) => { - const { token, limit = 500 } = ctx.body; +const cronHandler = async (ctx: Context) => { + const { token, limit = 500 } = ctx.body as { token?: string; limit: number }; if (process.env.UTILS_SECRET !== token) { throw AuthenticationError("Invalid secret token"); @@ -25,6 +26,11 @@ router.post("utils.gc", async (ctx) => { ctx.body = { success: true, }; -}); +}; + +router.post("cron.:period", cronHandler); + +// For backwards compatibility +router.post("utils.gc", cronHandler); export default router; diff --git a/server/routes/api/index.ts b/server/routes/api/index.ts index f386013bb..9ee309149 100644 --- a/server/routes/api/index.ts +++ b/server/routes/api/index.ts @@ -9,6 +9,7 @@ import attachments from "./attachments"; import auth from "./auth"; import authenticationProviders from "./authenticationProviders"; import collections from "./collections"; +import utils from "./cron"; import documents from "./documents"; import events from "./events"; import fileOperationsRoute from "./fileOperations"; @@ -25,7 +26,6 @@ import shares from "./shares"; import stars from "./stars"; import team from "./team"; import users from "./users"; -import utils from "./utils"; import views from "./views"; const api = new Koa();