LibreOffice: Improve logging

This commit is contained in:
Richard Hansen 2021-09-29 23:10:40 -04:00
parent 76374bc489
commit e42e5457c1

View file

@ -22,17 +22,15 @@ const fs = require('fs').promises;
const log4js = require('log4js'); const log4js = require('log4js');
const os = require('os'); const os = require('os');
const path = require('path'); const path = require('path');
const runCmd = require('./run_cmd');
const settings = require('./Settings'); const settings = require('./Settings');
const spawn = require('child_process').spawn;
const libreOfficeLogger = log4js.getLogger('LibreOffice'); const logger = log4js.getLogger('LibreOffice');
const doConvertTask = async (task) => { const doConvertTask = async (task) => {
const tmpDir = os.tmpdir(); const tmpDir = os.tmpdir();
const p = runCmd([
libreOfficeLogger.debug( settings.soffice,
`Converting ${task.srcFile} to format ${task.type}. The result will be put in ${tmpDir}`);
const soffice = spawn(settings.soffice, [
'--headless', '--headless',
'--invisible', '--invisible',
'--nologo', '--nologo',
@ -43,34 +41,32 @@ const doConvertTask = async (task) => {
task.srcFile, task.srcFile,
'--outdir', '--outdir',
tmpDir, tmpDir,
]); ], {stdio: [
soffice.stdin.end(); null,
(line) => logger.info(`[${p.child.pid}] stdout: ${line}`),
(line) => logger.error(`[${p.child.pid}] stderr: ${line}`),
]});
logger.info(`[${p.child.pid}] Converting ${task.srcFile} to ${task.type} in ${tmpDir}`);
// Soffice/libreoffice is buggy and often hangs. // Soffice/libreoffice is buggy and often hangs.
// To remedy this we kill the spawned process after a while. // To remedy this we kill the spawned process after a while.
// TODO: Use the timeout option once support for Node.js < v15.13.0 is dropped. // TODO: Use the timeout option once support for Node.js < v15.13.0 is dropped.
const hangTimeout = setTimeout(() => { const hangTimeout = setTimeout(() => {
soffice.kill(); logger.error(`[${p.child.pid}] Conversion timed out; killing LibreOffice...`);
p.child.kill();
}, 120000); }, 120000);
let stdoutBuffer = ''; try {
soffice.stdout.on('data', (data) => { stdoutBuffer += data.toString(); }); await p;
soffice.stderr.on('data', (data) => { stdoutBuffer += data.toString(); }); } catch (err) {
await new Promise((resolve, reject) => { logger.error(`[${p.child.pid}] Conversion failed: ${err.stack || err}`);
soffice.on('exit', (code) => { throw err;
clearTimeout(hangTimeout); } finally {
if (code !== 0) { clearTimeout(hangTimeout);
const err = }
new Error(`LibreOffice died with exit code ${code} and message: ${stdoutBuffer}`); logger.info(`[${p.child.pid}] Conversion done.`);
libreOfficeLogger.error(err.stack);
return reject(err);
}
resolve();
});
});
const filename = path.basename(task.srcFile); const filename = path.basename(task.srcFile);
const sourceFile = `${filename.substr(0, filename.lastIndexOf('.'))}.${task.fileExtension}`; const sourceFile = `${filename.substr(0, filename.lastIndexOf('.'))}.${task.fileExtension}`;
const sourcePath = path.join(tmpDir, sourceFile); const sourcePath = path.join(tmpDir, sourceFile);
libreOfficeLogger.debug(`Renaming ${sourcePath} to ${task.destFile}`); logger.debug(`Renaming ${sourcePath} to ${task.destFile}`);
await fs.rename(sourcePath, task.destFile); await fs.rename(sourcePath, task.destFile);
}; };