Skip to content

Commit 8b0080c

Browse files
committed
Do 3 attempts for starting PIO Home Server
1 parent fa51b29 commit 8b0080c

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

src/home.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ async function findFreePort() {
8484
while (port < 9000) {
8585
inUse = await new Promise(resolve => {
8686
tcpPortUsed.check(port, HTTP_HOST)
87-
.then(inUse => {
88-
resolve(inUse);
87+
.then(result => {
88+
resolve(result);
8989
}, () => {
9090
return resolve(false);
9191
});
@@ -101,15 +101,32 @@ async function findFreePort() {
101101
export function isServerStarted() {
102102
return new Promise(resolve => {
103103
tcpPortUsed.check(HTTP_PORT, HTTP_HOST)
104-
.then(inUse => {
105-
resolve(inUse);
104+
.then(result => {
105+
resolve(result);
106106
}, () => {
107107
return resolve(false);
108108
});
109109
});
110110
}
111111

112112
export async function ensureServerStarted(options={}) {
113+
const maxAttempts = 3;
114+
let attemptNums = 0;
115+
let lastError = undefined;
116+
while (attemptNums < maxAttempts) {
117+
try {
118+
return await _ensureServerStarted(options);
119+
} catch (err) {
120+
lastError = err;
121+
console.warn(err);
122+
HTTP_PORT = 0;
123+
}
124+
attemptNums++;
125+
}
126+
throw lastError;
127+
}
128+
129+
async function _ensureServerStarted(options={}) {
113130
if (HTTP_PORT === 0) {
114131
HTTP_PORT = await findFreePort();
115132
}
@@ -123,6 +140,7 @@ export async function ensureServerStarted(options={}) {
123140
['home', '--port', HTTP_PORT, '--no-open'],
124141
(code, stdout, stderr) => {
125142
if (code !== 0) {
143+
HTTP_PORT = 0;
126144
return reject(stderr);
127145
}
128146
}

0 commit comments

Comments
 (0)