Files
cloudron-box/src/cron.js

272 lines
8.3 KiB
JavaScript
Raw Normal View History

'use strict';
exports = module.exports = {
initialize: initialize,
uninitialize: uninitialize
};
var apps = require('./apps.js'),
2017-04-13 01:07:07 -07:00
appstore = require('./appstore.js'),
assert = require('assert'),
backups = require('./backups.js'),
2015-12-11 12:24:52 -08:00
certificates = require('./certificates.js'),
cloudron = require('./cloudron.js'),
2015-10-20 11:33:19 -07:00
config = require('./config.js'),
constants = require('./constants.js'),
CronJob = require('cron').CronJob,
debug = require('debug')('box:cron'),
2016-07-25 12:36:43 -07:00
eventlog = require('./eventlog.js'),
janitor = require('./janitor.js'),
scheduler = require('./scheduler.js'),
settings = require('./settings.js'),
updateChecker = require('./updatechecker.js');
var gAutoupdaterJob = null,
gBoxUpdateCheckerJob = null,
gAppUpdateCheckerJob = null,
2017-01-09 11:14:11 -08:00
gHeartbeatJob = null, // for CaaS health check
gAliveJob = null, // send periodic stats
gBackupJob = null,
2015-10-14 23:08:36 -07:00
gCleanupTokensJob = null,
gCleanupBackupsJob = null,
gDockerVolumeCleanerJob = null,
2015-12-10 13:31:47 -08:00
gSchedulerSyncJob = null,
2016-01-22 17:37:41 -08:00
gCertificateRenewJob = null,
2016-07-25 12:36:43 -07:00
gCheckDiskSpaceJob = null,
2017-01-02 13:14:03 +01:00
gCleanupEventlogJob = null,
gDynamicDNSJob = null;
var NOOP_CALLBACK = function (error) { if (error) console.error(error); };
2016-06-02 18:47:09 -07:00
var AUDIT_SOURCE = { userId: null, username: 'cron' };
// cron format
// Seconds: 0-59
// Minutes: 0-59
// Hours: 0-23
// Day of Month: 1-31
// Months: 0-11
// Day of Week: 0-6
function initialize(callback) {
assert.strictEqual(typeof callback, 'function');
gHeartbeatJob = new CronJob({
cronTime: '00 */1 * * * *', // every minute
onTick: cloudron.sendHeartbeat,
start: false
});
// hack: send the first heartbeat only after we are running for 60 seconds
// required as we end up sending a heartbeat and then cloudron-setup reboots the server
setTimeout(function () {
if (!gHeartbeatJob) return; // already uninitalized
gHeartbeatJob.start();
cloudron.sendHeartbeat();
}, 1000 * 60);
2016-12-19 11:56:35 -08:00
var randomHourMinute = Math.floor(60*Math.random());
2016-11-15 15:25:21 +01:00
gAliveJob = new CronJob({
cronTime: '00 ' + randomHourMinute + ' * * * *', // every hour on a random minute
2017-04-13 01:07:07 -07:00
onTick: appstore.sendAliveStatus,
2016-11-15 15:25:21 +01:00
start: true
});
settings.events.on(settings.TIME_ZONE_KEY, recreateJobs);
settings.events.on(settings.AUTOUPDATE_PATTERN_KEY, autoupdatePatternChanged);
2017-01-02 13:14:03 +01:00
settings.events.on(settings.DYNAMIC_DNS_KEY, dynamicDNSChanged);
settings.getAll(function (error, allSettings) {
if (error) return callback(error);
recreateJobs(allSettings[settings.TIME_ZONE_KEY]);
autoupdatePatternChanged(allSettings[settings.AUTOUPDATE_PATTERN_KEY]);
2017-01-02 13:51:58 +01:00
dynamicDNSChanged(allSettings[settings.DYNAMIC_DNS_KEY]);
2015-10-27 17:05:56 -07:00
callback();
});
}
function recreateJobs(tz) {
assert.strictEqual(typeof tz, 'string');
debug('Creating jobs with timezone %s', tz);
if (gBackupJob) gBackupJob.stop();
gBackupJob = new CronJob({
cronTime: '00 00 */4 * * *', // every 4 hours. backups.ensureBackup() will only trigger a backup once per day
onTick: backups.ensureBackup.bind(null, AUDIT_SOURCE, NOOP_CALLBACK),
start: true,
timeZone: tz
});
if (gCheckDiskSpaceJob) gCheckDiskSpaceJob.stop();
gCheckDiskSpaceJob = new CronJob({
cronTime: '00 30 */4 * * *', // every 4 hours
onTick: cloudron.checkDiskSpace,
start: true,
timeZone: tz
});
// randomized pattern per cloudron every hour
var randomMinute = Math.floor(60*Math.random());
if (gBoxUpdateCheckerJob) gBoxUpdateCheckerJob.stop();
gBoxUpdateCheckerJob = new CronJob({
cronTime: '00 ' + randomMinute + ' * * * *', // once an hour
onTick: updateChecker.checkBoxUpdates,
start: true,
timeZone: tz
});
if (gAppUpdateCheckerJob) gAppUpdateCheckerJob.stop();
gAppUpdateCheckerJob = new CronJob({
cronTime: '00 ' + randomMinute + ' * * * *', // once an hour
onTick: updateChecker.checkAppUpdates,
start: true,
timeZone: tz
});
if (gCleanupTokensJob) gCleanupTokensJob.stop();
gCleanupTokensJob = new CronJob({
cronTime: '00 */30 * * * *', // every 30 minutes
onTick: janitor.cleanupTokens,
start: true,
timeZone: tz
});
if (gCleanupBackupsJob) gCleanupBackupsJob.stop();
gCleanupBackupsJob = new CronJob({
cronTime: '00 00 */4 * * *', // every 4 hours
onTick: backups.cleanup,
start: true,
timeZone: tz
});
if (gCleanupEventlogJob) gCleanupEventlogJob.stop();
gCleanupEventlogJob = new CronJob({
cronTime: '00 */30 * * * *', // every 30 minutes
onTick: eventlog.cleanup,
start: true,
timeZone: tz
});
if (gDockerVolumeCleanerJob) gDockerVolumeCleanerJob.stop();
gDockerVolumeCleanerJob = new CronJob({
cronTime: '00 00 */12 * * *', // every 12 hours
onTick: janitor.cleanupDockerVolumes,
start: true,
timeZone: tz
});
if (gSchedulerSyncJob) gSchedulerSyncJob.stop();
gSchedulerSyncJob = new CronJob({
cronTime: config.TEST ? '*/10 * * * * *' : '00 */1 * * * *', // every minute
onTick: scheduler.sync,
start: true,
timeZone: tz
});
if (gCertificateRenewJob) gCertificateRenewJob.stop();
gCertificateRenewJob = new CronJob({
cronTime: '00 00 */12 * * *', // every 12 hours
onTick: certificates.renewAll.bind(null, AUDIT_SOURCE, NOOP_CALLBACK),
start: true,
timeZone: tz
});
}
function autoupdatePatternChanged(pattern) {
assert.strictEqual(typeof pattern, 'string');
assert(gBoxUpdateCheckerJob);
debug('Auto update pattern changed to %s', pattern);
if (gAutoupdaterJob) gAutoupdaterJob.stop();
if (pattern === constants.AUTOUPDATE_PATTERN_NEVER) return;
gAutoupdaterJob = new CronJob({
cronTime: pattern,
onTick: function() {
var updateInfo = updateChecker.getUpdateInfo();
if (updateInfo.box) {
debug('Starting autoupdate to %j', updateInfo.box);
2016-06-02 18:47:09 -07:00
cloudron.updateToLatest(AUDIT_SOURCE, NOOP_CALLBACK);
} else if (updateInfo.apps) {
debug('Starting app update to %j', updateInfo.apps);
2016-06-02 18:49:56 -07:00
apps.updateApps(updateInfo.apps, AUDIT_SOURCE, NOOP_CALLBACK);
} else {
debug('No auto updates available');
}
},
start: true,
timeZone: gBoxUpdateCheckerJob.cronTime.zone // hack
});
}
2017-01-02 13:14:03 +01:00
function dynamicDNSChanged(enabled) {
assert.strictEqual(typeof enabled, 'boolean');
assert(gBoxUpdateCheckerJob);
debug('Dynamic DNS setting changed to %s', enabled);
if (enabled) {
gDynamicDNSJob = new CronJob({
cronTime: '00 */10 * * * *',
onTick: cloudron.refreshDNS,
start: true,
timeZone: gBoxUpdateCheckerJob.cronTime.zone // hack
});
} else {
if (gDynamicDNSJob) gDynamicDNSJob.stop();
gDynamicDNSJob = null;
}
}
function uninitialize(callback) {
assert.strictEqual(typeof callback, 'function');
2015-11-03 15:19:06 -08:00
settings.events.removeListener(settings.TIME_ZONE_KEY, recreateJobs);
settings.events.removeListener(settings.AUTOUPDATE_PATTERN_KEY, autoupdatePatternChanged);
if (gAutoupdaterJob) gAutoupdaterJob.stop();
gAutoupdaterJob = null;
2015-10-27 16:02:42 -07:00
if (gBoxUpdateCheckerJob) gBoxUpdateCheckerJob.stop();
gBoxUpdateCheckerJob = null;
2015-10-27 16:02:42 -07:00
if (gAppUpdateCheckerJob) gAppUpdateCheckerJob.stop();
gAppUpdateCheckerJob = null;
2015-10-27 16:02:42 -07:00
if (gHeartbeatJob) gHeartbeatJob.stop();
gHeartbeatJob = null;
2016-11-15 15:25:21 +01:00
if (gAliveJob) gAliveJob.stop();
gAliveJob = null;
2015-10-27 16:02:42 -07:00
if (gBackupJob) gBackupJob.stop();
gBackupJob = null;
2015-10-27 16:02:42 -07:00
if (gCleanupTokensJob) gCleanupTokensJob.stop();
gCleanupTokensJob = null;
if (gCleanupBackupsJob) gCleanupBackupsJob.stop();
gCleanupBackupsJob = null;
2016-07-25 12:36:43 -07:00
if (gCleanupEventlogJob) gCleanupEventlogJob.stop();
gCleanupEventlogJob = null;
2015-10-27 16:02:42 -07:00
if (gDockerVolumeCleanerJob) gDockerVolumeCleanerJob.stop();
2015-10-14 23:08:36 -07:00
gDockerVolumeCleanerJob = null;
2015-10-27 16:02:42 -07:00
if (gSchedulerSyncJob) gSchedulerSyncJob.stop();
gSchedulerSyncJob = null;
2015-12-10 13:31:47 -08:00
if (gCertificateRenewJob) gCertificateRenewJob.stop();
gCertificateRenewJob = null;
2017-01-02 13:14:03 +01:00
if (gDynamicDNSJob) gDynamicDNSJob.stop();
gDynamicDNSJob = null;
callback();
}