refs #4550 Code refactor, minor fixes
gitea/printnatura/pipeline/head This commit looks good Details

This commit is contained in:
Juan Ferrer 2022-12-27 11:40:30 +01:00
parent 6a5b7f4a25
commit d6fe231cea
1 changed files with 48 additions and 41 deletions

View File

@ -56,8 +56,10 @@ class PrintServer {
this.pool = await mysql.createPool(conf.db); this.pool = await mysql.createPool(conf.db);
} }
async end() { async end() {
if (this.token) {
await this.api.post(`Accounts/logout`); await this.api.post(`Accounts/logout`);
this.token = null; this.token = null;
}
if (this.pollTimeout) { if (this.pollTimeout) {
clearTimeout(this.pollTimeout); clearTimeout(this.pollTimeout);
this.pollTimeout = null; this.pollTimeout = null;
@ -116,9 +118,8 @@ class PrintServer {
if (this.dbDown) { if (this.dbDown) {
try { try {
let conn; const conn = await this.pool.getConnection();
try { try {
conn = await this.pool.getConnection();
await conn.ping(); await conn.ping();
this.dbDown = false; this.dbDown = false;
this.serverLog('log', 'DB connection recovered'.green); this.serverLog('log', 'DB connection recovered'.green);
@ -164,10 +165,13 @@ class PrintServer {
jobId = printJob.id; jobId = printJob.id;
await conn.query(updateQuery, ['printing', null, jobId]); await conn.query(updateQuery, ['printing', null, jobId]);
this.jobLog(jobId, 'debug', 'get: printing'); this.jobLog(jobId, 'debug', 'get: printing');
}
await conn.commit(); await conn.commit();
} catch (err) { } else
await conn.rollback(); await conn.rollback();
} catch (err) {
try {
await conn.rollback();
} catch (e) {}
if (jobId) if (jobId)
this.jobLog(jobId, 'error', err.message); this.jobLog(jobId, 'error', err.message);
throw err; throw err;
@ -185,23 +189,21 @@ class PrintServer {
let tmpFilePath; let tmpFilePath;
let tmpFileCreated = false; let tmpFileCreated = false;
const conn = await this.pool.getConnection(); let conn;
try { try {
conn = await this.pool.getConnection();
// Job data
await conn.beginTransaction(); await conn.beginTransaction();
try { try {
// Job data
const [[data]] = await conn.query(jobDataQuery, jobId); const [[data]] = await conn.query(jobDataQuery, jobId);
jobData = data; jobData = data;
// Job arguments
const [res] = await conn.query(jobArgsQuery, jobId); const [res] = await conn.query(jobArgsQuery, jobId);
for (const row of res) for (const row of res)
args[row.name] = row.value; args[row.name] = row.value;
} finally {
await conn.commit();
} catch (err) {
await conn.rollback(); await conn.rollback();
throw err;
} }
// Path params // Path params
@ -213,9 +215,6 @@ class PrintServer {
return value !== undefined ? value : match; return value !== undefined ? value : match;
}); });
let pdfData;
let url;
for (let attempts = 0; !pdfData && attempts < 2; attempts++) {
// URL params // URL params
const params = {userFk: jobData.userFk}; const params = {userFk: jobData.userFk};
for (const key in args) { for (const key in args) {
@ -224,22 +223,32 @@ class PrintServer {
} }
const urlParams = new URLSearchParams(params); const urlParams = new URLSearchParams(params);
url = `${methodPath}?${urlParams.toString()}`; const url = `${methodPath}?${urlParams.toString()}`;
this.jobLog(jobId, 'debug', `api: ${url}`); this.jobLog(jobId, 'debug', `api: ${url}`);
// Request // Request
let pdfData;
for (let attempts = 0; !pdfData && attempts < 2; attempts++) {
try { try {
const response = await this.api({ const res = await this.api({
method: 'get', method: 'get',
url, url,
responseType: 'arraybuffer', responseType: 'arraybuffer',
headers: {'Accept': 'application/pdf'} headers: {'Accept': 'application/pdf'}
}); });
pdfData = response.data; pdfData = res.data;
} }
catch (err) { catch (err) {
if (err.response?.statusText === 'Unauthorized') { if (err.name === 'AxiosError' && err.code === 'ERR_BAD_REQUEST') {
const res = err.response;
if (res.status === 401) { // Unauthorized
await this.getToken(); await this.getToken();
} else {
const resMessage = JSON.parse(res.data).error.message;
const resErr = new Error(`${err.message}: ${resMessage}`);
resErr.stack = err.stack;
throw resErr;
}
} else } else
throw err; throw err;
} }
@ -266,20 +275,18 @@ class PrintServer {
await conn.query(updateQuery, ['printed', null, jobId]); await conn.query(updateQuery, ['printed', null, jobId]);
this.jobLog(jobId, 'log', `${jobData.report}: '${printCommand}': GET ${url}`); this.jobLog(jobId, 'log', `${jobData.report}: '${printCommand}': GET ${url}`);
} catch (err) { } catch (err) {
let message = err.message; try {
if (err.name === 'AxiosError' && err.code === 'ERR_BAD_REQUEST') { await conn.query(updateQuery, ['error', err.message, jobId]);
const resMessage = JSON.parse(err.response.data).error.message; } catch (e) {
message = `${message}: ${resMessage}`; this.jobLog(jobId, 'error', e.message);
} }
await conn.query(updateQuery, ['error', message, jobId]); this.jobLog(jobId, 'error', err.message);
this.jobLog(jobId, 'error', message); const jobErr = new Error(`(${jobId}) ${err.message}`);
const jobErr = new Error(`(${jobId}) ${message}`);
jobErr.stack = err.stack; jobErr.stack = err.stack;
throw jobErr; throw jobErr;
} finally { } finally {
conn.release(); if (conn) conn.release();
if (!conf.keepFile) { if (!conf.keepFile) {
try { try {