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