diff --git a/README.md b/README.md index 6dd13e7..56e0212 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,5 @@ -To install dependencies: -```sh -bun install -``` +### Scheduler + +- Setup with PM2 -To run: -```sh -bun run dev -``` -open http://localhost:3000 diff --git a/bun.lockb b/bun.lockb index 13bca2c..1e5a63a 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index 6dce8c6..23f7626 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,6 @@ "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", "scheduler": "bun run src/scheduler/index.ts" }, "dependencies": { @@ -13,6 +12,7 @@ "axios": "^1.7.3", "base62str": "^1.0.10", "cron": "^3.1.7", + "croner": "^8.1.1", "drizzle-orm": "^0.33.0", "fluent-ffmpeg": "^2.1.3", "hono": "^4.5.3", diff --git a/src/features/codante-apis/senator-expenses/db/db.ts b/src/features/codante-apis/senator-expenses/db/db.ts index 4edf750..4e8804c 100644 --- a/src/features/codante-apis/senator-expenses/db/db.ts +++ b/src/features/codante-apis/senator-expenses/db/db.ts @@ -1,13 +1,20 @@ import { drizzle } from 'drizzle-orm/mysql2'; -import * as schema from './schema'; import mysql from 'mysql2/promise'; -const connection = await mysql.createConnection({ +declare global { + var _db: ReturnType | undefined; +} + +const poolConnection = mysql.createPool({ user: process.env.DATABASE_USER_SENATOR_EXPENSES, password: process.env.DATABASE_PASSWORD_SENATOR_EXPENSES, database: process.env.DATABASE_SENATOR_EXPENSES, }); -const db = drizzle(connection, { schema, mode: 'default' }); +const db = globalThis._db || drizzle(poolConnection); + +if (process.env.NODE_ENV !== 'production') { + globalThis._db = db; +} -export { db, connection }; +export { db, poolConnection }; diff --git a/src/features/codante-apis/senator-expenses/updater.ts b/src/features/codante-apis/senator-expenses/updater.ts index 3df42e8..123651f 100644 --- a/src/features/codante-apis/senator-expenses/updater.ts +++ b/src/features/codante-apis/senator-expenses/updater.ts @@ -1,11 +1,8 @@ -import { connection } from './db/db'; import { scrapeExpenses } from './expenses/scrapeExpenses'; import { updatePartySummary } from './summaries/updatePartySummary'; import { updateUFSummary } from './summaries/updateUFSummary'; -await updater(); - -async function updater() { +export async function updater() { const YEAR = 2024; // update expenses @@ -17,6 +14,4 @@ async function updater() { await updatePartySummary(YEAR); console.log('Updating UF summary...'); await updateUFSummary(YEAR); - - await connection.end(); } diff --git a/src/scheduler/index.ts b/src/scheduler/index.ts index fcebb2e..66f3867 100644 --- a/src/scheduler/index.ts +++ b/src/scheduler/index.ts @@ -1,27 +1,7 @@ -import { CronJob } from 'cron'; -import { exec } from 'child_process'; +import { Cron } from 'croner'; +import { updater } from '../features/codante-apis/senator-expenses/updater'; -// 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}`); - }); -} +const job = Cron('0 2 * * *', async () => { + console.log('Running '); + await updater(); +});