From 83bfed29c50ad4c95af7f7b45dcb41b56e05aca9 Mon Sep 17 00:00:00 2001 From: Edoardo Pirovano Date: Wed, 7 Feb 2024 12:13:06 +0000 Subject: [PATCH] Fix connection check now that we're using a proxy --- package.json | 4 ++- src/utils/check-connection.ts | 63 ++++++++++++++++++++--------------- src/utils/results-reporter.ts | 2 +- yarn.lock | 10 ++++++ 4 files changed, 51 insertions(+), 28 deletions(-) diff --git a/package.json b/package.json index 2f22e02b..a4c9a572 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,8 @@ "@sentry/node": "^7.50.0", "lodash.debounce": "^4.0.8", "loglevel": "^1.8.1", - "luxon": "^3.4.3" + "luxon": "^3.4.3", + "retry": "^0.13.1" }, "devDependencies": { "@alwaysmeticulous/api": "^2.97.0", @@ -43,6 +44,7 @@ "@types/jest": "^27.0.3", "@types/lodash.debounce": "^4.0.7", "@types/luxon": "^3.3.2", + "@types/retry": "^0.12.5", "@types/node": "^20.2.5", "@typescript-eslint/eslint-plugin": "^5.62.0", "@typescript-eslint/parser": "^5.62.0", diff --git a/src/utils/check-connection.ts b/src/utils/check-connection.ts index 69651c06..43c8a527 100644 --- a/src/utils/check-connection.ts +++ b/src/utils/check-connection.ts @@ -1,33 +1,44 @@ -import { Socket } from "net"; +import * as retry from "retry"; -export const throwIfCannotConnectToOrigin = async (url: string) => { - const { hostname, port, protocol, origin } = new URL(url); - const defaultPortForProtocol = protocol === "https:" ? 443 : 80; - const portNumber = - port != null && port != "" ? Number(port) : defaultPortForProtocol; - const connectionAccepted = await canConnectTo(hostname, portNumber); - if (!connectionAccepted) { - throw new Error( - `Could not connect to '${hostname}:${portNumber}'. Please check:\n\n` + - `1. The server running at '${origin}' has fully started by the time the Meticulous action starts. You may need to add a 'sleep 30' after starting the server to ensure that this is the case.\n` + - `2. The server running at '${origin}' is using tcp instead of tcp6. You can use 'netstat -tulpen' to see what addresses and ports it is bound to.\n\n` +export const throwIfCannotConnectToOrigin = async (appUrl: string) => { + // Wait 1s, 2s, 4s, 8s, 16s, 32s, 64s for a total of just over 2 minutes + const operation = retry.operation({ + retries: 7, + factor: 2, + minTimeout: 1_000, + }); + const url = new URL(appUrl); + operation.attempt(async () => { + if (await canConnectTo(url)) { + return; + } + operation.retry( + new Error( + `Could not connect to '${appUrl}'. Please check:\n\n` + + `1. The server running at '${origin}' has fully started by the time the Meticulous action starts. You may need to add a 'sleep 30' after starting the server to ensure that this is the case.\n` + + `2. The server running at '${origin}' is using tcp instead of tcp6. You can use 'netstat -tulpen' to see what addresses and ports it is bound to.\n\n` + ) ); + }); +}; + +const canConnectTo = async (url: URL) => { + try { + const result = await fetchWithTimeout(url); + return result.status !== 502; + } catch (error) { + return false; } }; -const canConnectTo = async (host: string, port: number, timeout = 5000) => { - return new Promise((resolve) => { - const socket = new Socket(); - const onError = () => { - socket.destroy(); - resolve(false); - }; +async function fetchWithTimeout(url: URL) { + const controller = new AbortController(); + const id = setTimeout(() => controller.abort(), 5000); - socket.setTimeout(timeout, onError); - socket.on("error", onError); - socket.connect(port, host, () => { - socket.end(); - resolve(true); - }); + const response = await fetch(url, { + signal: controller.signal, }); -}; + clearTimeout(id); + + return response; +} diff --git a/src/utils/results-reporter.ts b/src/utils/results-reporter.ts index fdc2c09f..6dc1bf1a 100644 --- a/src/utils/results-reporter.ts +++ b/src/utils/results-reporter.ts @@ -129,7 +129,7 @@ export class ResultsReporter { // Usually this means that the user has just set up Meticulous and is running it for the first time. await this.setStatusComment({ createIfDoesNotExist: true, - body: `🤖 Meticulous replayed ${testCaseResults.length} user sessions and [took ${totalScreenshotsTaken} visual snapshots](${testRun.url}). Meticulous did not run on ${this.options.baseRef} of the ${baseRefStr} branch and so there was nothing to compare against. + body: `🤖 Meticulous replayed ${testCaseResults.length} user sessions and [took ${totalScreenshotsTaken} visual snapshots](${testRun.url}). Meticulous did not run on ${this.options.baseSha} of the ${baseRefStr} branch and so there was nothing to compare against. \nPlease merge your pull request for setting up Meticulous in CI and ensure that it’s running on push events to the ${baseRefStr} branch.`, }); } diff --git a/yarn.lock b/yarn.lock index 7e1e799f..e5b2e598 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1772,6 +1772,11 @@ resolved "https://registry.yarnpkg.com/@types/prettier/-/prettier-2.7.2.tgz#6c2324641cc4ba050a8c710b2b251b377581fbf0" integrity sha512-KufADq8uQqo1pYKVIYzfKbJfBAc0sOeXqGbFaSpv8MRmC/zXgowNZmFcbngndGk922QDmOASEXUZCaY48gs4cg== +"@types/retry@^0.12.5": + version "0.12.5" + resolved "https://registry.yarnpkg.com/@types/retry/-/retry-0.12.5.tgz#f090ff4bd8d2e5b940ff270ab39fd5ca1834a07e" + integrity sha512-3xSjTp3v03X/lSQLkczaN9UIEwJMoMCA1+Nb5HfbJEQWogdeQIyVtTvxPXDQjZ5zws8rFQfVfRdz03ARihPJgw== + "@types/semver@^7.3.12": version "7.3.13" resolved "https://registry.yarnpkg.com/@types/semver/-/semver-7.3.13.tgz#da4bfd73f49bd541d28920ab0e2bf0ee80f71c91" @@ -5297,6 +5302,11 @@ retry@^0.12.0: resolved "https://registry.yarnpkg.com/retry/-/retry-0.12.0.tgz#1b42a6266a21f07421d1b0b54b7dc167b01c013b" integrity sha512-9LkiTwjUh6rT555DtE9rTX+BKByPfrMzEAtnlEtdEwr3Nkffwiihqe2bWADg+OQRjt9gl6ICdmB/ZFDCGAtSow== +retry@^0.13.1: + version "0.13.1" + resolved "https://registry.yarnpkg.com/retry/-/retry-0.13.1.tgz#185b1587acf67919d63b357349e03537b2484658" + integrity sha512-XQBQ3I8W1Cge0Seh+6gjj03LbmRFWuoszgK9ooCpwYIrhhoO80pfq4cUkU5DkknwfOfFteRwlZ56PYOGYyFWdg== + reusify@^1.0.4: version "1.0.4" resolved "https://registry.yarnpkg.com/reusify/-/reusify-1.0.4.tgz#90da382b1e126efc02146e90845a88db12925d76"