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;