diff --git a/bun.lockb b/bun.lockb index b468b26..13bca2c 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index cc7cfb2..6dce8c6 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,8 @@ "scripts": { "dev": "bun run --hot src/index.ts", "start": "bun run src/index.ts", - "codante-apis:update-senator-expenses": "bun run src/features/codante-apis/senator-expenses/updater.ts" + "codante-apis:update-senator-expenses": "bun run src/features/codante-apis/senator-expenses/updater.ts", + "scheduler": "bun run src/scheduler/index.ts" }, "dependencies": { "@aws-sdk/client-s3": "^3.623.0", @@ -11,6 +12,7 @@ "@types/fluent-ffmpeg": "^2.1.25", "axios": "^1.7.3", "base62str": "^1.0.10", + "cron": "^3.1.7", "drizzle-orm": "^0.33.0", "fluent-ffmpeg": "^2.1.3", "hono": "^4.5.3", diff --git a/src/features/codante-apis/senator-expenses/expenses/scrapeExpenses.ts b/src/features/codante-apis/senator-expenses/expenses/scrapeExpenses.ts index 98f28af..82621de 100644 --- a/src/features/codante-apis/senator-expenses/expenses/scrapeExpenses.ts +++ b/src/features/codante-apis/senator-expenses/expenses/scrapeExpenses.ts @@ -1,5 +1,5 @@ import { eq } from 'drizzle-orm'; -import { connection, db } from '../db/db'; +import { db } from '../db/db'; import { expenses as expensesTable, senators as senatorsTable, diff --git a/src/scheduler/index.ts b/src/scheduler/index.ts new file mode 100644 index 0000000..fcebb2e --- /dev/null +++ b/src/scheduler/index.ts @@ -0,0 +1,27 @@ +import { CronJob } from 'cron'; +import { exec } from 'child_process'; + +// Schedule the task to run every day at 2am +new CronJob( + '0 2 * * *', + () => { + console.log('Running the npm script every day at 2am'); + runNpmScript('codante-apis:update-senator-expenses'); + }, + null, + true, + 'America/Sao_Paulo' +); + +function runNpmScript(scriptName: string): void { + exec(`npm run ${scriptName}`, (error, stdout, stderr) => { + if (error) { + console.error(`Error executing npm script: ${error.message}`); + return; + } + if (stderr) { + console.error(`npm script stderr: ${stderr}`); + } + console.log(`npm script output: ${stdout}`); + }); +}