From dc4fe4685acf0d2d3095be8dfcaecc2241460a49 Mon Sep 17 00:00:00 2001 From: Seth Foster Date: Tue, 10 Oct 2023 12:28:25 -0400 Subject: [PATCH] fix(odd): pretend to be online all the time (#13746) For some reason, when the ODD starts and the robot boots while not connected to any network, some (some!) requests to localhost will fail. It's really strange. One thing that seems to help is pretending to be online by overriding the navigator.onLine browser api to always return true. --------- Co-authored-by: Jamey H --- app-shell-odd/Makefile | 4 ++-- app/src/App/OnDeviceDisplayApp.tsx | 8 +++++++- app/src/App/__mocks__/hacks.ts | 1 + app/src/App/hacks.ts | 16 ++++++++++++++++ scripts/setup-global-mocks.js | 1 + 5 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 app/src/App/__mocks__/hacks.ts create mode 100644 app/src/App/hacks.ts diff --git a/app-shell-odd/Makefile b/app-shell-odd/Makefile index 5dd7ca92736..309beca156d 100644 --- a/app-shell-odd/Makefile +++ b/app-shell-odd/Makefile @@ -48,7 +48,7 @@ setup: .PHONY: clean clean: - shx rm -rf lib dist + shx rm -rf lib dist opentrons-robot-app.tar.gz # artifacts ##################################################################### @@ -75,7 +75,7 @@ push-ot3: dist-ot3 scp $(if $(ssh_key),-i $(ssh_key)) $(ssh_opts) -r ./opentrons-robot-app.tar.gz root@$(host): ssh $(if $(ssh_key),-i $(ssh_key)) $(ssh_opts) root@$(host) "mount -o remount,rw / && systemctl stop opentrons-robot-app && rm -rf /opt/opentrons-app && mkdir -p /opt/opentrons-app" ssh $(if $(ssh_key),-i $(ssh_key)) $(ssh_opts) root@$(host) "tar -xvf opentrons-robot-app.tar.gz -C /opt/opentrons-app/ && mount -o remount,ro / && systemctl start opentrons-robot-app && rm -rf opentrons-robot-app.tar.gz" - rm -rf opentrons-robot-app.tar.gz + # development ##################################################################### diff --git a/app/src/App/OnDeviceDisplayApp.tsx b/app/src/App/OnDeviceDisplayApp.tsx index 6cc63af8986..a8663707b93 100644 --- a/app/src/App/OnDeviceDisplayApp.tsx +++ b/app/src/App/OnDeviceDisplayApp.tsx @@ -47,9 +47,15 @@ import { useCurrentRunRoute, useProtocolReceiptToast } from './hooks' import { OnDeviceDisplayAppFallback } from './OnDeviceDisplayAppFallback' +import { hackWindowNavigatorOnLine } from './hacks' + import type { Dispatch } from '../redux/types' import type { RouteProps } from './types' +// forces electron to think we're online which means axios won't elide +// network calls to localhost. see ./hacks.ts for more. +hackWindowNavigatorOnLine() + export const onDeviceDisplayRoutes: RouteProps[] = [ { Component: InitialLoadingScreen, @@ -257,7 +263,7 @@ export const OnDeviceDisplayApp = (): JSX.Element => { // TODO (sb:6/12/23) Create a notification manager to set up preference and order of takeover modals return ( - + {isIdle ? ( diff --git a/app/src/App/__mocks__/hacks.ts b/app/src/App/__mocks__/hacks.ts new file mode 100644 index 00000000000..842209dfcba --- /dev/null +++ b/app/src/App/__mocks__/hacks.ts @@ -0,0 +1 @@ +export const hackWindowNavigatorOnLine = (): void => {} diff --git a/app/src/App/hacks.ts b/app/src/App/hacks.ts new file mode 100644 index 00000000000..696e7baec9a --- /dev/null +++ b/app/src/App/hacks.ts @@ -0,0 +1,16 @@ +// If the system boots while no network connection is available, then some requests to localhost +// hang eternally while connecting (so no request timeouts work either) and things get +// generally weird. Overriding the browser API to pretend to always be "online" fixes this. +// It makes sense; if "onLine" is false, that means that any network call is _guaranteed_ to fail +// so middlewares probably elide them; but we really want it to be true basically always because +// most of what we do is via localhost. +// +// This function is exposed in its own module so it can be mocked in testing +// since jest really doesn't like you doing this. + +export const hackWindowNavigatorOnLine = (): void => { + Object.defineProperty(window.navigator, 'onLine', { + get: () => true, + }) + window.dispatchEvent(new Event('online')) +} diff --git a/scripts/setup-global-mocks.js b/scripts/setup-global-mocks.js index 7f5188f9eb5..79333ac496c 100644 --- a/scripts/setup-global-mocks.js +++ b/scripts/setup-global-mocks.js @@ -16,6 +16,7 @@ jest.mock('../app/src/pages/Labware/helpers/getAllDefs') jest.mock('../app/src/logger') jest.mock('../app/src/App/portal') jest.mock('../app/src/redux/shell/remote') +jest.mock('../app/src/App/hacks') jest.mock('../app-shell/src/config') jest.mock('../app-shell/src/log') jest.mock('../app-shell-odd/src/config')