From 0cdf85541c4bf97e2b5971cfbe803cf8aeaf300a Mon Sep 17 00:00:00 2001 From: guillermo Date: Wed, 8 Mar 2023 18:38:43 +0100 Subject: [PATCH] refs #5368 Added retryAttempts & retryTimeout --- README.md | 15 ++++++++++++--- config.yml | 2 ++ print-server.js | 21 ++++++++++++--------- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 809f191..4683f70 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,17 @@ Build Create file named "config.local.yml" and put your private configuration: ``` -debug: true +debug: false +log: true +dryPrint: false +keepFile: false +serverId: 1 +concurrency: 4 +reconnectTimeout: 10 +refreshRate: 1000 +tmpDir: /dev/shm/printnatura +retryAttempts: 3 +retryTimeout: 1000 db: host: localhost port: 3306 @@ -29,8 +39,7 @@ db: salix: url: http://localhost:3000 user: user -reconnectTimeout: 30 -refreshRate: 1000 + password: password ``` ## How to use diff --git a/config.yml b/config.yml index 6e63ea6..63e387c 100644 --- a/config.yml +++ b/config.yml @@ -7,6 +7,8 @@ concurrency: 4 reconnectTimeout: 10 refreshRate: 1000 tmpDir: /dev/shm/printnatura +retryAttempts: 3 +retryTimeout: 1500 db: host: localhost port: 3306 diff --git a/print-server.js b/print-server.js index ec0a85a..a0b5da8 100644 --- a/print-server.js +++ b/print-server.js @@ -239,7 +239,7 @@ class PrintServer { // Request let pdfData; - for (let attempts = 0; !pdfData && attempts < 3; attempts++) { + for (let attempts = 0; !pdfData && attempts < this.conf.retryAttempts; attempts++) { try { const res = await this.api({ method: 'get', @@ -250,28 +250,31 @@ class PrintServer { pdfData = res.data; } catch (err) { - if (err.name === 'AxiosError') { + if (err.name === 'AxiosError' && attempts < this.conf.retryAttempts - 1) { const res = err.response; switch(res.status) { case 401: // Unauthorized await this.getToken(); break; case 502 || 504: // Bad Gateway & Gateway Timeout + await new Promise( + resolve => setTimeout(resolve, this.conf.retryTimeout)); break; default: - const resMessage = JSON.parse(res.data).error.message; - const resErr = new Error(`${err.message}: ${resMessage}`); - resErr.stack = err.stack; - throw resErr; + try { + const resMessage = JSON.parse(res.data).error.message; + const resErr = new Error(`${err.message}: ${resMessage}`); + resErr.stack = err.stack; + throw resErr; + } catch (err) { + throw err; + } } } else throw err; } } - if (!pdfData) - throw 'Could not get pdf from server' - // Save PDF to disk const printer = jobData.printer; const tmpPath = conf.tmpDir;