Skip to content

Commit

Permalink
add scheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
robertotcestari committed Sep 13, 2024
1 parent 727a93b commit be62cfc
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
Binary file modified bun.lockb
Binary file not shown.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
"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",
"@aws-sdk/lib-storage": "^3.623.0",
"@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",
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
27 changes: 27 additions & 0 deletions src/scheduler/index.ts
Original file line number Diff line number Diff line change
@@ -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}`);
});
}

0 comments on commit be62cfc

Please sign in to comment.