Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(app, app-shell, app-shell-odd): block initial HTTP request until successful MQTT subscription #15094

Merged
merged 2 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app-shell-odd/src/notifications/deserialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ export function sendDeserialized(
} catch {} // Prevents shell erroring during app shutdown event.
}

export function sendDeserializedRefetch(topic: NotifyTopic): void {
sendDeserialized(topic, { refetch: true })
}

export function sendDeserializedGenericError(topic: NotifyTopic): void {
sendDeserialized(topic, FAILURE_STATUSES.ECONNFAILED)
}
Expand Down
24 changes: 16 additions & 8 deletions app-shell-odd/src/notifications/subscribe.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import mqtt from 'mqtt'

import { connectionStore } from './store'
import { sendDeserialized, sendDeserializedGenericError } from './deserialize'
import {
sendDeserialized,
sendDeserializedGenericError,
sendDeserializedRefetch,
} from './deserialize'
import { notifyLog } from './notifyLog'

import type { NotifyTopic } from '@opentrons/app/src/redux/shell/types'
Expand Down Expand Up @@ -30,8 +34,8 @@ export function subscribe(topic: NotifyTopic): Promise<void> {
if (client == null) {
return Promise.reject(new Error('Expected hostData, received null.'))
}

if (
// The first time the client wants to subscribe on a robot to a particular topic.
else if (
!connectionStore.isActiveSub(topic) &&
!connectionStore.isPendingSub(topic)
) {
Expand All @@ -44,13 +48,15 @@ export function subscribe(topic: NotifyTopic): Promise<void> {
})
)
.catch((error: Error) => notifyLog.debug(error.message))
} else {
void waitUntilActiveOrErrored('subscription', topic).catch(
(error: Error) => {
}
// The client is either already subscribed or the subscription is currently pending.
else {
void waitUntilActiveOrErrored('subscription', topic)
.then(() => sendDeserializedRefetch(topic))
.catch((error: Error) => {
notifyLog.debug(error.message)
sendDeserializedGenericError(topic)
}
)
})
}
})
.catch((error: Error) => {
Expand All @@ -74,6 +80,8 @@ export function subscribe(topic: NotifyTopic): Promise<void> {
connectionStore
.setSubStatus(topic, 'subscribed')
.catch((error: Error) => notifyLog.debug(error.message))

sendDeserializedRefetch(topic)
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions app-shell/src/notifications/deserialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ export function sendDeserialized({
} catch {} // Prevents shell erroring during app shutdown event.
}

export function sendDeserializedRefetch(ip: string, topic: NotifyTopic): void {
sendDeserialized({
ip,
topic,
message: { refetch: true },
})
}

export function sendDeserializedGenericError(
ip: string,
topic: NotifyTopic
Expand Down
24 changes: 17 additions & 7 deletions app-shell/src/notifications/subscribe.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import mqtt from 'mqtt'

import { connectionStore } from './store'
import { sendDeserialized, sendDeserializedGenericError } from './deserialize'
import {
sendDeserialized,
sendDeserializedGenericError,
sendDeserializedRefetch,
} from './deserialize'
import { notifyLog } from './notifyLog'

import type { NotifyTopic } from '@opentrons/app/src/redux/shell/types'
Expand Down Expand Up @@ -36,8 +40,8 @@ export function subscribe(ip: string, topic: NotifyTopic): Promise<void> {
if (client == null) {
return Promise.reject(new Error('Expected hostData, received null.'))
}

if (
// The first time the client wants to subscribe on a robot to a particular topic.
else if (
!connectionStore.isActiveSub(robotName, topic) &&
!connectionStore.isPendingSub(robotName, topic)
) {
Expand All @@ -50,16 +54,20 @@ export function subscribe(ip: string, topic: NotifyTopic): Promise<void> {
})
)
.catch((error: Error) => notifyLog.debug(error.message))
} else {
}
// The client is either already subscribed or the subscription is currently pending.
else {
void waitUntilActiveOrErrored({
connection: 'subscription',
ip,
robotName,
topic,
}).catch((error: Error) => {
notifyLog.debug(error.message)
sendDeserializedGenericError(ip, topic)
})
.then(() => sendDeserializedRefetch(ip, topic))
.catch((error: Error) => {
notifyLog.debug(error.message)
sendDeserializedGenericError(ip, topic)
})
}
})
.catch((error: Error) => {
Expand All @@ -81,6 +89,8 @@ export function subscribe(ip: string, topic: NotifyTopic): Promise<void> {
connectionStore
.setSubStatus(ip, topic, 'subscribed')
.catch((error: Error) => notifyLog.debug(error.message))

sendDeserializedRefetch(ip, topic)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/resources/useNotifyService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export function useNotifyService<TData, TError = Error>({

React.useEffect(() => {
if (shouldUseNotifications) {
// Always fetch on initial mount.
// Always fetch on initial mount to keep latency as low as possible.
setRefetch('once')
appShellListener({
hostname,
Expand Down
Loading