diff --git a/src/cron.js b/src/cron.js index ea2dc9803..8f73f6a91 100644 --- a/src/cron.js +++ b/src/cron.js @@ -63,24 +63,36 @@ const gJobs = { // Day of Week: 0-6 function getCronSeed() { - let seedHour = parseInt(safe.fs.readFileSync(paths.CRON_SEED_FILE)); - if (!seedHour || seedHour < 0 || seedHour > 23) { - seedHour = Math.floor(Math.random()*24); - debug(`getCronSeed: writing new cron seed file with ${seedHour} to ${paths.CRON_SEED_FILE}`); - safe.fs.writeFileSync(paths.CRON_SEED_FILE, `${seedHour}`); + let hour = null; + let minute = null; + + let tmp = safe.fs.readFileSync(paths.CRON_SEED_FILE, 'utf8'); + if (tmp) tmp = tmp.split(':'); + + if (tmp.length === 2) { + hour = parseInt(tmp[0]); + minute = parseInt(tmp[1]); } - return seedHour; + if ((hour === null || hour < 0 || hour > 23) || (minute === null || minute < 0 || minute > 60)) { + hour = Math.floor(Math.random()*24); + minute = Math.floor(60*Math.random()); + + debug(`getCronSeed: writing new cron seed file with ${hour}:${minute} to ${paths.CRON_SEED_FILE}`); + + safe.fs.writeFileSync(paths.CRON_SEED_FILE, `${hour}:${minute}`); + } + + return { hour, minute }; } async function startJobs() { - const hourlySeed = getCronSeed(); + const { hour, minute } = getCronSeed(); - debug(`startJobs: starting cron jobs with hourly offset seed ${hourlySeed}`); + debug(`startJobs: starting cron jobs with hour ${hour} and minute ${minute}`); - const randomTick = Math.floor(60*Math.random()); gJobs.systemChecks = new CronJob({ - cronTime: `${randomTick} ${randomTick} 2 * * *`, // once a day. if you change this interval, change the notification messages with correct duration + cronTime: `00 ${minute} 2 * * *`, // once a day. if you change this interval, change the notification messages with correct duration onTick: async () => await safe(cloudron.runSystemChecks(), { debug }), start: true }); @@ -93,7 +105,7 @@ async function startJobs() { // this is run separately from the update itself so that the user can disable automatic updates but can still get a notification gJobs.updateCheckerJob = new CronJob({ - cronTime: `${randomTick} ${randomTick} 1,5,9,13,17,21,23 * * *`, + cronTime: `00 ${minute} 1,5,9,13,17,21,23 * * *`, onTick: async () => await safe(updateChecker.checkForUpdates({ automatic: true }), { debug }), start: true }); @@ -130,7 +142,7 @@ async function startJobs() { // randomized per Cloudron based on hourlySeed gJobs.certificateRenew = new CronJob({ - cronTime: `00 10 ${hourlySeed} * * *`, + cronTime: `00 10 ${hour} * * *`, onTick: async () => await safe(cloudron.renewCerts({}, AuditSource.CRON), { debug }), start: true });