From bff352a395c91f814c63382a46322fa847bd0880 Mon Sep 17 00:00:00 2001 From: Neshura Date: Sun, 11 Dec 2022 17:11:02 +0100 Subject: [PATCH] Replace foreach with for --- pages/services.tsx | 49 ++++++++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/pages/services.tsx b/pages/services.tsx index 3111d67..bdf57e9 100644 --- a/pages/services.tsx +++ b/pages/services.tsx @@ -86,7 +86,7 @@ async function getStatus(entry: Service, containers: Dockerode.ContainerInfo[]) await fetch(entry.href) .then((response) => { if (response.ok) { - switch(response.status) { + switch (response.status) { case 200: case 301: case 302: @@ -107,9 +107,10 @@ async function getStatus(entry: Service, containers: Dockerode.ContainerInfo[]) } // Type Docker else if (entry.type === ServiceType.docker) { - containers.forEach((container) => { + for (let i = 0; i < containers.length; i++) { + const container = containers[i]; // Docker API returns container names with / prepended - if (container.Names.includes("/" + entry.docker_container_name)) { + if (containers[i].Names.includes("/" + entry.docker_container_name)) { // so far only "running" is properly implemented, mroe cases to follow as needed switch (container.State) { case "running": @@ -119,6 +120,8 @@ async function getStatus(entry: Service, containers: Dockerode.ContainerInfo[]) console.log("Container Status " + container.State + " has no case implemented"); entry.status = ServiceStatus.offline; } + // cancel the for + break; } // If container name is not missing the container is set to offline else if (entry.docker_container_name !== null) { @@ -134,7 +137,7 @@ async function getStatus(entry: Service, containers: Dockerode.ContainerInfo[]) console.error("Container Name not specified"); entry.status = ServiceStatus.error; } - }) + } } // If no Type matches else { @@ -147,26 +150,26 @@ async function getStatus(entry: Service, containers: Dockerode.ContainerInfo[]) else if (entry.location === ServiceLocation.other) { // Currently uses the same handling as app type for the other location await fetch(entry.href) - .then((response) => { - if (response.ok) { - switch(response.status) { - case 200: - case 301: - case 302: - entry.status = ServiceStatus.online; - break; - default: - entry.status = ServiceStatus.offline; - } + .then((response) => { + if (response.ok) { + switch (response.status) { + case 200: + case 301: + case 302: + entry.status = ServiceStatus.online; + break; + default: + entry.status = ServiceStatus.offline; } - else { - entry.status = ServiceStatus.offline; - } - }) - .catch((error) => { - console.error("Error pinging Website: ", error); - entry.status = ServiceStatus.error; - }) + } + else { + entry.status = ServiceStatus.offline; + } + }) + .catch((error) => { + console.error("Error pinging Website: ", error); + entry.status = ServiceStatus.error; + }) } // If no Location matches else {