Skip to content

Commit

Permalink
ok
Browse files Browse the repository at this point in the history
  • Loading branch information
hughcrt committed May 2, 2024
1 parent d1a8241 commit 5d55324
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 35 deletions.
7 changes: 6 additions & 1 deletion packages/backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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) {
Expand Down
29 changes: 29 additions & 0 deletions packages/backend/src/jobs/materializedViews.ts
Original file line number Diff line number Diff line change
@@ -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)
}
}
34 changes: 0 additions & 34 deletions packages/backend/src/utils/cron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
})
Expand Down

0 comments on commit 5d55324

Please sign in to comment.