refs #4550 added keepFile flag, logging improved, code refactor
gitea/printnatura/pipeline/head This commit looks good Details

This commit is contained in:
Juan Ferrer 2022-12-25 13:34:18 +01:00
parent 22515cabb9
commit a3c79aa9b9
2 changed files with 23 additions and 9 deletions

View File

@ -1,6 +1,7 @@
debug: false
log: true
dryPrint: false
keepFile: false
concurrency: 4
reconnectTimeout: 10
refreshRate: 1000

View File

@ -24,7 +24,9 @@ class PrintServer {
const printnatura = colors.bgBlack.bold(' Print'.white + 'Natura '.green);
console.log(`${decoration} ${printnatura} ${decoration}`);
if (this.conf.dryPrint)
this.serverLog('log', 'Running in dry print mode! Documents won\'t be printed and PDFs not removed.'.yellow);
this.serverLog('log', 'Running in dry print mode, documents won\'t be printed'.yellow);
if (this.conf.keepFile)
this.serverLog('log', 'Keep file enabled, documents won\'t be deleted from disk'.yellow);
await this.init();
this.rejectionHandler = (err, p) => this.onRejection(err, p);
@ -34,8 +36,8 @@ class PrintServer {
setTimeout(() => this.poll());
}
async stop() {
process.off('unhandledRejection', this.rejectionHandler);
this.serverLog('log', 'Bye ( ◕ ‿ ◕ )っ'.green);
process.off('unhandledRejection', this.rejectionHandler);
await this.end();
}
async init() {
@ -54,6 +56,7 @@ class PrintServer {
}
async end() {
await this.api.post(`Accounts/logout`);
this.token = null;
if (this.pollTimeout) {
clearTimeout(this.pollTimeout);
this.pollTimeout = null;
@ -99,12 +102,12 @@ class PrintServer {
console.error(err);
}
async poll() {
let conf = this.conf;
const conf = this.conf;
this.pollTimeout = null;
if (this.dbDown) {
let conn;
try {
let conn;
try {
conn = await this.pool.getConnection();
await conn.ping();
@ -180,6 +183,8 @@ class PrintServer {
const conf = this.conf;
let jobData;
const args = {};
let tmpFilePath;
let tmpFileCreated = false;
const conn = await this.pool.getConnection();
try {
@ -246,8 +251,9 @@ class PrintServer {
const tmpPath = conf.tmpDir;
if (!fs.existsSync(tmpPath))
fs.mkdirSync(tmpPath)
const tmpFilePath = path.join(tmpPath, `job-${jobId}.pdf`);
tmpFilePath = path.join(tmpPath, `job-${jobId}.pdf`);
await fs.writeFile(tmpFilePath, pdfData, 'binary');
tmpFileCreated = true;
// Print PDF
const printCommand = `lp -d "${printer}" "${tmpFilePath}"`;
@ -255,14 +261,11 @@ class PrintServer {
try {
if (!conf.dryPrint) await pExec(printCommand);
} catch(err) {
await fs.unlink(tmpFilePath);
throw new Error(`Print error: ${err.message}`);
}
await conn.query(updateQuery, ['printed', null, jobId]);
this.jobLog(jobId, 'log', `report: ${jobData.report}, printer: ${printer}, get: ${url}`);
if (!conf.dryPrint) await fs.unlink(tmpFilePath);
this.jobLog(jobId, 'log', `${jobData.report}: '${printCommand}': GET ${url}`);
} catch (err) {
let message = err.message;
if (err.name === 'AxiosError' && err.code === 'ERR_BAD_REQUEST') {
@ -279,6 +282,16 @@ class PrintServer {
} finally {
conn.release();
}
if (!conf.keepFile) {
try {
const shouldDelete = tmpFileCreated
|| (tmpFilePath && await fs.pathExists(tmpFilePath));
if (shouldDelete) await fs.unlink(tmpFilePath);
} catch (err) {
this.jobLog(jobId, 'error', err.message);
}
}
}
jobLog(jobId, realm, message) {
this.log(`Job[${colors.yellow(jobId)}]`, realm, message);