Skip to content

Commit

Permalink
Add lcu server checking to prevent unnecessary swagger request failures
Browse files Browse the repository at this point in the history
  • Loading branch information
Morilli authored and Hi-Ray committed Jan 29, 2021
1 parent 64a1b68 commit 67bb293
Showing 1 changed file with 36 additions and 24 deletions.
60 changes: 36 additions & 24 deletions app/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,35 +164,47 @@ function createWindow() {
return;
}

const { username, password, address, port } = LCUData;

/**
* During multiple restarts of the client the backend server is not instantly ready to
* serve requests so we delay a bit
* During the initial load of the client the backend server is not instantly ready
* to serve requests so we check the connection first
*/
setTimeout(async () => {
const { username, password, address, port } = LCUData;

let serverReady = false;
let retries = 6; // reasonable amount of retries
while (!serverReady && retries > 0) {
await instance
.get(`https://${username}:${password}@${address}:${port}/swagger/v2/swagger.json`)
.then((res) => {
swaggerJson = res.data;
swaggerEnabled = true;
})
.catch(() => {
console.log("Swagger request failed; assuming swagger is not enabled.");
.get(`https://${username}:${password}@${address}:${port}/`)
.catch((res) => {
if (res.errno !== 'ECONNREFUSED') {
serverReady = true;
} else {
retries--;
}
});
};

await instance
.get(`https://${username}:${password}@${address}:${port}/swagger/v2/swagger.json`)
.then((res) => {
swaggerJson = res.data;
swaggerEnabled = true;
})
.catch(() => {
console.log("Swagger request failed; assuming swagger is not enabled.")
});

/**
* If swagger is enabled send the swagger json to the fe for generation
* otherwise just prompt the user to allow us to end the users session.
*/
if (swaggerEnabled) {
console.log("lcuswaggeren");
mainWindow?.webContents.send("LCUCONNECT", swaggerJson);
} else {
console.log("lcuswaggerdis");
mainWindow?.webContents.send("BELCUREQUESTGETRESTARTLCU");
}
}, 5000);
/**
* If swagger is enabled send the swagger json to the fe for generation
* otherwise just prompt the user to allow us to end the users session.
*/
if (swaggerEnabled) {
console.log("lcuswaggeren");
mainWindow?.webContents.send("LCUCONNECT", swaggerJson);
} else {
console.log("lcuswaggerdis");
mainWindow?.webContents.send("BELCUREQUESTGETRESTARTLCU");
};
});

/**
Expand Down

0 comments on commit 67bb293

Please sign in to comment.