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:
60
src/apps.js
60
src/apps.js
@@ -10,7 +10,7 @@ import crypto from 'node:crypto';
|
||||
import { CronTime } from 'cron';
|
||||
import dashboard from './dashboard.js';
|
||||
import database from './database.js';
|
||||
import debugModule from 'debug';
|
||||
import logger from './logger.js';
|
||||
import dns from './dns.js';
|
||||
import docker from './docker.js';
|
||||
import domains from './domains.js';
|
||||
@@ -40,7 +40,7 @@ import util from 'node:util';
|
||||
import volumes from './volumes.js';
|
||||
import _ from './underscore.js';
|
||||
|
||||
const debug = debugModule('box:apps');
|
||||
const { log, trace } = logger('apps');
|
||||
const shell = shellModule('apps');
|
||||
|
||||
const PORT_TYPE_TCP = 'tcp';
|
||||
@@ -306,7 +306,7 @@ function getDuplicateErrorDetails(errorMessage, locations, portBindings) {
|
||||
|
||||
const match = errorMessage.match(/Duplicate entry '(.*)' for key '(.*)'/);
|
||||
if (!match) {
|
||||
debug('Unexpected SQL error message.', errorMessage);
|
||||
log('Unexpected SQL error message.', errorMessage);
|
||||
return new BoxError(BoxError.DATABASE_ERROR, errorMessage);
|
||||
}
|
||||
|
||||
@@ -1058,7 +1058,7 @@ async function onTaskFinished(error, appId, installationState, taskId, auditSour
|
||||
|
||||
switch (installationState) {
|
||||
case ISTATE_PENDING_DATA_DIR_MIGRATION:
|
||||
if (success) await safe(services.rebuildService('sftp', auditSource), { debug });
|
||||
if (success) await safe(services.rebuildService('sftp', auditSource), { debug: log });
|
||||
break;
|
||||
case ISTATE_PENDING_UPDATE: {
|
||||
const fromManifest = success ? task.args[1].updateConfig.manifest : app.manifest;
|
||||
@@ -1071,8 +1071,8 @@ async function onTaskFinished(error, appId, installationState, taskId, auditSour
|
||||
}
|
||||
|
||||
// this can race with an new install task but very unlikely
|
||||
debug(`onTaskFinished: checking to stop unused services. hasPending: ${appTaskManager.hasPendingTasks()}`)
|
||||
if (!appTaskManager.hasPendingTasks()) safe(services.stopUnusedServices(), { debug });
|
||||
log(`onTaskFinished: checking to stop unused services. hasPending: ${appTaskManager.hasPendingTasks()}`)
|
||||
if (!appTaskManager.hasPendingTasks()) safe(services.stopUnusedServices(), { debug: log });
|
||||
}
|
||||
|
||||
async function getCount() {
|
||||
@@ -1452,7 +1452,7 @@ async function startExec(app, execId, options) {
|
||||
// there is a race where resizing too early results in a 404 "no such exec"
|
||||
// https://git.cloudron.io/cloudron/box/issues/549
|
||||
setTimeout(async function () {
|
||||
await safe(docker.resizeExec(execId, { h: options.rows, w: options.columns }, { debug }));
|
||||
await safe(docker.resizeExec(execId, { h: options.rows, w: options.columns }, { debug: log }));
|
||||
}, 2000);
|
||||
}
|
||||
|
||||
@@ -1568,7 +1568,7 @@ async function uploadFile(app, sourceFilePath, destFilePath) {
|
||||
// the built-in bash printf understands "%q" but not /usr/bin/printf.
|
||||
// ' gets replaced with '\'' . the first closes the quote and last one starts a new one
|
||||
const escapedDestFilePath = await shell.bash(`printf %q '${destFilePath.replace(/'/g, '\'\\\'\'')}'`, { encoding: 'utf8' });
|
||||
debug(`uploadFile: ${sourceFilePath} -> ${escapedDestFilePath}`);
|
||||
log(`uploadFile: ${sourceFilePath} -> ${escapedDestFilePath}`);
|
||||
|
||||
const execId = await createExec(app, { cmd: [ 'bash', '-c', `cat - > ${escapedDestFilePath}` ], tty: false });
|
||||
const destStream = await startExec(app, execId, { tty: false });
|
||||
@@ -1702,9 +1702,9 @@ async function scheduleTask(appId, installationState, taskId, auditSource) {
|
||||
const options = { timeout: 20 * 60 * 60 * 1000 /* 20 hours */, nice: 15, memoryLimit };
|
||||
|
||||
appTaskManager.scheduleTask(appId, taskId, options, async function (error) {
|
||||
debug(`scheduleTask: task ${taskId} of ${appId} completed. error: %o`, error);
|
||||
log(`scheduleTask: task ${taskId} of ${appId} completed. error: %o`, error);
|
||||
if (error?.code === tasks.ECRASHED || error?.code === tasks.ESTOPPED) { // if task crashed, update the error
|
||||
debug(`Apptask crashed/stopped: ${error.message}`);
|
||||
log(`Apptask crashed/stopped: ${error.message}`);
|
||||
const appError = {
|
||||
message: error.message,
|
||||
reason: BoxError.TASK_ERROR,
|
||||
@@ -1713,12 +1713,12 @@ async function scheduleTask(appId, installationState, taskId, auditSource) {
|
||||
taskId,
|
||||
installationState
|
||||
};
|
||||
await safe(update(appId, { installationState: ISTATE_ERROR, error: appError, taskId: null }), { debug });
|
||||
await safe(update(appId, { installationState: ISTATE_ERROR, error: appError, taskId: null }), { debug: log });
|
||||
} else if (!(installationState === ISTATE_PENDING_UNINSTALL && !error)) { // clear out taskId except for successful uninstall
|
||||
await safe(update(appId, { taskId: null }), { debug });
|
||||
await safe(update(appId, { taskId: null }), { debug: log });
|
||||
}
|
||||
|
||||
await safe(onTaskFinished(error, appId, installationState, taskId, auditSource), { debug }); // ignore error
|
||||
await safe(onTaskFinished(error, appId, installationState, taskId, auditSource), { debug: log }); // ignore error
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1740,7 +1740,7 @@ async function addTask(appId, installationState, task, auditSource) {
|
||||
if (updateError && updateError.reason === BoxError.NOT_FOUND) throw new BoxError(BoxError.BAD_STATE, 'Another task is scheduled for this app'); // could be because app went away OR a taskId exists
|
||||
if (updateError) throw updateError;
|
||||
|
||||
if (scheduleNow) await safe(scheduleTask(appId, installationState, taskId, auditSource), { debug }); // ignore error
|
||||
if (scheduleNow) await safe(scheduleTask(appId, installationState, taskId, auditSource), { debug: log }); // ignore error
|
||||
|
||||
return taskId;
|
||||
}
|
||||
@@ -1882,7 +1882,7 @@ async function install(data, auditSource) {
|
||||
if (constants.DEMO && (await getCount() >= constants.DEMO_APP_LIMIT)) throw new BoxError(BoxError.BAD_STATE, 'Too many installed apps, please uninstall a few and try again');
|
||||
|
||||
const appId = crypto.randomUUID();
|
||||
debug(`Installing app ${appId}`);
|
||||
log(`Installing app ${appId}`);
|
||||
|
||||
const app = {
|
||||
accessRestriction,
|
||||
@@ -2566,7 +2566,7 @@ async function exportApp(app, backupSiteId, auditSource) {
|
||||
if (!canBackupApp(app)) throw new BoxError(BoxError.BAD_STATE, 'App cannot be backed up in this state');
|
||||
|
||||
const taskId = await tasks.add(`${tasks.TASK_APP_BACKUP_PREFIX}${app.id}`, [ appId, backupSiteId, { snapshotOnly: true } ]);
|
||||
safe(tasks.startTask(taskId, {}), { debug }); // background
|
||||
safe(tasks.startTask(taskId, {}), { debug: log }); // background
|
||||
return { taskId };
|
||||
}
|
||||
|
||||
@@ -2887,7 +2887,7 @@ async function getBackupDownloadStream(app, backupId) {
|
||||
|
||||
const stream = await backupSites.storageApi(backupSite).download(backupSite.config, downloadBackup.remotePath);
|
||||
stream.on('error', function(error) {
|
||||
debug(`getBackupDownloadStream: read stream error: ${error.message}`);
|
||||
log(`getBackupDownloadStream: read stream error: ${error.message}`);
|
||||
ps.emit('error', new BoxError(BoxError.EXTERNAL_ERROR, error));
|
||||
});
|
||||
stream.pipe(ps);
|
||||
@@ -2926,11 +2926,11 @@ async function restoreApps(apps, options, auditSource) {
|
||||
requireNullTaskId: false // ignore existing stale taskId
|
||||
};
|
||||
|
||||
debug(`restoreApps: marking ${app.fqdn} as ${installationState} using restore config ${JSON.stringify(restoreConfig)}`);
|
||||
log(`restoreApps: marking ${app.fqdn} as ${installationState} using restore config ${JSON.stringify(restoreConfig)}`);
|
||||
|
||||
const [addTaskError, taskId] = await safe(addTask(app.id, installationState, task, auditSource));
|
||||
if (addTaskError) debug(`restoreApps: error marking ${app.fqdn} for restore: ${JSON.stringify(addTaskError)}`);
|
||||
else debug(`restoreApps: marked ${app.id} as ${installationState} with taskId ${taskId}`);
|
||||
if (addTaskError) log(`restoreApps: error marking ${app.fqdn} for restore: ${JSON.stringify(addTaskError)}`);
|
||||
else log(`restoreApps: marked ${app.id} as ${installationState} with taskId ${taskId}`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2945,7 +2945,7 @@ async function configureApps(apps, options, auditSource) {
|
||||
const scheduleNow = !!options.scheduleNow;
|
||||
|
||||
for (const app of apps) {
|
||||
debug(`configureApps: marking ${app.fqdn} for reconfigure (scheduleNow: ${scheduleNow})`);
|
||||
log(`configureApps: marking ${app.fqdn} for reconfigure (scheduleNow: ${scheduleNow})`);
|
||||
|
||||
const task = {
|
||||
args: {},
|
||||
@@ -2955,8 +2955,8 @@ async function configureApps(apps, options, auditSource) {
|
||||
};
|
||||
|
||||
const [addTaskError, taskId] = await safe(addTask(app.id, ISTATE_PENDING_CONFIGURE, task, auditSource));
|
||||
if (addTaskError) debug(`configureApps: error marking ${app.fqdn} for configure: ${JSON.stringify(addTaskError)}`);
|
||||
else debug(`configureApps: marked ${app.id} for re-configure with taskId ${taskId}`);
|
||||
if (addTaskError) log(`configureApps: error marking ${app.fqdn} for configure: ${JSON.stringify(addTaskError)}`);
|
||||
else log(`configureApps: marked ${app.id} for re-configure with taskId ${taskId}`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2972,7 +2972,7 @@ async function restartAppsUsingAddons(changedAddons, auditSource) {
|
||||
apps = apps.filter(app => app.runState !== RSTATE_STOPPED); // don't start stopped apps
|
||||
|
||||
for (const app of apps) {
|
||||
debug(`restartAppsUsingAddons: marking ${app.fqdn} for restart`);
|
||||
log(`restartAppsUsingAddons: marking ${app.fqdn} for restart`);
|
||||
|
||||
const task = {
|
||||
args: {},
|
||||
@@ -2981,27 +2981,27 @@ async function restartAppsUsingAddons(changedAddons, auditSource) {
|
||||
|
||||
// stop apps before updating the databases because postgres will "lock" them preventing import
|
||||
const [stopError] = await safe(docker.stopContainers(app.id));
|
||||
if (stopError) debug(`restartAppsUsingAddons: error stopping ${app.fqdn}`, stopError);
|
||||
if (stopError) log(`restartAppsUsingAddons: error stopping ${app.fqdn}`, stopError);
|
||||
|
||||
const [addTaskError, taskId] = await safe(addTask(app.id, ISTATE_PENDING_RESTART, task, auditSource));
|
||||
if (addTaskError) debug(`restartAppsUsingAddons: error marking ${app.fqdn} for restart: ${JSON.stringify(addTaskError)}`);
|
||||
else debug(`restartAppsUsingAddons: marked ${app.id} for restart with taskId ${taskId}`);
|
||||
if (addTaskError) log(`restartAppsUsingAddons: error marking ${app.fqdn} for restart: ${JSON.stringify(addTaskError)}`);
|
||||
else log(`restartAppsUsingAddons: marked ${app.id} for restart with taskId ${taskId}`);
|
||||
}
|
||||
}
|
||||
|
||||
async function schedulePendingTasks(auditSource) {
|
||||
assert.strictEqual(typeof auditSource, 'object');
|
||||
|
||||
debug('schedulePendingTasks: scheduling app tasks');
|
||||
log('schedulePendingTasks: scheduling app tasks');
|
||||
|
||||
const result = await list();
|
||||
|
||||
for (const app of result) {
|
||||
if (!app.taskId) continue; // if not in any pending state, do nothing
|
||||
|
||||
debug(`schedulePendingTasks: schedule task for ${app.fqdn} ${app.id}: state=${app.installationState},taskId=${app.taskId}`);
|
||||
log(`schedulePendingTasks: schedule task for ${app.fqdn} ${app.id}: state=${app.installationState},taskId=${app.taskId}`);
|
||||
|
||||
await safe(scheduleTask(app.id, app.installationState, app.taskId, auditSource), { debug }); // ignore error
|
||||
await safe(scheduleTask(app.id, app.installationState, app.taskId, auditSource), { debug: log }); // ignore error
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user