Skip to content

Commit

Permalink
scheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
robertotcestari committed Sep 13, 2024
1 parent be62cfc commit 7b521aa
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 46 deletions.
12 changes: 3 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
To install dependencies:
```sh
bun install
```
### Scheduler

- Setup with PM2

To run:
```sh
bun run dev
```

open http://localhost:3000
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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",
Expand Down
15 changes: 11 additions & 4 deletions src/features/codante-apis/senator-expenses/db/db.ts
Original file line number Diff line number Diff line change
@@ -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<typeof drizzle> | 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 };
7 changes: 1 addition & 6 deletions src/features/codante-apis/senator-expenses/updater.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -17,6 +14,4 @@ async function updater() {
await updatePartySummary(YEAR);
console.log('Updating UF summary...');
await updateUFSummary(YEAR);

await connection.end();
}
32 changes: 6 additions & 26 deletions src/scheduler/index.ts
Original file line number Diff line number Diff line change
@@ -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();
});

0 comments on commit 7b521aa

Please sign in to comment.