Skip to content

Commit

Permalink
Bundling de la CLI avec migrations + cronjobs
Browse files Browse the repository at this point in the history
  • Loading branch information
totakoko committed Sep 9, 2024
1 parent 0e19df2 commit 7c33c2b
Show file tree
Hide file tree
Showing 6 changed files with 657 additions and 46 deletions.
10 changes: 3 additions & 7 deletions knexfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default {
acquireConnectionTimeout: 10000,
migrations: {
tableName: 'knex_migrations',
directory: './src/db/migrations',
directory: process.env.KNEX_MIGRATIONS_DIR ?? './src/db/migrations',
},
pool: {
idleTimeoutMillis: 10000,
Expand All @@ -19,12 +19,8 @@ export default {
},
} satisfies Knex.Config;

function addApplicationName(
connectionString: string | undefined
): string | undefined {
function addApplicationName(connectionString: string | undefined): string | undefined {
return connectionString === undefined
? undefined
: `${connectionString}${
connectionString.includes('?') ? '&' : '?'
}application_name=FCU-API`;
: `${connectionString}${connectionString.includes('?') ? '&' : '?'}application_name=FCU-API`;
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
"talisman:add-exception": "node-talisman -i -g pre-commit"
},
"dependencies": {
"@codegouvfr/react-dsfr": "^1.11.7",
"@betagouv/france-chaleur-urbaine-publicodes": "^0.13.0",
"@codegouvfr/react-dsfr": "^1.11.7",
"@commander-js/extra-typings": "^11.1.0",
"@emotion/react": "^11.11.4",
"@emotion/server": "^11.11.0",
Expand Down Expand Up @@ -160,6 +160,7 @@
"raw-loader": "^4.0.2",
"sinon": "^15.2.0",
"ts-node": "^10.9.2",
"tsup": "^8.2.4",
"tsx": "^4.7.0",
"typescript": "5.1.6",
"vitest": "^1.0.4"
Expand Down
16 changes: 16 additions & 0 deletions scripts/cli.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { readFile } from 'fs/promises';

import { InvalidArgumentError, createCommand } from '@commander-js/extra-typings';
import { knex } from 'knex';

import { logger } from '@helpers/logger';
import knexConfig from 'knexfile';
import { startCronJobs } from 'src/cron_jobs/cron';
import db from 'src/db';
import { DatabaseTileInfo, SourceId, tilesInfo, zSourceId } from 'src/services/tiles.config';

Expand All @@ -24,6 +27,19 @@ program
await db.destroy();
});

program.command('db:migrate').action(async () => {
const db = knex(knexConfig);
const [batchNo, log] = await db.migrate.latest();
if (log.length === 0) {
console.info('Already up to date');
}
console.info(`Batch ${batchNo} run: ${log.length} migrations`);
});

program.command('cronjobs:start').action(async () => {
startCronJobs();
});

program
.command('create-modifications-reseau')
.argument('<airtable_base>', 'Base Airtable', validateKnownAirtableBase)
Expand Down
52 changes: 27 additions & 25 deletions src/cron_jobs/cron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,34 @@ import cron from 'cron';

import { launchJob } from './launch';

new cron.CronJob({
cronTime: '00 10 * * 1-5', // du lundi au vendredi à 10:00
onTick: () => launchJob('dailyNewManagerMail'),
start: true,
timeZone: 'Europe/Paris',
});
export function startCronJobs() {
new cron.CronJob({
cronTime: '00 10 * * 1-5', // du lundi au vendredi à 10:00
onTick: () => launchJob('dailyNewManagerMail'),
start: true,
timeZone: 'Europe/Paris',
});

new cron.CronJob({
cronTime: '55 9 * * 2', // le mardi à 09:55
onTick: () => launchJob('weeklyOldManagerMail'),
start: true,
timeZone: 'Europe/Paris',
});
new cron.CronJob({
cronTime: '55 9 * * 2', // le mardi à 09:55
onTick: () => launchJob('weeklyOldManagerMail'),
start: true,
timeZone: 'Europe/Paris',
});

new cron.CronJob({
cronTime: '05 10 * * 1', // le lundi à 10:05
onTick: () => launchJob('dailyRelanceMail'),
start: true,
timeZone: 'Europe/Paris',
});
new cron.CronJob({
cronTime: '05 10 * * 1', // le lundi à 10:05
onTick: () => launchJob('dailyRelanceMail'),
start: true,
timeZone: 'Europe/Paris',
});

new cron.CronJob({
cronTime: '00 * * * *', // toutes les heures
onTick: () => launchJob('updateUsers'),
start: true,
timeZone: 'Europe/Paris',
});
new cron.CronJob({
cronTime: '00 * * * *', // toutes les heures
onTick: () => launchJob('updateUsers'),
start: true,
timeZone: 'Europe/Paris',
});

console.log('-- CRON JOB --- Started cron jobs waiting to get ticked...');
console.log('-- CRON JOB --- Started cron jobs waiting to get ticked...');
}
33 changes: 33 additions & 0 deletions tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { readdirSync } from 'node:fs';
import { basename } from 'node:path';

import { defineConfig } from 'tsup';

export default defineConfig(() => {
const entry = {
cli: 'scripts/cli.ts',
};
// keep the structure
const migrationFiles = readdirSync('./src/db/migrations');
for (const filename of migrationFiles) {
entry[`migrations/${basename(filename, '.ts')}`] = `src/db/migrations/${filename}`;
}

return {
entry,
format: ['cjs'],
outDir: '.next/cli/',
bundle: true,
splitting: false,
sourcemap: false,
clean: true,
minify: true,
env: {
KNEX_MIGRATIONS_DIR: './migrations',
},

// include node_modules dependencies
external: [],
noExternal: ['*'],
};
});
Loading

0 comments on commit 7c33c2b

Please sign in to comment.