reorder functions for no-use-before-define
This commit is contained in:
+87
-87
@@ -86,6 +86,84 @@ function getCronSeed() {
|
||||
return { hour, minute };
|
||||
}
|
||||
|
||||
async function handleBackupScheduleChanged(site) {
|
||||
assert.strictEqual(typeof site, 'object');
|
||||
|
||||
const tz = await cloudron.getTimeZone();
|
||||
|
||||
debug(`handleBackupScheduleChanged: schedule ${site.schedule} (${tz})`);
|
||||
|
||||
if (gJobs.backups.has(site.id)) gJobs.backups.get(site.id).stop();
|
||||
gJobs.backups.delete(site.id);
|
||||
|
||||
if (site.schedule === constants.CRON_PATTERN_NEVER) return;
|
||||
|
||||
const job = CronJob.from({
|
||||
cronTime: site.schedule,
|
||||
onTick: async () => {
|
||||
const t = await backupSites.get(site.id);
|
||||
if (!t) return;
|
||||
await safe(backupSites.startBackupTask(t, AuditSource.CRON), { debug });
|
||||
},
|
||||
start: true,
|
||||
timeZone: tz
|
||||
});
|
||||
gJobs.backups.set(site.id, job);
|
||||
}
|
||||
|
||||
async function handleAutoupdatePatternChanged(pattern) {
|
||||
assert.strictEqual(typeof pattern, 'string');
|
||||
|
||||
const tz = await cloudron.getTimeZone();
|
||||
|
||||
debug(`autoupdatePatternChanged: pattern - ${pattern} (${tz})`);
|
||||
|
||||
if (gJobs.autoUpdater) gJobs.autoUpdater.stop();
|
||||
gJobs.autoUpdater = null;
|
||||
|
||||
if (pattern === constants.CRON_PATTERN_NEVER) return;
|
||||
|
||||
gJobs.autoUpdater = CronJob.from({
|
||||
cronTime: pattern,
|
||||
onTick: async () => await safe(updater.autoUpdate(AuditSource.CRON), { debug }),
|
||||
start: true,
|
||||
timeZone: tz
|
||||
});
|
||||
}
|
||||
|
||||
function handleDynamicDnsChanged(enabled) {
|
||||
assert.strictEqual(typeof enabled, 'boolean');
|
||||
|
||||
debug('Dynamic DNS setting changed to %s', enabled);
|
||||
|
||||
if (gJobs.dynamicDns) gJobs.dynamicDns.stop();
|
||||
gJobs.dynamicDns = null;
|
||||
|
||||
if (!enabled) return;
|
||||
|
||||
gJobs.dynamicDns = CronJob.from({
|
||||
// until we can be smarter about actual IP changes, lets ensure it every 10minutes
|
||||
cronTime: '00 */10 * * * *',
|
||||
onTick: async () => { await safe(dyndns.refreshDns(AuditSource.CRON), { debug }); },
|
||||
start: true
|
||||
});
|
||||
}
|
||||
|
||||
async function handleExternalLdapChanged(config) {
|
||||
assert.strictEqual(typeof config, 'object');
|
||||
|
||||
if (gJobs.externalLdapSyncer) gJobs.externalLdapSyncer.stop();
|
||||
gJobs.externalLdapSyncer = null;
|
||||
|
||||
if (config.provider === 'noop') return;
|
||||
|
||||
gJobs.externalLdapSyncer = CronJob.from({
|
||||
cronTime: '00 00 */4 * * *', // every 4 hours
|
||||
onTick: async () => await safe(externalLdap.startSyncer(AuditSource.CRON), { debug }),
|
||||
start: true
|
||||
});
|
||||
}
|
||||
|
||||
async function startJobs() {
|
||||
const { hour, minute } = getCronSeed();
|
||||
|
||||
@@ -195,93 +273,6 @@ async function startJobs() {
|
||||
await handleExternalLdapChanged(await externalLdap.getConfig());
|
||||
}
|
||||
|
||||
async function handleBackupScheduleChanged(site) {
|
||||
assert.strictEqual(typeof site, 'object');
|
||||
|
||||
const tz = await cloudron.getTimeZone();
|
||||
|
||||
debug(`handleBackupScheduleChanged: schedule ${site.schedule} (${tz})`);
|
||||
|
||||
if (gJobs.backups.has(site.id)) gJobs.backups.get(site.id).stop();
|
||||
gJobs.backups.delete(site.id);
|
||||
|
||||
if (site.schedule === constants.CRON_PATTERN_NEVER) return;
|
||||
|
||||
const job = CronJob.from({
|
||||
cronTime: site.schedule,
|
||||
onTick: async () => {
|
||||
const t = await backupSites.get(site.id);
|
||||
if (!t) return;
|
||||
await safe(backupSites.startBackupTask(t, AuditSource.CRON), { debug });
|
||||
},
|
||||
start: true,
|
||||
timeZone: tz
|
||||
});
|
||||
gJobs.backups.set(site.id, job);
|
||||
}
|
||||
|
||||
async function handleTimeZoneChanged(tz) {
|
||||
assert.strictEqual(typeof tz, 'string');
|
||||
|
||||
debug('handleTimeZoneChanged: recreating all jobs');
|
||||
await stopJobs();
|
||||
await scheduler.deleteJobs(); // have to re-create with new tz
|
||||
await startJobs();
|
||||
}
|
||||
|
||||
async function handleAutoupdatePatternChanged(pattern) {
|
||||
assert.strictEqual(typeof pattern, 'string');
|
||||
|
||||
const tz = await cloudron.getTimeZone();
|
||||
|
||||
debug(`autoupdatePatternChanged: pattern - ${pattern} (${tz})`);
|
||||
|
||||
if (gJobs.autoUpdater) gJobs.autoUpdater.stop();
|
||||
gJobs.autoUpdater = null;
|
||||
|
||||
if (pattern === constants.CRON_PATTERN_NEVER) return;
|
||||
|
||||
gJobs.autoUpdater = CronJob.from({
|
||||
cronTime: pattern,
|
||||
onTick: async () => await safe(updater.autoUpdate(AuditSource.CRON), { debug }),
|
||||
start: true,
|
||||
timeZone: tz
|
||||
});
|
||||
}
|
||||
|
||||
function handleDynamicDnsChanged(enabled) {
|
||||
assert.strictEqual(typeof enabled, 'boolean');
|
||||
|
||||
debug('Dynamic DNS setting changed to %s', enabled);
|
||||
|
||||
if (gJobs.dynamicDns) gJobs.dynamicDns.stop();
|
||||
gJobs.dynamicDns = null;
|
||||
|
||||
if (!enabled) return;
|
||||
|
||||
gJobs.dynamicDns = CronJob.from({
|
||||
// until we can be smarter about actual IP changes, lets ensure it every 10minutes
|
||||
cronTime: '00 */10 * * * *',
|
||||
onTick: async () => { await safe(dyndns.refreshDns(AuditSource.CRON), { debug }); },
|
||||
start: true
|
||||
});
|
||||
}
|
||||
|
||||
async function handleExternalLdapChanged(config) {
|
||||
assert.strictEqual(typeof config, 'object');
|
||||
|
||||
if (gJobs.externalLdapSyncer) gJobs.externalLdapSyncer.stop();
|
||||
gJobs.externalLdapSyncer = null;
|
||||
|
||||
if (config.provider === 'noop') return;
|
||||
|
||||
gJobs.externalLdapSyncer = CronJob.from({
|
||||
cronTime: '00 00 */4 * * *', // every 4 hours
|
||||
onTick: async () => await safe(externalLdap.startSyncer(AuditSource.CRON), { debug }),
|
||||
start: true
|
||||
});
|
||||
}
|
||||
|
||||
async function stopJobs() {
|
||||
for (const jobName in gJobs) {
|
||||
if (!gJobs[jobName]) continue;
|
||||
@@ -295,6 +286,15 @@ async function stopJobs() {
|
||||
}
|
||||
}
|
||||
|
||||
async function handleTimeZoneChanged(tz) {
|
||||
assert.strictEqual(typeof tz, 'string');
|
||||
|
||||
debug('handleTimeZoneChanged: recreating all jobs');
|
||||
await stopJobs();
|
||||
await scheduler.deleteJobs(); // have to re-create with new tz
|
||||
await startJobs();
|
||||
}
|
||||
|
||||
export default {
|
||||
startJobs,
|
||||
|
||||
|
||||
Reference in New Issue
Block a user