-
Notifications
You must be signed in to change notification settings - Fork 678
add support for subscribing to execution store updates #6402
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
Open
tom-vx51
wants to merge
1
commit into
develop
Choose a base branch
from
feat/es-subscriptions
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
180 changes: 180 additions & 0 deletions
180
app/packages/core/src/subscription/useExecutionStoreSubscribe.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
import { resolveOperatorURI } from "@fiftyone/operators/src/operators"; | ||
import * as fos from "@fiftyone/state"; | ||
import { getEventSource } from "@fiftyone/utilities/src/fetch"; | ||
import { EventSourceMessage } from "@microsoft/fetch-event-source"; | ||
import { useCallback, useEffect, useRef, useState } from "react"; | ||
import { useRecoilCallback } from "recoil"; | ||
// Timeout duration (in milliseconds) after which the subscription is considered unhealthy if no 'ping' is received. | ||
const UNHEALTHY_SUBSCRIPTION_TIMEOUT = 30 * 1000; | ||
|
||
const EXECUTION_STORE_SUBSCRIBE_PATH = "/operators/subscribe-execution-store"; | ||
|
||
export type ExecutionStoreSubscribeCallback<T> = ( | ||
key: string, | ||
value: T, | ||
metadata: Record<string, unknown> | ||
) => void; | ||
|
||
/** | ||
* Custom hook to subscribe to an execution store using Server-Sent Events (SSE). | ||
* | ||
* @param operatorUri - The URI of the execution store subscription operator. | ||
* The operator must be a SSE operator. | ||
* @param callback - Function to call when a new message is received. | ||
* @param datasetId - Optional ID of the dataset to subscribe to. If not provided, the subscription will be global. | ||
* @returns An object containing: | ||
* - isSubscriptionHealthy: indicates the current health of the subscription. | ||
* - unsubscribe: function to terminate the subscription. | ||
* - resetSubscription: function to restart the subscription. | ||
*/ | ||
export const useExecutionStoreSubscribe = <T>({ | ||
operatorUri, | ||
callback, | ||
datasetId, | ||
}: { | ||
operatorUri: string; | ||
callback: ExecutionStoreSubscribeCallback<T>; | ||
datasetId?: string; | ||
}) => { | ||
// Use a ref to ensure the callback is stable over renders | ||
const callbackRef = useRef(callback); | ||
callbackRef.current = callback; | ||
|
||
const abortControllerRef = useRef(new AbortController()); | ||
|
||
const [isSubscriptionHealthy, setIsSubscriptionHealthy] = useState(false); | ||
|
||
const setSubscriptionNotHealthyTimerRef = useRef<number>(-1); | ||
|
||
/** | ||
* Handler for incoming SSE messages. | ||
* Clears any existing timeout and sets a new one when a 'ping' is received. | ||
* Otherwise, parses the event data and calls the provided callback. | ||
*/ | ||
const onMessageHandler = useCallback((event: EventSourceMessage) => { | ||
window.clearTimeout(setSubscriptionNotHealthyTimerRef.current); | ||
|
||
if (event.event === "ping") { | ||
setIsSubscriptionHealthy(true); | ||
|
||
setSubscriptionNotHealthyTimerRef.current = window.setTimeout(() => { | ||
setIsSubscriptionHealthy(false); | ||
}, UNHEALTHY_SUBSCRIPTION_TIMEOUT); | ||
|
||
return; | ||
} | ||
|
||
if (!event.data) { | ||
console.error("No data in execution store subscribe event"); | ||
return; | ||
} | ||
|
||
try { | ||
const { key, value, metadata } = JSON.parse(event.data); | ||
callbackRef.current(key, value, metadata); | ||
} catch (error) { | ||
console.error( | ||
"Error parsing execution store subscribe event data:", | ||
error | ||
); | ||
} | ||
}, []); | ||
|
||
/** | ||
* Handler for SSE errors. | ||
* Logs the error and marks the subscription as unhealthy. | ||
*/ | ||
const onErrorHandler = useCallback((error: Error) => { | ||
console.error("SSE connection error:", error); | ||
setIsSubscriptionHealthy(false); | ||
}, []); | ||
|
||
/** | ||
* Handler for SSE connection closure. | ||
* Marks the subscription as unhealthy. | ||
*/ | ||
const onCloseHandler = useCallback(() => { | ||
setIsSubscriptionHealthy(false); | ||
}, []); | ||
|
||
const setupSubscription = useRecoilCallback( | ||
({ snapshot }) => | ||
() => { | ||
const datasetName = datasetId | ||
? snapshot.getLoadable(fos.datasetName).getValue() | ||
: undefined; | ||
|
||
const resolvedOperatorUri = resolveOperatorURI(operatorUri); | ||
|
||
const data = { | ||
dataset_id: datasetId, | ||
operator_uri: resolvedOperatorUri, | ||
dataset_name: datasetName, | ||
}; | ||
|
||
try { | ||
getEventSource( | ||
EXECUTION_STORE_SUBSCRIBE_PATH, | ||
{ | ||
onmessage: onMessageHandler, | ||
onerror: onErrorHandler, | ||
onclose: onCloseHandler, | ||
}, | ||
abortControllerRef.current.signal, | ||
data | ||
); | ||
setIsSubscriptionHealthy(true); | ||
} catch (error) { | ||
console.error("Error subscribing to execution store:", error); | ||
} | ||
}, | ||
[operatorUri, datasetId, onMessageHandler, onErrorHandler, onCloseHandler] | ||
); | ||
|
||
const unsubscribe = useCallback(() => { | ||
abortControllerRef.current.abort(); | ||
setIsSubscriptionHealthy(false); | ||
}, []); | ||
|
||
/** | ||
* Effect to initialize the subscription when dependencies change. | ||
* It also cleans up the subscription when the component unmounts. | ||
*/ | ||
useEffect(() => { | ||
if (!abortControllerRef.current.signal.aborted) { | ||
setupSubscription(); | ||
} else { | ||
abortControllerRef.current = new AbortController(); | ||
setupSubscription(); | ||
} | ||
|
||
return () => { | ||
unsubscribe(); | ||
}; | ||
}, [setupSubscription, unsubscribe]); | ||
|
||
const resetSubscription = useCallback(() => { | ||
abortControllerRef.current.abort(); | ||
abortControllerRef.current = new AbortController(); | ||
setIsSubscriptionHealthy(false); | ||
setupSubscription(); | ||
}, [setupSubscription]); | ||
|
||
return { | ||
/** | ||
* Indicates the current health of the subscription. | ||
*/ | ||
isSubscriptionHealthy, | ||
|
||
/** | ||
* Unsubscribes from the SSE connection by aborting the current AbortController. | ||
* Also marks the subscription as unhealthy. | ||
*/ | ||
unsubscribe, | ||
|
||
/** | ||
* Resets the subscription by aborting the current connection, | ||
*/ | ||
resetSubscription, | ||
}; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.