refs #4550 added keepFile flag, logging improved, code refactor
gitea/printnatura/pipeline/head This commit looks good
Details
gitea/printnatura/pipeline/head This commit looks good
Details
This commit is contained in:
parent
22515cabb9
commit
a3c79aa9b9
|
@ -1,6 +1,7 @@
|
||||||
debug: false
|
debug: false
|
||||||
log: true
|
log: true
|
||||||
dryPrint: false
|
dryPrint: false
|
||||||
|
keepFile: false
|
||||||
concurrency: 4
|
concurrency: 4
|
||||||
reconnectTimeout: 10
|
reconnectTimeout: 10
|
||||||
refreshRate: 1000
|
refreshRate: 1000
|
||||||
|
|
|
@ -24,7 +24,9 @@ class PrintServer {
|
||||||
const printnatura = colors.bgBlack.bold(' Print'.white + 'Natura '.green);
|
const printnatura = colors.bgBlack.bold(' Print'.white + 'Natura '.green);
|
||||||
console.log(`${decoration} ${printnatura} ${decoration}`);
|
console.log(`${decoration} ${printnatura} ${decoration}`);
|
||||||
if (this.conf.dryPrint)
|
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();
|
await this.init();
|
||||||
this.rejectionHandler = (err, p) => this.onRejection(err, p);
|
this.rejectionHandler = (err, p) => this.onRejection(err, p);
|
||||||
|
@ -34,8 +36,8 @@ class PrintServer {
|
||||||
setTimeout(() => this.poll());
|
setTimeout(() => this.poll());
|
||||||
}
|
}
|
||||||
async stop() {
|
async stop() {
|
||||||
process.off('unhandledRejection', this.rejectionHandler);
|
|
||||||
this.serverLog('log', 'Bye ( ◕ ‿ ◕ )っ'.green);
|
this.serverLog('log', 'Bye ( ◕ ‿ ◕ )っ'.green);
|
||||||
|
process.off('unhandledRejection', this.rejectionHandler);
|
||||||
await this.end();
|
await this.end();
|
||||||
}
|
}
|
||||||
async init() {
|
async init() {
|
||||||
|
@ -54,6 +56,7 @@ class PrintServer {
|
||||||
}
|
}
|
||||||
async end() {
|
async end() {
|
||||||
await this.api.post(`Accounts/logout`);
|
await this.api.post(`Accounts/logout`);
|
||||||
|
this.token = null;
|
||||||
if (this.pollTimeout) {
|
if (this.pollTimeout) {
|
||||||
clearTimeout(this.pollTimeout);
|
clearTimeout(this.pollTimeout);
|
||||||
this.pollTimeout = null;
|
this.pollTimeout = null;
|
||||||
|
@ -99,12 +102,12 @@ class PrintServer {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
}
|
}
|
||||||
async poll() {
|
async poll() {
|
||||||
let conf = this.conf;
|
const conf = this.conf;
|
||||||
this.pollTimeout = null;
|
this.pollTimeout = null;
|
||||||
|
|
||||||
if (this.dbDown) {
|
if (this.dbDown) {
|
||||||
let conn;
|
|
||||||
try {
|
try {
|
||||||
|
let conn;
|
||||||
try {
|
try {
|
||||||
conn = await this.pool.getConnection();
|
conn = await this.pool.getConnection();
|
||||||
await conn.ping();
|
await conn.ping();
|
||||||
|
@ -180,6 +183,8 @@ class PrintServer {
|
||||||
const conf = this.conf;
|
const conf = this.conf;
|
||||||
let jobData;
|
let jobData;
|
||||||
const args = {};
|
const args = {};
|
||||||
|
let tmpFilePath;
|
||||||
|
let tmpFileCreated = false;
|
||||||
|
|
||||||
const conn = await this.pool.getConnection();
|
const conn = await this.pool.getConnection();
|
||||||
try {
|
try {
|
||||||
|
@ -246,8 +251,9 @@ class PrintServer {
|
||||||
const tmpPath = conf.tmpDir;
|
const tmpPath = conf.tmpDir;
|
||||||
if (!fs.existsSync(tmpPath))
|
if (!fs.existsSync(tmpPath))
|
||||||
fs.mkdirSync(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');
|
await fs.writeFile(tmpFilePath, pdfData, 'binary');
|
||||||
|
tmpFileCreated = true;
|
||||||
|
|
||||||
// Print PDF
|
// Print PDF
|
||||||
const printCommand = `lp -d "${printer}" "${tmpFilePath}"`;
|
const printCommand = `lp -d "${printer}" "${tmpFilePath}"`;
|
||||||
|
@ -255,14 +261,11 @@ class PrintServer {
|
||||||
try {
|
try {
|
||||||
if (!conf.dryPrint) await pExec(printCommand);
|
if (!conf.dryPrint) await pExec(printCommand);
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
await fs.unlink(tmpFilePath);
|
|
||||||
throw new Error(`Print error: ${err.message}`);
|
throw new Error(`Print error: ${err.message}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
await conn.query(updateQuery, ['printed', null, jobId]);
|
await conn.query(updateQuery, ['printed', null, jobId]);
|
||||||
this.jobLog(jobId, 'log', `report: ${jobData.report}, printer: ${printer}, get: ${url}`);
|
this.jobLog(jobId, 'log', `${jobData.report}: '${printCommand}': GET ${url}`);
|
||||||
|
|
||||||
if (!conf.dryPrint) await fs.unlink(tmpFilePath);
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
let message = err.message;
|
let message = err.message;
|
||||||
if (err.name === 'AxiosError' && err.code === 'ERR_BAD_REQUEST') {
|
if (err.name === 'AxiosError' && err.code === 'ERR_BAD_REQUEST') {
|
||||||
|
@ -279,6 +282,16 @@ class PrintServer {
|
||||||
} finally {
|
} finally {
|
||||||
conn.release();
|
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) {
|
jobLog(jobId, realm, message) {
|
||||||
this.log(`Job[${colors.yellow(jobId)}]`, realm, message);
|
this.log(`Job[${colors.yellow(jobId)}]`, realm, message);
|
||||||
|
|
Loading…
Reference in New Issue