-
-
Notifications
You must be signed in to change notification settings - Fork 808
Multiple streams can now be consumed at the same time #1522
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
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,5 @@ | ||
--- | ||
"@trigger.dev/core": patch | ||
--- | ||
|
||
Multiple streams can now be consumed simultaneously |
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 |
---|---|---|
|
@@ -97,7 +97,7 @@ export function runShapeStream<TRunTypes extends AnyRunTypes>( | |
|
||
// First, define interfaces for the stream handling | ||
export interface StreamSubscription { | ||
subscribe(onChunk: (chunk: unknown) => Promise<void>): Promise<() => void>; | ||
subscribe(): Promise<ReadableStream<unknown>>; | ||
} | ||
|
||
export interface StreamSubscriptionFactory { | ||
|
@@ -111,33 +111,29 @@ export class SSEStreamSubscription implements StreamSubscription { | |
private options: { headers?: Record<string, string>; signal?: AbortSignal } | ||
) {} | ||
|
||
async subscribe(onChunk: (chunk: unknown) => Promise<void>): Promise<() => void> { | ||
const response = await fetch(this.url, { | ||
async subscribe(): Promise<ReadableStream<unknown>> { | ||
return fetch(this.url, { | ||
headers: { | ||
Accept: "text/event-stream", | ||
...this.options.headers, | ||
}, | ||
signal: this.options.signal, | ||
}); | ||
|
||
if (!response.body) { | ||
throw new Error("No response body"); | ||
} | ||
|
||
const reader = response.body | ||
.pipeThrough(new TextDecoderStream()) | ||
.pipeThrough(new EventSourceParserStream()) | ||
.getReader(); | ||
|
||
while (true) { | ||
const { done, value } = await reader.read(); | ||
|
||
if (done) break; | ||
|
||
await onChunk(safeParseJSON(value.data)); | ||
} | ||
}).then((response) => { | ||
if (!response.body) { | ||
throw new Error("No response body"); | ||
} | ||
|
||
return () => reader.cancel(); | ||
return response.body | ||
.pipeThrough(new TextDecoderStream()) | ||
.pipeThrough(new EventSourceParserStream()) | ||
.pipeThrough( | ||
new TransformStream({ | ||
transform(chunk, controller) { | ||
controller.enqueue(safeParseJSON(chunk.data)); | ||
}, | ||
}) | ||
); | ||
}); | ||
} | ||
} | ||
|
||
|
@@ -254,13 +250,31 @@ export class RunSubscription<TRunTypes extends AnyRunTypes> { | |
this.options.client?.baseUrl | ||
); | ||
|
||
await subscription.subscribe(async (chunk) => { | ||
controller.enqueue({ | ||
type: streamKey, | ||
chunk: chunk as TStreams[typeof streamKey], | ||
run, | ||
} as StreamPartResult<RunShape<TRunTypes>, TStreams>); | ||
}); | ||
const stream = await subscription.subscribe(); | ||
|
||
// Create the pipeline and start it | ||
stream | ||
.pipeThrough( | ||
new TransformStream({ | ||
transform(chunk, controller) { | ||
controller.enqueue({ | ||
type: streamKey, | ||
chunk: chunk as TStreams[typeof streamKey], | ||
run, | ||
} as StreamPartResult<RunShape<TRunTypes>, TStreams>); | ||
}, | ||
}) | ||
) | ||
.pipeTo( | ||
new WritableStream({ | ||
write(chunk) { | ||
controller.enqueue(chunk); | ||
}, | ||
}) | ||
) | ||
.catch((error) => { | ||
console.error(`Error in stream ${streamKey}:`, error); | ||
}); | ||
Comment on lines
+263
to
+287
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Improve error handling in stream pipeline The current error handling only logs to console, which could lead to silent failures. Consider:
stream
.pipeThrough(
new TransformStream({
transform(chunk, controller) {
controller.enqueue({
type: streamKey,
chunk: chunk as TStreams[typeof streamKey],
run,
} as StreamPartResult<RunShape<TRunTypes>, TStreams>);
},
})
)
.pipeTo(
new WritableStream({
write(chunk) {
controller.enqueue(chunk);
},
+ abort(reason) {
+ controller.error(new Error(`Stream ${streamKey} aborted: ${reason}`));
+ }
})
)
.catch((error) => {
- console.error(`Error in stream ${streamKey}:`, error);
+ // Propagate error to consumer
+ controller.error(new Error(`Error in stream ${streamKey}: ${error.message}`));
+ // Attempt recovery by resubscribing after a delay
+ setTimeout(() => {
+ if (!activeStreams.has(streamKey)) return;
+ subscription.subscribe()
+ .then(/* ... handle resubscription ... */)
+ .catch(/* ... handle resubscription failure ... */);
+ }, 1000);
});
|
||
} | ||
} | ||
} | ||
|
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add Error Handling for Fetch Response
In the
SSEStreamSubscription
class, thesubscribe
method performs afetch
request but doesn't check if the response is successful (e.g., HTTP status 200). If the response is an error (like 4xx or 5xx), this could lead to unexpected behavior.Apply this diff to handle HTTP errors appropriately:
📝 Committable suggestion