-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
be62cfc
commit 7b521aa
Showing
6 changed files
with
22 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |