Merge pull request 'refs #4550 Fix: Cannot read property 'method' of undefined' (!6) from 4550-transactionSlaveFixes into master
gitea/printnatura/pipeline/head This commit looks good
Details
gitea/printnatura/pipeline/head This commit looks good
Details
Reviewed-on: #6
This commit is contained in:
commit
34413c7c57
141
print-server.js
141
print-server.js
|
@ -27,9 +27,7 @@ class PrintServer {
|
|||
await this.init();
|
||||
}
|
||||
async init() {
|
||||
this.conn = await mysql.createConnection(this.conf.db);
|
||||
this.dbErrorHandler = err => this.onDbError(err);
|
||||
this.conn.on('error', this.dbErrorHandler);
|
||||
this.pool = await mysql.createPool(this.conf.db);
|
||||
console.log('Connected to DB successfully.'.green);
|
||||
await this.poll();
|
||||
}
|
||||
|
@ -42,14 +40,7 @@ class PrintServer {
|
|||
clearTimeout(this.pollTimeout);
|
||||
this.pollTimeout = null;
|
||||
}
|
||||
if (this.reconnectTimeout) {
|
||||
clearTimeout(this.reconnectTimeout);
|
||||
this.reconnectTimeout = null;
|
||||
}
|
||||
this.conn.off('error', this.dbErrorHandler);
|
||||
// FIXME: mysql2/promise bug, conn.end() ends process
|
||||
this.conn.on('error', () => {});
|
||||
await this.conn.end();
|
||||
await this.pool.end();
|
||||
}
|
||||
async getToken() {
|
||||
const salix = this.conf.salix;
|
||||
|
@ -59,70 +50,72 @@ class PrintServer {
|
|||
});
|
||||
this.token = response.data.token;
|
||||
}
|
||||
async onDbError(err) {
|
||||
switch(err.code) {
|
||||
case 'PROTOCOL_CONNECTION_LOST':
|
||||
case 'ECONNRESET':
|
||||
case 1927: // ER_CONNECTION_KILLED
|
||||
console.error(`DB: ${err.message}`.red);
|
||||
try {
|
||||
await this.end();
|
||||
} catch(e) {}
|
||||
console.log('Waiting until DB is available again...'.yellow);
|
||||
await this.reconnect();
|
||||
break;
|
||||
}
|
||||
}
|
||||
async reconnect() {
|
||||
this.reconnectTimeout = null;
|
||||
try {
|
||||
await this.init();
|
||||
} catch (err) {
|
||||
this.reconnectTimeout = setTimeout(
|
||||
() => this.reconnect(), this.conf.reconnectTimeout * 1000);
|
||||
}
|
||||
}
|
||||
async poll() {
|
||||
let delay = this.conf.refreshRate;
|
||||
this.pollTimeout = null;
|
||||
|
||||
try {
|
||||
await this.printJob();
|
||||
const conn = await this.pool.getConnection();
|
||||
|
||||
try {
|
||||
if (this.dbDisconnected) {
|
||||
await conn.ping();
|
||||
this.dbDisconnected = false;
|
||||
console.log('DB connection recovered.'.green);
|
||||
}
|
||||
|
||||
if (await this.printJob(conn))
|
||||
delay = 0;
|
||||
} finally {
|
||||
await conn.release();
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
if (err.code === 'ETIMEDOUT') {
|
||||
delay = this.conf.reconnectTimeout;
|
||||
if (!this.dbDisconnected) {
|
||||
this.dbDisconnected = true;
|
||||
console.log(`DB connection lost: ${err.message}`.red);
|
||||
}
|
||||
} else
|
||||
console.error(err);
|
||||
}
|
||||
this.pollTimeout = setTimeout(() => this.poll(), this.conf.refreshRate);
|
||||
|
||||
this.pollTimeout = setTimeout(() => this.poll(), delay);
|
||||
}
|
||||
async printJob() {
|
||||
const conn = this.conn;
|
||||
async printJob(conn) {
|
||||
const conf = this.conf;
|
||||
let printJob;
|
||||
let jobId;
|
||||
|
||||
try {
|
||||
await conn.beginTransaction();
|
||||
[[printJob]] = await conn.query(selectQuery);
|
||||
if (!printJob) {
|
||||
let jobData;
|
||||
const args = {};
|
||||
|
||||
try {
|
||||
await conn.beginTransaction();
|
||||
const [[printJob]] = await conn.query(selectQuery);
|
||||
if (!printJob) {
|
||||
await conn.rollback();
|
||||
return;
|
||||
}
|
||||
|
||||
jobId = printJob.id;
|
||||
|
||||
// Job data
|
||||
const [[data]] = await conn.query(jobDataQuery, jobId);
|
||||
jobData = data;
|
||||
|
||||
// Job arguments
|
||||
const [res] = await conn.query(jobArgsQuery, jobId);
|
||||
for (const row of res)
|
||||
args[row.name] = row.value;
|
||||
|
||||
await conn.query(updateQuery, ['printing', null, jobId]);
|
||||
await conn.commit();
|
||||
} catch (err) {
|
||||
await conn.rollback();
|
||||
return;
|
||||
throw err;
|
||||
}
|
||||
|
||||
jobId = printJob.id;
|
||||
|
||||
await conn.query(updateQuery, ['printing', null, jobId]);
|
||||
await conn.commit();
|
||||
} catch (err) {
|
||||
await conn.rollback();
|
||||
throw err;
|
||||
}
|
||||
|
||||
try {
|
||||
// Job data
|
||||
// FIXME: Cannot read property 'method' of undefined
|
||||
const [[jobData]] = await conn.query(jobDataQuery, jobId);
|
||||
const args = {};
|
||||
const [res] = await conn.query(jobArgsQuery, jobId);
|
||||
for (const row of res)
|
||||
args[row.name] = row.value;
|
||||
|
||||
// Path params
|
||||
const usedParams = new Set();
|
||||
const methodPath = jobData.method.replace(/{\w+}/g, function(match) {
|
||||
|
@ -135,10 +128,7 @@ class PrintServer {
|
|||
let pdfData;
|
||||
for (let attempts = 0; !pdfData && attempts < 2; attempts++) {
|
||||
// URL params
|
||||
const params = {
|
||||
access_token: this.token,
|
||||
userFk: printJob.userFk
|
||||
};
|
||||
const params = {userFk: jobData.userFk};
|
||||
for (const key in args) {
|
||||
if (!usedParams.has(key))
|
||||
params[key] = args[key];
|
||||
|
@ -152,7 +142,8 @@ class PrintServer {
|
|||
url: `${conf.salix.url}/api/${methodPath}?${urlParams.toString()}`,
|
||||
responseType: 'arraybuffer',
|
||||
headers: {
|
||||
'Accept': 'application/pdf'
|
||||
'Accept': 'application/pdf',
|
||||
'Authorization': this.token
|
||||
}
|
||||
});
|
||||
pdfData = response.data;
|
||||
|
@ -175,7 +166,7 @@ class PrintServer {
|
|||
|
||||
// Print PDF
|
||||
try {
|
||||
await pExec(`lp -d "${printer}" "${tmpFilePath}"`);
|
||||
//await pExec(`lp -d "${printer}" "${tmpFilePath}"`);
|
||||
} catch(err) {
|
||||
await fs.unlink(tmpFilePath);
|
||||
throw new Error(`Print error: ${err.message}`);
|
||||
|
@ -197,16 +188,18 @@ class PrintServer {
|
|||
await conn.query(updateQuery, ['error', message, jobId]);
|
||||
throw new Error(`(${jobId}) ${message}`);
|
||||
}
|
||||
|
||||
return jobId;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = PrintServer;
|
||||
|
||||
function pExec(command) {
|
||||
return new Promise(function(resolve, reject) {
|
||||
exec(command, function(err, stdout, stderr) {
|
||||
if (err) return reject(err);
|
||||
resolve({stdout, stderr})
|
||||
});
|
||||
});
|
||||
return new Promise(function(resolve, reject) {
|
||||
exec(command, function(err, stdout, stderr) {
|
||||
if (err) return reject(err);
|
||||
resolve({stdout, stderr})
|
||||
});
|
||||
});
|
||||
}
|
|
@ -1,4 +1,5 @@
|
|||
SELECT r.name report,
|
||||
SELECT pq.workerFk userFk,
|
||||
r.name report,
|
||||
p.name printer,
|
||||
r.method
|
||||
FROM printQueue pq
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
SELECT pq.id,
|
||||
pq.workerFk userFk
|
||||
SELECT pq.id
|
||||
FROM printQueue pq
|
||||
JOIN report r ON r.id = pq.reportFk
|
||||
WHERE pq.statusCode = 'queued'
|
||||
|
|
Loading…
Reference in New Issue