From 6b90d3ef2853deac81a8041e95ffc334f7347556 Mon Sep 17 00:00:00 2001 From: Michael Shamoon <4887959+shamoon@users.noreply.github.com> Date: Thu, 22 Dec 2022 21:16:52 -0800 Subject: [PATCH] Handle docker server failures if others succeed --- src/utils/config/api-response.js | 6 +-- src/utils/config/service-helpers.js | 59 +++++++++++++++++------------ 2 files changed, 37 insertions(+), 28 deletions(-) diff --git a/src/utils/config/api-response.js b/src/utils/config/api-response.js index b8de959fc..a34505377 100644 --- a/src/utils/config/api-response.js +++ b/src/utils/config/api-response.js @@ -52,7 +52,7 @@ export async function servicesResponse() { discoveredServices = cleanServiceGroups(await servicesFromDocker()); } catch (e) { console.error("Failed to discover services, please check docker.yaml for errors or remove example entries."); - if (e) console.error(e); + if (e) console.error(e.toString()); discoveredServices = []; } @@ -60,7 +60,7 @@ export async function servicesResponse() { configuredServices = cleanServiceGroups(await servicesFromConfig()); } catch (e) { console.error("Failed to load services.yaml, please check for errors"); - if (e) console.error(e); + if (e) console.error(e.toString()); configuredServices = []; } @@ -68,7 +68,7 @@ export async function servicesResponse() { initialSettings = await getSettings(); } catch (e) { console.error("Failed to load settings.yaml, please check for errors"); - if (e) console.error(e); + if (e) console.error(e.toString()); initialSettings = {}; } diff --git a/src/utils/config/service-helpers.js b/src/utils/config/service-helpers.js index 623b7d6be..cccd010bd 100644 --- a/src/utils/config/service-helpers.js +++ b/src/utils/config/service-helpers.js @@ -44,39 +44,48 @@ export async function servicesFromDocker() { const serviceServers = await Promise.all( Object.keys(servers).map(async (serverName) => { - const docker = new Docker(getDockerArguments(serverName).conn); - const containers = await docker.listContainers({ - all: true, - }); - - // bad docker connections can result in a object? - // in any case, this ensures the result is the expected array - if (!Array.isArray(containers)) { - return []; - } + try { + const docker = new Docker(getDockerArguments(serverName).conn); + const containers = await docker.listContainers({ + all: true, + }); - const discovered = containers.map((container) => { - let constructedService = null; + // bad docker connections can result in a object? + // in any case, this ensures the result is the expected array + if (!Array.isArray(containers)) { + return []; + } - Object.keys(container.Labels).forEach((label) => { - if (label.startsWith("homepage.")) { - if (!constructedService) { - constructedService = { - container: container.Names[0].replace(/^\//, ""), - server: serverName, - }; + const discovered = containers.map((container) => { + let constructedService = null; + + Object.keys(container.Labels).forEach((label) => { + if (label.startsWith("homepage.")) { + if (!constructedService) { + constructedService = { + container: container.Names[0].replace(/^\//, ""), + server: serverName, + }; + } + shvl.set(constructedService, label.replace("homepage.", ""), container.Labels[label]); } - shvl.set(constructedService, label.replace("homepage.", ""), container.Labels[label]); - } - }); + }); - return constructedService; - }); + return constructedService; + }); - return { server: serverName, services: discovered.filter((filteredService) => filteredService) }; + return { server: serverName, services: discovered.filter((filteredService) => filteredService) }; + } catch (e) { + // a server failed, but others may succeed + return { server: serverName, services: [] }; + } }) ); + if (serviceServers.every(server => server.services.length === 0)) { + throw new Error('All docker servers failed to connect or returned no containers'); + } + const mappedServiceGroups = []; serviceServers.forEach((server) => {