Skip to content

Commit 0b9b173

Browse files
committed
feat(launcher): allow to retry launcher request
1 parent 02841cc commit 0b9b173

2 files changed

Lines changed: 20 additions & 6 deletions

File tree

js/src/ProcessLauncher/api.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@ a VTK or a ParaView backend.
1010
import ProcessLauncher from 'wslink/src/ProcessLauncher';
1111

1212
processLauncher = ProcessLauncher.newInstance({ endPoint: '/paraview' });
13+
14+
// Optionally you can provide a launcherRetry: [1000, 2000, 3000, 5000]
15+
// with the set of time to wait in ms before another retry.
16+
// Retries will only occured on a 503 response from the server.
1317
```
1418

15-
## ProcessLauncher.newInstance({ endPoint })
19+
## ProcessLauncher.newInstance({ endPoint, launcherRetry: [] })
1620

1721
Create a process launcher that will make requests to a remote
1822
server using the provided endpoint url.

js/src/ProcessLauncher/index.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,22 @@ const connections = [];
55

66
function ProcessLauncher(publicAPI, model) {
77
publicAPI.start = (config) => {
8-
var xhr = new XMLHttpRequest(),
9-
url = model.endPoint;
8+
const xhr = new XMLHttpRequest();
9+
const url = model.endPoint;
10+
if (!model._retry) {
11+
model._retry = config.launcherRetry || [];
12+
}
1013

1114
xhr.open('POST', url, true);
1215

1316
if (config.headers) {
14-
Object.entries(config.headers).forEach(([key, value]) =>
17+
model._headers = config.headers;
18+
delete config.headers;
19+
}
20+
if (model._headers) {
21+
Object.entries(model._headers).forEach(([key, value]) =>
1522
xhr.setRequestHeader(key, value)
1623
);
17-
delete config.headers;
1824
}
1925

2026
xhr.responseType = 'json';
@@ -27,8 +33,12 @@ function ProcessLauncher(publicAPI, model) {
2733
connections.push(response);
2834
publicAPI.fireProcessReady(response);
2935
return;
36+
} else if (xhr.status === 503 && model._retry.length > 0) {
37+
const timeout = model._retry.shift();
38+
setTimeout(publicAPI.start, timeout, config);
39+
} else {
40+
publicAPI.fireError(xhr);
3041
}
31-
publicAPI.fireError(xhr);
3242
};
3343

3444
xhr.onerror = (e) => {

0 commit comments

Comments
 (0)