refs #4550 Code refactor, minor fixes
gitea/printnatura/pipeline/head This commit looks good
Details
gitea/printnatura/pipeline/head This commit looks good
Details
This commit is contained in:
parent
6a5b7f4a25
commit
d6fe231cea
|
@ -56,8 +56,10 @@ class PrintServer {
|
||||||
this.pool = await mysql.createPool(conf.db);
|
this.pool = await mysql.createPool(conf.db);
|
||||||
}
|
}
|
||||||
async end() {
|
async end() {
|
||||||
await this.api.post(`Accounts/logout`);
|
if (this.token) {
|
||||||
this.token = null;
|
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;
|
||||||
|
@ -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();
|
} else
|
||||||
|
await conn.rollback();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
await conn.rollback();
|
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,33 +215,40 @@ class PrintServer {
|
||||||
return value !== undefined ? value : match;
|
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 pdfData;
|
||||||
let url;
|
|
||||||
for (let attempts = 0; !pdfData && attempts < 2; attempts++) {
|
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 {
|
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') {
|
||||||
await this.getToken();
|
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
|
} 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 {
|
||||||
|
|
Loading…
Reference in New Issue