refs #4550 DB Connection fixes
gitea/printnatura/pipeline/head This commit looks good Details

This commit is contained in:
Guillermo Bonet 2022-12-19 14:28:27 +01:00
parent da7c3c38c3
commit 449a23f080
1 changed files with 31 additions and 11 deletions

View File

@ -28,20 +28,28 @@ class PrintServer {
}
async init() {
this.conn = await mysql.createConnection(this.conf.db);
this.conn.on('error', err => this.onDbError(err));
this.dbErrorHandler = err => this.onDbError(err);
this.conn.on('error', this.dbErrorHandler);
console.log('Connected to DB successfully.'.green);
await this.poll();
}
async stop() {
await axios.post(`${this.conf.salix.url}/api/Accounts/logout?access_token=${this.token}`);
await this.end();
await axios.post(`${this.conf.salix.url}/api/Accounts/logout?access_token=${this.token}`);
}
async end() {
clearTimeout(this.pollTimeout);
await this.conn.end();
if (this.pollTimeout) {
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();
}
async getToken() {
const salix = this.conf.salix;
@ -53,10 +61,14 @@ class PrintServer {
}
async onDbError(err) {
switch(err.code) {
case 1927:
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;
}
@ -66,15 +78,16 @@ class PrintServer {
try {
await this.init();
} catch (err) {
this.reconnectTimeout = setTimeout(reconnect(), this.conf.reconnectTimeout * 1000);
this.reconnectTimeout = setTimeout(
() => this.reconnect(), this.conf.reconnectTimeout * 1000);
}
}
async poll() {
this.pollTimeout = null;
try {
await this.printJob();
} catch (err) {
console.error(err)
this.reconnect();
}
this.pollTimeout = setTimeout(() => this.poll(), this.conf.refreshRate);
}
@ -159,13 +172,14 @@ class PrintServer {
fs.mkdirSync(tmpPath)
const tmpFilePath = path.join(tmpPath, `${Math.random().toString(36).substring(7)}.pdf`);
await fs.writeFile(tmpFilePath, pdfData, 'binary');
const printCommand = `p -d "${printer}" "${tmpFilePath}"`;
// Print PDF
try {
await pExec(`lp -d "${printer}" "${tmpFilePath}"`);
// await pExec(printCommand);
} catch(err) {
await fs.unlink(tmpFilePath);
throw new Error(`The printer ${printer} is not installed: ${err.message}`);
throw new Error(`Print error: '${printCommand}': ${err.message}`);
}
await conn.query(updateQuery, ['printed', null, jobId]);
@ -175,8 +189,14 @@ class PrintServer {
await fs.unlink(tmpFilePath);
} catch (err) {
await this.conn.query(updateQuery, ['error', err.message, jobId]);
throw new Error(`(${jobId}) ${err.message}`);
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}`;
}
await conn.query(updateQuery, ['error', message, jobId]);
throw new Error(`(${jobId}) ${message}`);
}
}
}