Skip to content

Commit

Permalink
Merge pull request #113 from adinhodovic/add-retries-on-network
Browse files Browse the repository at this point in the history
fix: Add simple retries on network errors
  • Loading branch information
czue authored Mar 15, 2023
2 parents 1adc446 + f63da59 commit b495ff8
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions celery_progress/static/celery_progress/celery_progress.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class CeleryProgressBar {
this.onNetworkError = options.onNetworkError || this.onError;
this.onHttpError = options.onHttpError || this.onError;
this.pollInterval = options.pollInterval || 500;
this.maxNetworkRetryAttempts = options.maxNetworkRetryAttempts | 5;
// Other options
this.barColors = Object.assign({}, this.constructor.getBarColorsDefault(), options.barColors);

Expand Down Expand Up @@ -138,11 +139,23 @@ class CeleryProgressBar {

async connect() {
let response;
try {
response = await fetch(this.progressUrl);
} catch (networkError) {
this.onNetworkError(this.progressBarElement, this.progressBarMessageElement, "Network Error");
throw networkError;
let success = false;
let error = null;
let attempts = 0;
while(!success && attempts < this.maxNetworkRetryAttempts) {
try {
response = await fetch(this.progressUrl);
success = true;
} catch (networkError) {
error = networkError;
this.onNetworkError(this.progressBarElement, this.progressBarMessageElement, "Network Error");
attempts++;
await new Promise(r => setTimeout(r, 1000));
}
}

if (!success) {
throw(error)
}

if (response.status === 200) {
Expand Down

0 comments on commit b495ff8

Please sign in to comment.