From d173e451776e36c7660b7b4c9cc97392d82225c6 Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Thu, 3 Nov 2022 16:46:35 -0400 Subject: [PATCH] Fix starting of lens proxy Signed-off-by: Sebastian Malton --- src/common/utils/get-startable.ts | 24 +++++ .../runnables/lens-proxy/setup.injectable.ts | 8 +- .../startable-stoppable.injectable.ts | 90 +++++++++---------- .../lens-proxy/teardown.injectable.ts | 4 +- 4 files changed, 72 insertions(+), 54 deletions(-) create mode 100644 src/common/utils/get-startable.ts diff --git a/src/common/utils/get-startable.ts b/src/common/utils/get-startable.ts new file mode 100644 index 0000000000..e9ac79330b --- /dev/null +++ b/src/common/utils/get-startable.ts @@ -0,0 +1,24 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ + +export type Starter = (signal: AbortSignal) => Promise; + +export interface SingleStartable { + start: () => Promise; + stop: () => void; +} + +export function getSingleStartable(id: string, start: Starter): SingleStartable { + const controller = new AbortController(); + + return { + start: async () => { + await start(controller.signal); + }, + stop: () => { + controller.abort(); + }, + }; +} diff --git a/src/main/start-main-application/runnables/lens-proxy/setup.injectable.ts b/src/main/start-main-application/runnables/lens-proxy/setup.injectable.ts index 317d817df4..c0c7c92c87 100644 --- a/src/main/start-main-application/runnables/lens-proxy/setup.injectable.ts +++ b/src/main/start-main-application/runnables/lens-proxy/setup.injectable.ts @@ -4,18 +4,18 @@ */ import { getInjectable } from "@ogre-tools/injectable"; import { beforeApplicationIsLoadingInjectionToken } from "../../runnable-tokens/before-application-is-loading-injection-token"; -import setupLensProxyStartableStoppableInjectable from "./startable-stoppable.injectable"; +import setupLensProxyStartableInjectable from "./startable-stoppable.injectable"; const setupLensProxyInjectable = getInjectable({ id: "setup-lens-proxy", instantiate: (di) => { - const setupLensProxyStartableStoppable = di.inject(setupLensProxyStartableStoppableInjectable); + const setupLensProxyStartableStoppable = di.inject(setupLensProxyStartableInjectable); return { id: "setup-lens-proxy", - run: () => { - setupLensProxyStartableStoppable.start(); + run: async () => { + await setupLensProxyStartableStoppable.start(); }, }; }, diff --git a/src/main/start-main-application/runnables/lens-proxy/startable-stoppable.injectable.ts b/src/main/start-main-application/runnables/lens-proxy/startable-stoppable.injectable.ts index 9654289364..a4c96d2f85 100644 --- a/src/main/start-main-application/runnables/lens-proxy/startable-stoppable.injectable.ts +++ b/src/main/start-main-application/runnables/lens-proxy/startable-stoppable.injectable.ts @@ -5,7 +5,7 @@ import { getInjectable } from "@ogre-tools/injectable"; import loggerInjectable from "../../../../common/logger.injectable"; import { hasTypedProperty, isObject, isString } from "../../../../common/utils"; -import { getStartableStoppable } from "../../../../common/utils/get-startable-stoppable"; +import { getSingleStartable } from "../../../../common/utils/get-startable"; import isWindowsInjectable from "../../../../common/vars/is-windows.injectable"; import exitAppInjectable from "../../../electron-app/features/exit-app.injectable"; import showErrorPopupInjectable from "../../../electron-app/features/show-error-popup.injectable"; @@ -21,8 +21,8 @@ const getErrorMessage = (error: unknown, fallback: string): string => { return fallback; }; -const setupLensProxyStartableStoppableInjectable = getInjectable({ - id: "setup-lens-proxy-startable-stoppable", +const setupLensProxyStartableInjectable = getInjectable({ + id: "setup-lens-proxy-startable", instantiate: (di) => { const lensProxy = di.inject(lensProxyInjectable); const exitApp = di.inject(exitAppInjectable); @@ -32,60 +32,54 @@ const setupLensProxyStartableStoppableInjectable = getInjectable({ const showErrorPopup = di.inject(showErrorPopupInjectable); const buildVersion = di.inject(buildVersionInjectable); - return getStartableStoppable("setup-lens-proxy", () => { - const controller = new AbortController(); - - (async () => { - try { - logger.info("🔌 Starting LensProxy"); - await lensProxy.listen({ signal: controller.signal }); // lensProxy.port available - } catch (error) { - if (!controller.signal.aborted) { - showErrorPopup("Lens Error", `Could not start proxy: ${getErrorMessage(error, "unknown error")}`); - exitApp(); - } - - return; + return getSingleStartable("setup-lens-proxy", async (signal) => { + try { + logger.info("🔌 Starting LensProxy"); + await lensProxy.listen({ signal }); // lensProxy.port available + } catch (error) { + if (!signal.aborted) { + showErrorPopup("Lens Error", `Could not start proxy: ${getErrorMessage(error, "unknown error")}`); + exitApp(); } - // test proxy connection - try { - logger.info("🔎 Testing LensProxy connection ..."); - const versionFromProxy = await requestAppVersionViaProxy({ signal: controller.signal }); + return; + } - if (buildVersion.get() !== versionFromProxy) { - logger.error("Proxy server responded with invalid response"); + // test proxy connection + try { + logger.info("🔎 Testing LensProxy connection ..."); + const versionFromProxy = await requestAppVersionViaProxy({ signal }); - return exitApp(); - } - - logger.info("⚡ LensProxy connection OK"); - } catch (error) { - if (controller.signal.aborted) { - return; - } - - logger.error(`🛑 LensProxy: failed connection test: ${error}`); - - const hostsPath = isWindows - ? "C:\\windows\\system32\\drivers\\etc\\hosts" - : "/etc/hosts"; - const message = [ - `Failed connection test: ${error}`, - "Check to make sure that no other versions of Lens are running", - `Check ${hostsPath} to make sure that it is clean and that the localhost loopback is at the top and set to 127.0.0.1`, - "If you have HTTP_PROXY or http_proxy set in your environment, make sure that the localhost and the ipv4 loopback address 127.0.0.1 are added to the NO_PROXY environment variable.", - ]; - - showErrorPopup("Lens Proxy Error", message.join("\n\n")); + if (buildVersion.get() !== versionFromProxy) { + logger.error("Proxy server responded with invalid response"); return exitApp(); } - })(); - return () => controller.abort(); + logger.info("⚡ LensProxy connection OK"); + } catch (error) { + if (signal.aborted) { + return; + } + + logger.error(`🛑 LensProxy: failed connection test: ${error}`); + + const hostsPath = isWindows + ? "C:\\windows\\system32\\drivers\\etc\\hosts" + : "/etc/hosts"; + const message = [ + `Failed connection test: ${error}`, + "Check to make sure that no other versions of Lens are running", + `Check ${hostsPath} to make sure that it is clean and that the localhost loopback is at the top and set to 127.0.0.1`, + "If you have HTTP_PROXY or http_proxy set in your environment, make sure that the localhost and the ipv4 loopback address 127.0.0.1 are added to the NO_PROXY environment variable.", + ]; + + showErrorPopup("Lens Proxy Error", message.join("\n\n")); + + return exitApp(); + } }); }, }); -export default setupLensProxyStartableStoppableInjectable; +export default setupLensProxyStartableInjectable; diff --git a/src/main/start-main-application/runnables/lens-proxy/teardown.injectable.ts b/src/main/start-main-application/runnables/lens-proxy/teardown.injectable.ts index 55ef8e35f3..84cde3b0af 100644 --- a/src/main/start-main-application/runnables/lens-proxy/teardown.injectable.ts +++ b/src/main/start-main-application/runnables/lens-proxy/teardown.injectable.ts @@ -4,12 +4,12 @@ */ import { getInjectable } from "@ogre-tools/injectable"; import { beforeQuitOfBackEndInjectionToken } from "../../runnable-tokens/before-quit-of-back-end-injection-token"; -import setupLensProxyStartableStoppableInjectable from "./startable-stoppable.injectable"; +import setupLensProxyStartableInjectable from "./startable-stoppable.injectable"; const stopSettingUpLensProxyInjectable = getInjectable({ id: "stop-setting-up-lens-proxy", instantiate: (di) => { - const setupLensProxyStartableStoppable = di.inject(setupLensProxyStartableStoppableInjectable); + const setupLensProxyStartableStoppable = di.inject(setupLensProxyStartableInjectable); return { id: "stop-setting-up-lens-proxy",