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:
@@ -4,12 +4,12 @@ import BoxError from './boxerror.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 docker from './docker.js';
|
||||
import safe from 'safetydance';
|
||||
import _ from './underscore.js';
|
||||
|
||||
const debug = debugModule('box:scheduler');
|
||||
const { log, trace } = logger('scheduler');
|
||||
|
||||
|
||||
const gState = {}; // appId -> { containerId, schedulerConfig (manifest+crontab), cronjobs }
|
||||
@@ -17,12 +17,12 @@ const gSuspendedAppIds = new Set(); // suspended because some apptask is running
|
||||
|
||||
// TODO: this should probably also stop existing jobs to completely prevent race but the code is not re-entrant
|
||||
function suspendAppJobs(appId) {
|
||||
debug(`suspendAppJobs: ${appId}`);
|
||||
log(`suspendAppJobs: ${appId}`);
|
||||
gSuspendedAppIds.add(appId);
|
||||
}
|
||||
|
||||
function resumeAppJobs(appId) {
|
||||
debug(`resumeAppJobs: ${appId}`);
|
||||
log(`resumeAppJobs: ${appId}`);
|
||||
gSuspendedAppIds.delete(appId);
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ async function runTask(appId, taskName) {
|
||||
if (!error && data?.State?.Running === true) {
|
||||
const jobStartTime = new Date(data.State.StartedAt); // iso 8601
|
||||
if ((new Date() - jobStartTime) < JOB_MAX_TIME) return;
|
||||
debug(`runTask: ${containerName} is running too long, restarting`);
|
||||
log(`runTask: ${containerName} is running too long, restarting`);
|
||||
}
|
||||
|
||||
await docker.restartContainer(containerName);
|
||||
@@ -65,10 +65,10 @@ async function createJobs(app, schedulerConfig) {
|
||||
// stopJobs only deletes jobs since previous sync. This means that when box code restarts, none of the containers
|
||||
// are removed. The deleteContainer here ensures we re-create the cron containers with the latest config
|
||||
await safe(docker.deleteContainer(containerName)); // ignore error
|
||||
const [error] = await safe(docker.createSubcontainer(app, containerName, [ '/bin/sh', '-c', command ], {} /* options */), { debug });
|
||||
const [error] = await safe(docker.createSubcontainer(app, containerName, [ '/bin/sh', '-c', command ], {} /* options */), { debug: log });
|
||||
if (error && error.reason !== BoxError.ALREADY_EXISTS) continue;
|
||||
|
||||
debug(`createJobs: ${taskName} (${app.fqdn}) will run in container ${containerName}`);
|
||||
log(`createJobs: ${taskName} (${app.fqdn}) will run in container ${containerName}`);
|
||||
|
||||
let cronTime;
|
||||
if (schedule === '@service') {
|
||||
@@ -82,7 +82,7 @@ async function createJobs(app, schedulerConfig) {
|
||||
cronTime,
|
||||
onTick: async () => {
|
||||
const [taskError] = await safe(runTask(appId, taskName)); // put the app id in closure, so we don't use the outdated app object by mistake
|
||||
if (taskError) debug(`could not run task ${taskName} : ${taskError.message}`);
|
||||
if (taskError) log(`could not run task ${taskName} : ${taskError.message}`);
|
||||
},
|
||||
start: true,
|
||||
timeZone: tz
|
||||
@@ -105,15 +105,15 @@ async function deleteAppJobs(appId, appState) {
|
||||
|
||||
const containerName = `${appId}-${taskName}`;
|
||||
const [error] = await safe(docker.deleteContainer(containerName));
|
||||
if (error) debug(`deleteAppJobs: failed to delete task container with name ${containerName} : ${error.message}`);
|
||||
if (error) log(`deleteAppJobs: failed to delete task container with name ${containerName} : ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
async function deleteJobs() {
|
||||
for (const appId of Object.keys(gState)) {
|
||||
debug(`deleteJobs: removing jobs of ${appId}`);
|
||||
log(`deleteJobs: removing jobs of ${appId}`);
|
||||
const [error] = await safe(deleteAppJobs(appId, gState[appId]));
|
||||
if (error) debug(`deleteJobs: error stopping jobs of removed app ${appId}: ${error.message}`);
|
||||
if (error) log(`deleteJobs: error stopping jobs of removed app ${appId}: ${error.message}`);
|
||||
delete gState[appId];
|
||||
}
|
||||
}
|
||||
@@ -125,12 +125,12 @@ async function sync() {
|
||||
|
||||
const allAppIds = allApps.map(app => app.id);
|
||||
const removedAppIds = _.difference(Object.keys(gState), allAppIds);
|
||||
if (removedAppIds.length !== 0) debug(`sync: stopping jobs of removed apps ${JSON.stringify(removedAppIds)}`);
|
||||
if (removedAppIds.length !== 0) log(`sync: stopping jobs of removed apps ${JSON.stringify(removedAppIds)}`);
|
||||
|
||||
for (const appId of removedAppIds) {
|
||||
debug(`sync: removing jobs of ${appId}`);
|
||||
log(`sync: removing jobs of ${appId}`);
|
||||
const [error] = await safe(deleteAppJobs(appId, gState[appId]));
|
||||
if (error) debug(`sync: error stopping jobs of removed app ${appId}: ${error.message}`);
|
||||
if (error) log(`sync: error stopping jobs of removed app ${appId}: ${error.message}`);
|
||||
delete gState[appId];
|
||||
}
|
||||
|
||||
@@ -143,10 +143,10 @@ async function sync() {
|
||||
if (_.isEqual(appState.schedulerConfig, schedulerConfig) && appState.containerId === app.containerId) continue; // nothing changed
|
||||
}
|
||||
|
||||
debug(`sync: clearing jobs of ${app.id} (${app.fqdn})`);
|
||||
log(`sync: clearing jobs of ${app.id} (${app.fqdn})`);
|
||||
|
||||
const [error] = await safe(deleteAppJobs(app.id, appState));
|
||||
if (error) debug(`sync: error stopping jobs of ${app.id} : ${error.message}`);
|
||||
if (error) log(`sync: error stopping jobs of ${app.id} : ${error.message}`);
|
||||
|
||||
if (!schedulerConfig) { // updated app version removed scheduler addon
|
||||
delete gState[app.id];
|
||||
|
||||
Reference in New Issue
Block a user