diff --git a/packages/backend/src/index.ts b/packages/backend/src/index.ts index 838c01a5..1b971c67 100644 --- a/packages/backend/src/index.ts +++ b/packages/backend/src/index.ts @@ -17,9 +17,14 @@ import ratelimit from "./utils/ratelimit" import { initSentry, requestHandler, tracingMiddleWare } from "./utils/sentry" import licenseMiddleware from "./utils/license" import config from "./utils/config" +import { startMaterializedViewRefreshJob } from "./jobs/materializedViews" checkDbConnection() setupCronJobs() + +if (process.env.NODE_ENV === "production") { + startMaterializedViewRefreshJob() +} initSentry() const app = new Koa() @@ -36,7 +41,7 @@ app.use(corsMiddleware) app.use(authMiddleware) app.use(ratelimit) -app.use(bodyParser({ jsonLimit: "20mb", textLimit: "20mb" })) +app.use(bodyParser({ jsonLimit: "5mb", textLimit: "5mb" })) app.use(setDefaultBody) if (config.IS_SELF_HOSTED) { diff --git a/packages/backend/src/jobs/materializedViews.ts b/packages/backend/src/jobs/materializedViews.ts new file mode 100644 index 00000000..1a8aae92 --- /dev/null +++ b/packages/backend/src/jobs/materializedViews.ts @@ -0,0 +1,29 @@ +import sql from "../utils/db" +import { sleep } from "../utils/misc" + +export async function startMaterializedViewRefreshJob() { + try { + const views = [ + "model_name_cache", + "tag_cache", + "metadata_cache", + "feedback_cache", + "run_parent_feedback_cache", + ] + + while (true) { + for (const view of views) { + await sql`refresh materialized view concurrently ${sql(view)};`.catch( + (error) => { + console.error(`Error refreshing materialized view: ${view}`) + console.error(error) + }, + ) + } + + await sleep(1000) + } + } catch (error) { + console.error(error) + } +} diff --git a/packages/backend/src/utils/cron.ts b/packages/backend/src/utils/cron.ts index 562334b6..7309db70 100644 --- a/packages/backend/src/utils/cron.ts +++ b/packages/backend/src/utils/cron.ts @@ -5,40 +5,6 @@ import resetUsage from "@/src/jobs/resetUsage" const EVERY_MINUTE = "* * * * *" export function setupCronJobs() { - cron.schedule( - EVERY_MINUTE, - async () => { - const views = [ - "model_name_cache", - "tag_cache", - "metadata_cache", - "feedback_cache", - "run_parent_feedback_cache", - ] - - try { - for (const view of views) { - await sql`refresh materialized view concurrently ${sql(view)};` - } - } catch (error) { - console.error(error) - } - }, - { name: "refresh materialized views" }, - ) - - cron.schedule( - EVERY_MINUTE, - async () => { - try { - await sql`refresh materialized view concurrently metadata_cache;` - } catch (error) { - console.error(error) - } - }, - { name: "refresh model_name_cache" }, - ) - cron.schedule("0 10 * * *", resetUsage, { name: "reset usage", })