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