replace debug() with our custom logger
mostly we want trace() and log(). trace() can be enabled whenever we want by flipping a flag and restarting box
This commit is contained in:
54
src/cron.js
54
src/cron.js
@@ -6,7 +6,7 @@ import backupSites from './backupsites.js';
|
||||
import cloudron from './cloudron.js';
|
||||
import constants from './constants.js';
|
||||
import { CronJob } from 'cron';
|
||||
import debugModule from 'debug';
|
||||
import logger from './logger.js';
|
||||
import domains from './domains.js';
|
||||
import dyndns from './dyndns.js';
|
||||
import externalLdap from './externalldap.js';
|
||||
@@ -24,7 +24,7 @@ import system from './system.js';
|
||||
import updater from './updater.js';
|
||||
import util from 'node:util';
|
||||
|
||||
const debug = debugModule('box:cron');
|
||||
const { log, trace } = logger('cron');
|
||||
|
||||
// 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!
|
||||
@@ -78,7 +78,7 @@ function getCronSeed() {
|
||||
hour = Math.floor(24 * Math.random());
|
||||
minute = Math.floor(60 * Math.random());
|
||||
|
||||
debug(`getCronSeed: writing new cron seed file with ${hour}:${minute} to ${paths.CRON_SEED_FILE}`);
|
||||
log(`getCronSeed: writing new cron seed file with ${hour}:${minute} to ${paths.CRON_SEED_FILE}`);
|
||||
|
||||
safe.fs.writeFileSync(paths.CRON_SEED_FILE, `${hour}:${minute}`);
|
||||
}
|
||||
@@ -91,7 +91,7 @@ async function handleBackupScheduleChanged(site) {
|
||||
|
||||
const tz = await cloudron.getTimeZone();
|
||||
|
||||
debug(`handleBackupScheduleChanged: schedule ${site.schedule} (${tz})`);
|
||||
log(`handleBackupScheduleChanged: schedule ${site.schedule} (${tz})`);
|
||||
|
||||
if (gJobs.backups.has(site.id)) gJobs.backups.get(site.id).stop();
|
||||
gJobs.backups.delete(site.id);
|
||||
@@ -103,7 +103,7 @@ async function handleBackupScheduleChanged(site) {
|
||||
onTick: async () => {
|
||||
const t = await backupSites.get(site.id);
|
||||
if (!t) return;
|
||||
await safe(backupSites.startBackupTask(t, AuditSource.CRON), { debug });
|
||||
await safe(backupSites.startBackupTask(t, AuditSource.CRON), { debug: log });
|
||||
},
|
||||
start: true,
|
||||
timeZone: tz
|
||||
@@ -116,7 +116,7 @@ async function handleAutoupdatePatternChanged(pattern) {
|
||||
|
||||
const tz = await cloudron.getTimeZone();
|
||||
|
||||
debug(`autoupdatePatternChanged: pattern - ${pattern} (${tz})`);
|
||||
log(`autoupdatePatternChanged: pattern - ${pattern} (${tz})`);
|
||||
|
||||
if (gJobs.autoUpdater) gJobs.autoUpdater.stop();
|
||||
gJobs.autoUpdater = null;
|
||||
@@ -125,7 +125,7 @@ async function handleAutoupdatePatternChanged(pattern) {
|
||||
|
||||
gJobs.autoUpdater = CronJob.from({
|
||||
cronTime: pattern,
|
||||
onTick: async () => await safe(updater.autoUpdate(AuditSource.CRON), { debug }),
|
||||
onTick: async () => await safe(updater.autoUpdate(AuditSource.CRON), { debug: log }),
|
||||
start: true,
|
||||
timeZone: tz
|
||||
});
|
||||
@@ -134,7 +134,7 @@ async function handleAutoupdatePatternChanged(pattern) {
|
||||
function handleDynamicDnsChanged(enabled) {
|
||||
assert.strictEqual(typeof enabled, 'boolean');
|
||||
|
||||
debug('Dynamic DNS setting changed to %s', enabled);
|
||||
log('Dynamic DNS setting changed to %s', enabled);
|
||||
|
||||
if (gJobs.dynamicDns) gJobs.dynamicDns.stop();
|
||||
gJobs.dynamicDns = null;
|
||||
@@ -144,7 +144,7 @@ function handleDynamicDnsChanged(enabled) {
|
||||
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 }); },
|
||||
onTick: async () => { await safe(dyndns.refreshDns(AuditSource.CRON), { debug: log }); },
|
||||
start: true
|
||||
});
|
||||
}
|
||||
@@ -159,7 +159,7 @@ async function handleExternalLdapChanged(config) {
|
||||
|
||||
gJobs.externalLdapSyncer = CronJob.from({
|
||||
cronTime: '00 00 */4 * * *', // every 4 hours
|
||||
onTick: async () => await safe(externalLdap.startSyncer(AuditSource.CRON), { debug }),
|
||||
onTick: async () => await safe(externalLdap.startSyncer(AuditSource.CRON), { debug: log }),
|
||||
start: true
|
||||
});
|
||||
}
|
||||
@@ -167,42 +167,42 @@ async function handleExternalLdapChanged(config) {
|
||||
async function startJobs() {
|
||||
const { hour, minute } = getCronSeed();
|
||||
|
||||
debug(`startJobs: starting cron jobs with hour ${hour} and minute ${minute}`);
|
||||
log(`startJobs: starting cron jobs with hour ${hour} and minute ${minute}`);
|
||||
|
||||
gJobs.systemChecks = CronJob.from({
|
||||
cronTime: `00 ${minute} 2 * * *`, // once a day. if you change this interval, change the notification messages with correct duration
|
||||
onTick: async () => await safe(system.runSystemChecks(), { debug }),
|
||||
onTick: async () => await safe(system.runSystemChecks(), { debug: log }),
|
||||
start: true
|
||||
});
|
||||
|
||||
gJobs.mailStatusCheck = CronJob.from({
|
||||
cronTime: `00 ${minute} 2 * * *`, // once a day. if you change this interval, change the notification messages with correct duration
|
||||
onTick: async () => await safe(mail.checkStatus(), { debug }),
|
||||
onTick: async () => await safe(mail.checkStatus(), { debug: log }),
|
||||
start: true
|
||||
});
|
||||
|
||||
gJobs.diskSpaceChecker = CronJob.from({
|
||||
cronTime: '00 30 * * * *', // every 30 minutes. if you change this interval, change the notification messages with correct duration
|
||||
onTick: async () => await safe(system.checkDiskSpace(), { debug }),
|
||||
onTick: async () => await safe(system.checkDiskSpace(), { debug: log }),
|
||||
start: true
|
||||
});
|
||||
|
||||
// this is run separately from the update itself so that the user can disable automatic updates but can still get a notification
|
||||
gJobs.updateCheckerJob = CronJob.from({
|
||||
cronTime: `00 ${minute} 1,5,9,13,17,21,23 * * *`,
|
||||
onTick: async () => await safe(updater.checkForUpdates({ stableOnly: true }), { debug }),
|
||||
onTick: async () => await safe(updater.checkForUpdates({ stableOnly: true }), { debug: log }),
|
||||
start: true
|
||||
});
|
||||
|
||||
gJobs.cleanupTokens = CronJob.from({
|
||||
cronTime: '00 */30 * * * *', // every 30 minutes
|
||||
onTick: async () => await safe(janitor.cleanupTokens(), { debug }),
|
||||
onTick: async () => await safe(janitor.cleanupTokens(), { debug: log }),
|
||||
start: true
|
||||
});
|
||||
|
||||
gJobs.cleanupOidc = CronJob.from({
|
||||
cronTime: '00 10 * * * *', // every hour ten minutes past
|
||||
onTick: async () => await safe(oidcServer.cleanupExpired(), { debug }),
|
||||
onTick: async () => await safe(oidcServer.cleanupExpired(), { debug: log }),
|
||||
start: true
|
||||
});
|
||||
|
||||
@@ -210,7 +210,7 @@ async function startJobs() {
|
||||
cronTime: DEFAULT_CLEANUP_BACKUPS_PATTERN,
|
||||
onTick: async () => {
|
||||
for (const backupSite of await backupSites.list()) {
|
||||
await safe(backupSites.startCleanupTask(backupSite, AuditSource.CRON), { debug });
|
||||
await safe(backupSites.startCleanupTask(backupSite, AuditSource.CRON), { debug: log });
|
||||
}
|
||||
},
|
||||
start: true
|
||||
@@ -218,50 +218,50 @@ async function startJobs() {
|
||||
|
||||
gJobs.cleanupEventlog = CronJob.from({
|
||||
cronTime: '00 */30 * * * *', // every 30 minutes
|
||||
onTick: async () => await safe(eventlog.cleanup({ creationTime: new Date(Date.now() - 90 * 60 * 24 * 60 * 1000) }), { debug }), // 90 days ago
|
||||
onTick: async () => await safe(eventlog.cleanup({ creationTime: new Date(Date.now() - 90 * 60 * 24 * 60 * 1000) }), { debug: log }), // 90 days ago
|
||||
start: true
|
||||
});
|
||||
|
||||
gJobs.dockerVolumeCleaner = CronJob.from({
|
||||
cronTime: '00 00 */12 * * *', // every 12 hours
|
||||
onTick: async () => await safe(janitor.cleanupDockerVolumes(), { debug }),
|
||||
onTick: async () => await safe(janitor.cleanupDockerVolumes(), { debug: log }),
|
||||
start: true
|
||||
});
|
||||
|
||||
gJobs.schedulerSync = CronJob.from({
|
||||
cronTime: constants.TEST ? '*/10 * * * * *' : '00 */1 * * * *', // every minute
|
||||
onTick: async () => await safe(scheduler.sync(), { debug }),
|
||||
onTick: async () => await safe(scheduler.sync(), { debug: log }),
|
||||
start: true
|
||||
});
|
||||
|
||||
// randomized per Cloudron based on hourlySeed
|
||||
gJobs.certificateRenew = CronJob.from({
|
||||
cronTime: `00 10 ${hour} * * *`,
|
||||
onTick: async () => await safe(reverseProxy.startRenewCerts({}, AuditSource.CRON), { debug }),
|
||||
onTick: async () => await safe(reverseProxy.startRenewCerts({}, AuditSource.CRON), { debug: log }),
|
||||
start: true
|
||||
});
|
||||
|
||||
gJobs.checkDomainConfigs = CronJob.from({
|
||||
cronTime: `00 ${minute} 5 * * *`, // once a day
|
||||
onTick: async () => await safe(domains.checkConfigs(AuditSource.CRON), { debug }),
|
||||
onTick: async () => await safe(domains.checkConfigs(AuditSource.CRON), { debug: log }),
|
||||
start: true
|
||||
});
|
||||
|
||||
gJobs.appHealthMonitor = CronJob.from({
|
||||
cronTime: '*/10 * * * * *', // every 10 seconds
|
||||
onTick: async () => await safe(appHealthMonitor.run(10), { debug }), // 10 is the max run time
|
||||
onTick: async () => await safe(appHealthMonitor.run(10), { debug: log }), // 10 is the max run time
|
||||
start: true
|
||||
});
|
||||
|
||||
gJobs.collectStats = CronJob.from({
|
||||
cronTime: '*/20 * * * * *', // every 20 seconds. if you change this, change carbon config
|
||||
onTick: async () => await safe(metrics.sendToGraphite(), { debug }),
|
||||
onTick: async () => await safe(metrics.sendToGraphite(), { debug: log }),
|
||||
start: true
|
||||
});
|
||||
|
||||
gJobs.subscriptionChecker = CronJob.from({
|
||||
cronTime: `00 ${minute} ${hour} * * *`, // once a day based on seed to randomize
|
||||
onTick: async () => await safe(appstore.checkSubscription(), { debug }),
|
||||
onTick: async () => await safe(appstore.checkSubscription(), { debug: log }),
|
||||
start: true
|
||||
});
|
||||
|
||||
@@ -289,7 +289,7 @@ async function stopJobs() {
|
||||
async function handleTimeZoneChanged(tz) {
|
||||
assert.strictEqual(typeof tz, 'string');
|
||||
|
||||
debug('handleTimeZoneChanged: recreating all jobs');
|
||||
log('handleTimeZoneChanged: recreating all jobs');
|
||||
await stopJobs();
|
||||
await scheduler.deleteJobs(); // have to re-create with new tz
|
||||
await startJobs();
|
||||
|
||||
Reference in New Issue
Block a user