Skip to content

Commit

Permalink
web: Replace cron endpoint by CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
schneefux committed Mar 26, 2024
1 parent d6e73eb commit c17505c
Show file tree
Hide file tree
Showing 4 changed files with 223 additions and 132 deletions.
42 changes: 42 additions & 0 deletions web/api/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import 'dotenv/config'
import { Command } from 'commander'
import { updateAllProfiles } from './routes/player'
import { updateAllReports, updateReport } from './routes/report'

const program = new Command()

program
.command('update-profiles')
.description('Update tracked profiles')
.action(async () => {
console.time('profiles update')
const summary = await updateAllProfiles()
console.timeEnd('profiles update')
console.log(summary)
})

program
.command('update-reports')
.description('Update AI reports')
.action(async () => {
console.time('reports update')
const summary = await updateAllReports()
console.timeEnd('reports update');
console.log(summary)
})

program
.command('update-report')
.description('Update a report')
.option('-l, --locale <locale>', 'Locale')
.option('-m, --mode <mode>', 'Mode')
.option('-p, --map <map>', 'Map')
.action(async (options) => {
console.time('report update')
const { locale, mode, map } = options
const summary = await updateReport(locale, mode, map)
console.timeEnd('report update')
console.log(summary)
})

program.parse(process.argv)
44 changes: 2 additions & 42 deletions web/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import express from 'express'
import { createExpressMiddleware } from '@trpc/server/adapters/express'
import { playerRouter, updateAllProfiles } from './routes/player'
import { playerRouter } from './routes/player'
import { clubRouter } from './routes/club'
import { createContext } from './context'
import { rankingsRouter } from './routes/rankings'
Expand All @@ -9,7 +9,7 @@ import { router } from './trpc'
import renderRouter from './routes/render'
import klickerRouter from './routes/klicker'
import etag from 'etag'
import { reportRouter, updateAllReports, updateReport } from './routes/report'
import { reportRouter } from './routes/report'

const appRouter = router({
player: playerRouter,
Expand All @@ -23,46 +23,6 @@ export type AppRouter = typeof appRouter

const app = express()

// TODO restrict to localhost
app.post('/cron', async (req, res, next) => {
console.time('running cron jobs')
try {
const summary = await updateAllProfiles()
res.json(summary)
} catch (err) {
console.error(err)
next(err)
}
console.timeEnd('running cron jobs')
})

// TODO move to cron
app.post('/update-reports', async (req, res, next) => {
console.time('reports update')
try {
const summary = await updateAllReports()
res.json(summary)
} catch (err) {
console.error(err)
next(err)
}
console.timeEnd('reports update')
})

// TODO remove, only for testing
app.post('/update-report', express.json(), async (req, res, next) => {
console.time('report update')
try {
const { locale, mode, map } = req.body
const summary = await updateReport(locale as string, mode as string, map as string)
res.json(summary)
} catch (err) {
console.error(err)
next(err)
}
console.timeEnd('report update')
})

app.use('/render', renderRouter)
app.use('/klicker', klickerRouter)

Expand Down
8 changes: 5 additions & 3 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
"dev": "node --inspect ./node_modules/.bin/vite",
"typecheck": "vue-tsc --noEmit",
"build": "vavite",
"knex": "cd ./api/ && node --loader ts-node/esm ../node_modules/.bin/knex",
"knex": "cd ./api/ && tsx ../node_modules/.bin/knex",
"start": "cross-env NODE_ENV=production node ./dist/server/index.mjs",
"start:debug": "cross-env NODE_ENV=production node --inspect ./dist/server/index.mjs"
"start:debug": "cross-env NODE_ENV=production node --inspect ./dist/server/index.mjs",
"cli": "cross-env NODE_ENV=production tsx ./api/cli.ts"
},
"dependencies": {
"@clickhouse/client": "^0.3.0",
Expand All @@ -28,6 +29,7 @@
"@unhead/ssr": "^1.8.20",
"@unhead/vue": "^1.8.20",
"@vitejs/plugin-vue": "^5.0.4",
"commander": "^12.0.0",
"compression": "^1.7.4",
"cookie": "^0.6.0",
"core-js": "^3.36.1",
Expand All @@ -50,6 +52,7 @@
"sirv": "^2.0.4",
"string-similarity": "^4.0.4",
"superjson": "^2.2.1",
"tsx": "^4.7.1",
"typescript": "^5.4.2",
"undici": "^6.9.0",
"vike": "0.4.166",
Expand Down Expand Up @@ -83,7 +86,6 @@
"postcss": "^8.4.37",
"tailwindcss": "^3.4.1",
"ts-jest": "^29.1.2",
"ts-node": "^10.9.2",
"unplugin-vue-components": "^0.26.0",
"vavite": "^4.1.1",
"vite-plugin-pages": "^0.32.0",
Expand Down
Loading

0 comments on commit c17505c

Please sign in to comment.