refs #4550 Fix: Cannot read property 'method' of undefined
gitea/printnatura/pipeline/head This commit looks good Details

This commit is contained in:
Juan Ferrer 2022-12-22 17:39:34 +01:00
parent 00ea09c7e2
commit d633fecae1
3 changed files with 31 additions and 30 deletions

View File

@ -73,15 +73,15 @@ class PrintServer {
break; break;
} }
} }
async reconnect() { async reconnect() {
this.reconnectTimeout = null; this.reconnectTimeout = null;
try { try {
await this.init(); await this.init();
} catch (err) { } catch (err) {
this.reconnectTimeout = setTimeout( this.reconnectTimeout = setTimeout(
() => this.reconnect(), this.conf.reconnectTimeout * 1000); () => this.reconnect(), this.conf.reconnectTimeout * 1000);
} }
} }
async poll() { async poll() {
this.pollTimeout = null; this.pollTimeout = null;
try { try {
@ -94,12 +94,13 @@ class PrintServer {
async printJob() { async printJob() {
const conn = this.conn; const conn = this.conn;
const conf = this.conf; const conf = this.conf;
let printJob;
let jobId; let jobId;
let jobData;
const args = {};
try { try {
await conn.beginTransaction(); await conn.beginTransaction();
[[printJob]] = await conn.query(selectQuery); const [[printJob]] = await conn.query(selectQuery);
if (!printJob) { if (!printJob) {
await conn.rollback(); await conn.rollback();
return; return;
@ -107,6 +108,16 @@ class PrintServer {
jobId = printJob.id; jobId = printJob.id;
// Job data
const [[data]] = await conn.query(jobDataQuery, jobId);
jobData = data;
// Job arguments
const args = {};
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.query(updateQuery, ['printing', null, jobId]);
await conn.commit(); await conn.commit();
} catch (err) { } catch (err) {
@ -115,14 +126,6 @@ class PrintServer {
} }
try { 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 // Path params
const usedParams = new Set(); const usedParams = new Set();
const methodPath = jobData.method.replace(/{\w+}/g, function(match) { const methodPath = jobData.method.replace(/{\w+}/g, function(match) {
@ -135,10 +138,7 @@ class PrintServer {
let pdfData; let pdfData;
for (let attempts = 0; !pdfData && attempts < 2; attempts++) { for (let attempts = 0; !pdfData && attempts < 2; attempts++) {
// URL params // URL params
const params = { const params = {userFk: jobData.userFk};
access_token: this.token,
userFk: printJob.userFk
};
for (const key in args) { for (const key in args) {
if (!usedParams.has(key)) if (!usedParams.has(key))
params[key] = args[key]; params[key] = args[key];
@ -152,7 +152,8 @@ class PrintServer {
url: `${conf.salix.url}/api/${methodPath}?${urlParams.toString()}`, url: `${conf.salix.url}/api/${methodPath}?${urlParams.toString()}`,
responseType: 'arraybuffer', responseType: 'arraybuffer',
headers: { headers: {
'Accept': 'application/pdf' 'Accept': 'application/pdf',
'Authorization': this.token
} }
}); });
pdfData = response.data; pdfData = response.data;
@ -203,10 +204,10 @@ class PrintServer {
module.exports = PrintServer; module.exports = PrintServer;
function pExec(command) { function pExec(command) {
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
exec(command, function(err, stdout, stderr) { exec(command, function(err, stdout, stderr) {
if (err) return reject(err); if (err) return reject(err);
resolve({stdout, stderr}) resolve({stdout, stderr})
}); });
}); });
} }

View File

@ -1,4 +1,5 @@
SELECT r.name report, SELECT pq.workerFk userFk,
r.name report,
p.name printer, p.name printer,
r.method r.method
FROM printQueue pq FROM printQueue pq

View File

@ -1,5 +1,4 @@
SELECT pq.id, SELECT pq.id
pq.workerFk userFk
FROM printQueue pq FROM printQueue pq
JOIN report r ON r.id = pq.reportFk JOIN report r ON r.id = pq.reportFk
WHERE pq.statusCode = 'queued' WHERE pq.statusCode = 'queued'