Files
cloudron-box/src/cron.js
T

232 lines
7.7 KiB
JavaScript
Raw Normal View History

'use strict';
// IMPORTANT: These patterns are together because they spin tasks which acquire a lock
// If the patterns overlap all the time, then the task may not ever get a chance to run!
// If you change this change dashboard patterns in settings.html
const DEFAULT_CLEANUP_BACKUPS_PATTERN = '00 30 1,3,5,23 * * *',
DEFAULT_AUTOUPDATE_PATTERN = '00 00 1,3,5,23 * * *';
exports = module.exports = {
startJobs,
stopJobs,
2018-09-26 12:44:38 -07:00
handleSettingsChanged,
2019-03-04 10:25:18 -08:00
DEFAULT_AUTOUPDATE_PATTERN,
};
const appHealthMonitor = require('./apphealthmonitor.js'),
2018-10-22 11:39:42 -07:00
apps = require('./apps.js'),
assert = require('assert'),
2021-09-30 09:50:30 -07:00
AuditSource = require('./auditsource.js'),
2016-04-10 19:17:44 -07:00
backups = require('./backups.js'),
cloudron = require('./cloudron.js'),
constants = require('./constants.js'),
CronJob = require('cron').CronJob,
debug = require('debug')('box:cron'),
2018-01-25 14:03:42 -08:00
dyndns = require('./dyndns.js'),
2016-07-25 12:36:43 -07:00
eventlog = require('./eventlog.js'),
2015-10-14 22:34:00 -07:00
janitor = require('./janitor.js'),
2021-08-20 09:19:44 -07:00
safe = require('safetydance'),
2015-10-18 08:40:24 -07:00
scheduler = require('./scheduler.js'),
settings = require('./settings.js'),
2019-11-21 12:58:06 -08:00
system = require('./system.js'),
2018-07-31 11:35:23 -07:00
updater = require('./updater.js'),
2021-01-31 20:47:33 -08:00
updateChecker = require('./updatechecker.js'),
_ = require('underscore');
2021-01-31 20:47:33 -08:00
const gJobs = {
autoUpdater: null,
2017-11-27 12:40:13 -08:00
backup: null,
2020-08-19 21:39:58 -07:00
updateChecker: null,
systemChecks: null,
2019-08-19 13:50:44 -07:00
diskSpaceChecker: null,
2017-11-27 12:40:13 -08:00
certificateRenew: null,
cleanupBackups: null,
cleanupEventlog: null,
cleanupTokens: null,
dockerVolumeCleaner: null,
2018-11-10 00:18:56 -08:00
dynamicDns: null,
2018-10-22 11:39:42 -07:00
schedulerSync: null,
appHealthMonitor: null
2017-11-27 12:40:13 -08:00
};
// cron format
// Seconds: 0-59
// Minutes: 0-59
// Hours: 0-23
// Day of Month: 1-31
// Months: 0-11
// Day of Week: 0-6
2021-08-18 13:25:42 -07:00
async function startJobs() {
2020-10-07 14:47:51 -07:00
debug('startJobs: starting cron jobs');
2020-08-19 21:39:58 -07:00
const randomTick = Math.floor(60*Math.random());
gJobs.systemChecks = new CronJob({
2020-07-31 11:20:17 -07:00
cronTime: '00 30 2 * * *', // once a day. if you change this interval, change the notification messages with correct duration
2021-09-17 09:22:46 -07:00
onTick: async () => await safe(cloudron.runSystemChecks(), { debug }),
start: true
2016-12-28 14:21:41 -08:00
});
2019-08-19 13:50:44 -07:00
gJobs.diskSpaceChecker = new CronJob({
cronTime: '00 30 * * * *', // every 30 minutes. if you change this interval, change the notification messages with correct duration
2021-09-13 11:29:55 -07:00
onTick: async () => await safe(system.checkDiskSpace(), { debug }),
start: true
2019-08-19 13:50:44 -07:00
});
// this is run separately from the update itself so that the user can disable automatic updates but can still get a notification
2020-08-19 21:39:58 -07:00
gJobs.updateCheckerJob = new CronJob({
cronTime: `${randomTick} ${randomTick} 1,5,9,13,17,21,23 * * *`,
2021-09-13 11:29:55 -07:00
onTick: async () => await safe(updateChecker.checkForUpdates({ automatic: true }), { debug }),
start: true
2016-12-28 14:21:41 -08:00
});
2017-11-27 12:40:13 -08:00
gJobs.cleanupTokens = new CronJob({
2016-12-28 14:21:41 -08:00
cronTime: '00 */30 * * * *', // every 30 minutes
2021-09-17 09:22:46 -07:00
onTick: async () => await safe(janitor.cleanupTokens(), { debug }),
start: true
2016-12-28 14:21:41 -08:00
});
2017-11-27 12:40:13 -08:00
gJobs.cleanupBackups = new CronJob({
cronTime: DEFAULT_CLEANUP_BACKUPS_PATTERN,
2021-09-30 09:50:30 -07:00
onTick: async () => await safe(backups.startCleanupTask(AuditSource.CRON), { debug }),
start: true
2016-12-28 14:21:41 -08:00
});
2017-11-27 12:40:13 -08:00
gJobs.cleanupEventlog = new CronJob({
2016-12-28 14:21:41 -08:00
cronTime: '00 */30 * * * *', // every 30 minutes
2021-09-17 09:22:46 -07:00
onTick: async () => await safe(eventlog.cleanup({ creationTime: new Date(Date.now() - 60 * 60 * 24 * 10 * 1000) }), { debug }), // 10 days ago
start: true
2016-12-28 14:21:41 -08:00
});
2017-11-27 12:40:13 -08:00
gJobs.dockerVolumeCleaner = new CronJob({
2016-12-28 14:21:41 -08:00
cronTime: '00 00 */12 * * *', // every 12 hours
2021-09-17 09:22:46 -07:00
onTick: async () => await safe(janitor.cleanupDockerVolumes(), { debug }),
start: true
2016-12-28 14:21:41 -08:00
});
2017-11-27 12:40:13 -08:00
gJobs.schedulerSync = new CronJob({
2019-07-26 10:10:14 -07:00
cronTime: constants.TEST ? '*/10 * * * * *' : '00 */1 * * * *', // every minute
2021-09-13 11:29:55 -07:00
onTick: async () => await safe(scheduler.sync(), { debug }),
start: true
2016-12-28 14:21:41 -08:00
});
2017-11-27 12:40:13 -08:00
gJobs.certificateRenew = new CronJob({
2016-12-28 14:21:41 -08:00
cronTime: '00 00 */12 * * *', // every 12 hours
2021-09-30 09:50:30 -07:00
onTick: async () => await safe(cloudron.renewCerts({}, AuditSource.CRON), { debug }),
start: true
});
2017-07-21 16:13:44 +02:00
2018-10-22 11:39:42 -07:00
gJobs.appHealthMonitor = new CronJob({
cronTime: '*/10 * * * * *', // every 10 seconds
2021-09-13 11:29:55 -07:00
onTick: async () => await safe(appHealthMonitor.run(10), { debug }), // 10 is the max run time
start: true
});
2021-08-18 13:25:42 -07:00
const allSettings = await settings.list();
2021-08-18 13:25:42 -07:00
const tz = allSettings[settings.TIME_ZONE_KEY];
backupConfigChanged(allSettings[settings.BACKUP_CONFIG_KEY], tz);
autoupdatePatternChanged(allSettings[settings.AUTOUPDATE_PATTERN_KEY], tz);
dynamicDnsChanged(allSettings[settings.DYNAMIC_DNS_KEY]);
}
// eslint-disable-next-line no-unused-vars
2021-09-17 09:22:46 -07:00
async function handleSettingsChanged(key, value) {
assert.strictEqual(typeof key, 'string');
// value is a variant
switch (key) {
case settings.TIME_ZONE_KEY:
case settings.BACKUP_CONFIG_KEY:
case settings.AUTOUPDATE_PATTERN_KEY:
case settings.DYNAMIC_DNS_KEY:
debug('handleSettingsChanged: recreating all jobs');
2021-09-17 09:22:46 -07:00
await stopJobs();
await startJobs();
break;
default:
break;
}
}
function backupConfigChanged(value, tz) {
assert.strictEqual(typeof value, 'object');
assert.strictEqual(typeof tz, 'string');
2020-07-29 09:34:23 -07:00
debug(`backupConfigChanged: schedule ${value.schedulePattern} (${tz})`);
if (gJobs.backup) gJobs.backup.stop();
gJobs.backup = new CronJob({
cronTime: value.schedulePattern,
2021-09-30 09:50:30 -07:00
onTick: async () => await safe(backups.startBackupTask(AuditSource.CRON), { debug }),
2018-10-22 11:39:42 -07:00
start: true,
timeZone: tz
});
}
function autoupdatePatternChanged(pattern, tz) {
assert.strictEqual(typeof pattern, 'string');
assert.strictEqual(typeof tz, 'string');
debug(`autoupdatePatternChanged: pattern - ${pattern} (${tz})`);
if (gJobs.autoUpdater) gJobs.autoUpdater.stop();
if (pattern === constants.AUTOUPDATE_PATTERN_NEVER) return;
gJobs.autoUpdater = new CronJob({
cronTime: pattern,
2021-08-20 09:19:44 -07:00
onTick: async function() {
const updateInfo = updateChecker.getUpdateInfo();
// do box before app updates. for the off chance that the box logic fixes some app update logic issue
if (updateInfo.box && !updateInfo.box.unstable) {
debug('Starting box autoupdate to %j', updateInfo.box);
2021-09-30 09:50:30 -07:00
const [error] = await safe(updater.updateToLatest({ skipBackup: false }, AuditSource.CRON));
2021-08-31 13:12:14 -07:00
if (error) debug(`Failed to box autoupdate: ${error.message}`);
return;
}
2021-01-31 20:46:55 -08:00
const appUpdateInfo = _.omit(updateInfo, 'box');
if (Object.keys(appUpdateInfo).length > 0) {
debug('Starting app update to %j', appUpdateInfo);
2021-09-30 09:50:30 -07:00
const [error] = await safe(apps.autoupdateApps(appUpdateInfo, AuditSource.CRON));
2021-08-31 13:12:14 -07:00
if (error) debug(`Failed to app autoupdate: ${error.message}`);
2015-07-28 09:37:46 -07:00
} else {
debug('No app auto updates available');
}
},
start: true,
timeZone: tz
});
}
2018-01-25 14:03:42 -08:00
function dynamicDnsChanged(enabled) {
2017-01-02 13:14:03 +01:00
assert.strictEqual(typeof enabled, 'boolean');
debug('Dynamic DNS setting changed to %s', enabled);
if (enabled) {
2018-11-10 00:18:56 -08:00
gJobs.dynamicDns = new CronJob({
cronTime: '5 * * * * *', // we only update the records if the ip has changed.
2021-09-30 09:50:30 -07:00
onTick: async () => await safe(dyndns.sync(AuditSource.CRON), { debug }),
start: true
2017-01-02 13:14:03 +01:00
});
} else {
2018-11-10 00:18:56 -08:00
if (gJobs.dynamicDns) gJobs.dynamicDns.stop();
gJobs.dynamicDns = null;
2017-01-02 13:14:03 +01:00
}
}
2021-09-07 09:57:49 -07:00
async function stopJobs() {
for (const job in gJobs) {
2017-11-27 12:40:13 -08:00
if (!gJobs[job]) continue;
gJobs[job].stop();
gJobs[job] = null;
}
}