chore: redirect watcher logs to app.log

This commit is contained in:
Nicolas Meienberger 2023-09-02 01:53:20 +02:00
parent 64ba3a292d
commit a778c7e3a5
2 changed files with 35 additions and 47 deletions

View file

@ -1,3 +1,4 @@
/* eslint-disable no-restricted-syntax */
/* eslint-disable no-await-in-loop */
import { Queue } from 'bullmq';
import fs from 'fs';
@ -80,30 +81,27 @@ export class SystemExecutors {
this.logger.info('Setting file permissions a+rwx on required files');
// Give permission to read and write to all files and folders for the current user
await Promise.all(
filesAndFolders.map(async (fileOrFolder) => {
if (await pathExists(fileOrFolder)) {
this.logger.info(`Setting permissions on ${fileOrFolder}`);
await execAsync(`chmod -R a+rwx ${fileOrFolder}`).catch(() => {
logger.fail(`Failed to set permissions on ${fileOrFolder}`);
});
this.logger.info(`Successfully set permissions on ${fileOrFolder}`);
}
}),
);
for (const fileOrFolder of filesAndFolders) {
if (await pathExists(fileOrFolder)) {
this.logger.info(`Setting permissions on ${fileOrFolder}`);
await execAsync(`chmod -R a+rwx ${fileOrFolder}`).catch(() => {
logger.fail(`Failed to set permissions on ${fileOrFolder}`);
});
this.logger.info(`Successfully set permissions on ${fileOrFolder}`);
}
}
this.logger.info('Setting file permissions 600 on required files');
await Promise.all(
files600.map(async (fileOrFolder) => {
if (await pathExists(fileOrFolder)) {
this.logger.info(`Setting permissions on ${fileOrFolder}`);
await execAsync(`chmod 600 ${fileOrFolder}`).catch(() => {
logger.fail(`Failed to set permissions on ${fileOrFolder}`);
});
}
}),
);
for (const fileOrFolder of files600) {
if (await pathExists(fileOrFolder)) {
this.logger.info(`Setting permissions on ${fileOrFolder}`);
await execAsync(`chmod 600 ${fileOrFolder}`).catch(() => {
logger.fail(`Failed to set permissions on ${fileOrFolder}`);
});
this.logger.info(`Successfully set permissions on ${fileOrFolder}`);
}
}
};
public cleanLogs = async () => {
@ -231,24 +229,18 @@ export class SystemExecutors {
this.logger.info('Reloading env variables...');
dotenv.config({ path: this.envFile, override: true });
// Stop and Remove container tipi if exists
spinner.setMessage('Stopping and removing containers...');
spinner.start();
this.logger.info('Stopping and removing tipi-db...');
await execAsync('docker rm -f tipi-db');
this.logger.info('Stopping and removing tipi-redis...');
await execAsync('docker rm -f tipi-redis');
this.logger.info('Stopping and removing tipi-dashboard...');
await execAsync('docker rm -f tipi-dashboard');
this.logger.info('Stopping and removing tipi-reverse-proxy...');
await execAsync('docker rm -f tipi-reverse-proxy');
spinner.done('Containers stopped and removed');
// Pull images
spinner.setMessage('Pulling images...');
spinner.start();
this.logger.info('Pulling images new images...');
await execAsync(`docker compose --env-file "${this.envFile}" pull`);
this.logger.info('Pulling new images...');
const { stdout, stderr } = await execAsync(`docker compose --env-file "${this.envFile}" pull`).catch((e) => {
this.logger.error(`Failed to pull images: ${e}`);
throw e;
});
this.logger.error('stdout: ', stdout);
if (stderr) {
this.logger.error(stderr);
}
spinner.done('Images pulled');
@ -268,12 +260,8 @@ export class SystemExecutors {
await generateTlsCertificates({ domain: envMap.get('LOCAL_DOMAIN') });
if (killWatchers) {
this.logger.info('Opening log files for watcher...');
const out = fs.openSync('./logs/watcher.log', 'a');
const err = fs.openSync('./logs/watcher.log', 'a');
this.logger.info('Starting watcher...');
const subprocess = spawn('./runtipi-cli', [process.argv[1] as string, 'watch'], { cwd: this.rootFolder, detached: true, stdio: ['ignore', out, err] });
const subprocess = spawn('./runtipi-cli', [process.argv[1] as string, 'watch'], { cwd: this.rootFolder, detached: true, stdio: ['ignore', 'ignore', 'ignore'] });
subprocess.unref();
}

View file

@ -11,7 +11,7 @@ const execAsync = promisify(exec);
const runCommand = async (jobData: unknown) => {
const { gid, uid } = getUserIds();
console.log(`Running command with uid ${uid} and gid ${gid}`);
fileLogger.info(`Running command with uid ${uid} and gid ${gid}`);
const { installApp, startApp, stopApp, uninstallApp, updateApp, regenerateAppEnv } = new AppExecutors();
const { cloneRepo, pullRepo } = new RepoExecutors();
@ -103,7 +103,7 @@ export const startWorker = async () => {
const worker = new Worker(
'events',
async (job) => {
console.log(`Processing job ${job.id} with data ${JSON.stringify(job.data)}`);
fileLogger.info(`Processing job ${job.id} with data ${JSON.stringify(job.data)}`);
const { message, success } = await runCommand(job.data);
return { success, stdout: message };
@ -112,18 +112,18 @@ export const startWorker = async () => {
);
worker.on('ready', () => {
console.log('Worker is ready');
fileLogger.info('Worker is ready');
});
worker.on('completed', (job) => {
console.log(`Job ${job.id} completed with result: ${JSON.stringify(job.returnvalue)}`);
fileLogger.info(`Job ${job.id} completed with result: ${JSON.stringify(job.returnvalue)}`);
});
worker.on('failed', (job) => {
console.error(`Job ${job?.id} failed with reason ${job?.failedReason}`);
fileLogger.error(`Job ${job?.id} failed with reason ${job?.failedReason}`);
});
worker.on('error', async (e) => {
console.error('An error occurred:', e);
fileLogger.error(`Worker error: ${e}`);
});
};