import { DateTime } from 'luxon' ; import { CronJob } from 'cron' ; import { ensureDirs } from '../file-storage' ; import { dbConnect } from '../db' ; import config from '../config' ; import { runTasks } from './run-tasks' ; import { cronTasks , updateTasks } from './tasks' ; import { checkImportQueue } from '../worker/index' ; export async function cronInit ( ) { await ensureDirs ( ) ; await dbConnect ( ) ; const dailyJob = new CronJob ( { cronTime : config . cron . cronTime , start : true , runOnInit : config . cron . runOnInit , timeZone : 'Europe/Prague' , onTick : ( ) => runCron ( ) , } ) ; const importJob = new CronJob ( { cronTime : '*/10 * * * * *' , start : true , runOnInit : true , timeZone : 'Europe/Prague' , onTick : ( ) => checkImportQueue ( ) , } ) ; const updateJob = new CronJob ( { cronTime : '* */10 * * *' , start : false , runOnInit : true , timeZone : 'Europe/Prague' , onTick : ( ) => runUpdate ( ) , } ) ; if ( ! dailyJob . running ) { dailyJob . start ( ) ; } importJob . start ( ) ; updateJob . start ( ) ; console . log ( '[CRON] CityVizor daily job set at ' + config . cron . cronTime ) ; } async function runCron ( ) { console . log ( '\n[CRON] =============================' ) ; console . log ( '[CRON] ##### CRON RUN #####' ) ; console . log ( '[CRON] =============================' ) ; console . log ( `[CRON] Started at ${ DateTime . local ( ) . toLocaleString ( ) }.\n` ) ; await runTasks ( cronTasks ) ; console . log ( '[CRON] ===================================\n\n' ) ; console . log ( `[CRON] Finished at ${ DateTime . local ( ) . toLocaleString ( ) }.\n` ) ; } async function runUpdate ( ) { console . log ( '\n[CRON] =============================' ) ; console . log ( '[CRON] ##### CRON RUN UPDATE JOB #####' ) ; console . log ( '[CRON] =============================' ) ; console . log ( `[CRON] Started at ${ DateTime . local ( ) . toLocaleString ( ) }.\n` ) ; await runTasks ( updateTasks ) ; console . log ( '[CRON] ===================================\n\n' ) ; console . log ( `[CRON] Finished at ${ DateTime . local ( ) . toLocaleString ( ) }.\n` ) ; }