diff --git a/config.yml b/config.yml index 562c416..2f99fd5 100644 --- a/config.yml +++ b/config.yml @@ -1,6 +1,7 @@ debug: false log: true dryPrint: false +keepFile: false concurrency: 4 reconnectTimeout: 10 refreshRate: 1000 diff --git a/print-server.js b/print-server.js index 7fbd432..200b716 100644 --- a/print-server.js +++ b/print-server.js @@ -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);