-
Notifications
You must be signed in to change notification settings - Fork 7
feat(stream): stream to zenodo-rdm deposit form #13
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
mairasalazar
wants to merge
7
commits into
inveniosoftware:main
Choose a base branch
from
mairasalazar:allow-reading-file-from-path
base: main
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
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4e1af90
feat(stream): add SSE support
mairasalazar 9399e23
fix: accept both url and uri file paths
mairasalazar 9ad4213
refactor: manage errors in open file
mairasalazar 6e9a647
feature(stream): improve streaming of results
mairasalazar 21d3e79
refactor: change README header for clarity
mairasalazar 2a74978
fix: better error handling
mairasalazar e970fce
refactor: use match/case
mairasalazar 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
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
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,9 @@ | ||
| """Exception classes.""" | ||
|
|
||
|
|
||
| class WorkflowEventError(Exception): | ||
| """Exception raised when generating SSE events is failed.""" | ||
|
|
||
| def __init__(self, error_code: str): | ||
| """Initialize exception.""" | ||
| self.error_code = error_code |
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 |
|---|---|---|
| @@ -1,18 +1,21 @@ | ||
| """Workflow API routes with tenant-scoped access control.""" | ||
|
|
||
| import asyncio | ||
| import json | ||
|
|
||
| from fastapi import APIRouter, Depends, HTTPException, Request | ||
| from fastapi.responses import StreamingResponse | ||
| from pydantic import BaseModel | ||
| from sqlalchemy.exc import SQLAlchemyError | ||
| from sqlalchemy.orm.exc import NoResultFound | ||
| from sqlmodel import Session, select | ||
| from sse_starlette import EventSourceResponse, ServerSentEvent | ||
| from temporalio.client import Client | ||
|
|
||
| from app.auth import AuthContext, decode_access_token | ||
| from app.database.models import Workflow, WorkflowStatus | ||
| from app.database.session import get_db_session | ||
| from app.dependencies import get_current_user | ||
| from app.errors import WorkflowEventError | ||
| from app.workflows.extract_metadata_workflow import ( | ||
| ExtractMetadata, | ||
| ExtractMetadataWorkflowRequest, | ||
|
|
@@ -158,19 +161,42 @@ async def workflow_event(request: Request, workflow_id: str): | |
| if await request.is_disconnected(): | ||
| break | ||
|
|
||
| with Session(request.app.state.db_engine) as session: | ||
| try: | ||
| workflow = session.exec( | ||
| select(Workflow).where(Workflow.public_id == workflow_id) | ||
| ).one() | ||
| if workflow.status.name == "ERROR" or workflow.status.name == "SUCCESS": | ||
| yield workflow.status.name | ||
| try: | ||
| with Session(request.app.state.db_engine) as session: | ||
| try: | ||
| workflow = session.exec( | ||
| select(Workflow).where(Workflow.public_id == workflow_id) | ||
| ).one() | ||
| except NoResultFound: | ||
| raise WorkflowEventError(error_code="WORKFLOW_NOT_FOUND") | ||
|
Collaborator
Author
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. todo: still need to handle error codes on the ui side |
||
| except SQLAlchemyError as e: | ||
| print("Error in fetching from database (stream_workflow)", e) | ||
| raise WorkflowEventError(error_code="DB_FETCH_FAILED") | ||
|
|
||
| status = workflow.status | ||
|
|
||
| match status: | ||
| case WorkflowStatus.SUCCESS: | ||
| yield ServerSentEvent( | ||
| data=json.dumps(workflow.result), event="metadata" | ||
| ) | ||
| yield ServerSentEvent(data="done", event="end") | ||
| break | ||
| case WorkflowStatus.ERROR: | ||
| yield ServerSentEvent( | ||
| data=json.dumps({"error_code": "WORKFLOW_FAILED"}), | ||
| event="error", | ||
| ) | ||
| yield ServerSentEvent(data="done", event="end") | ||
| break | ||
|
|
||
|
mairasalazar marked this conversation as resolved.
|
||
| yield workflow.status.name | ||
| except SQLAlchemyError as e: | ||
| print("Error(stream)", e) | ||
| raise HTTPException(status_code=500) | ||
| except WorkflowEventError as e: | ||
| yield ServerSentEvent( | ||
| data=json.dumps({"error_code": e.error_code}), | ||
| event="error", | ||
| ) | ||
| yield ServerSentEvent(data="done", event="end") | ||
| break | ||
|
|
||
| await asyncio.sleep(STREAM_DELAY) | ||
|
|
||
|
|
@@ -211,6 +237,6 @@ async def stream( | |
|
|
||
| verify_tenant_owns_workflow(auth, workflow) | ||
|
|
||
| return StreamingResponse( | ||
| return EventSourceResponse( | ||
| workflow_event(request, workflow_id), media_type="text/event-stream" | ||
| ) | ||
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
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.
minor: I see from the FastAPI SSE docs, you can import these from
fastapi.sse. Does this maybe imply also thatsse-starletteis already bundled with FastAPI (or comes via an extra?)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.
fastapi.sse was introduced in version 0.135.0 and Orcha is on version 0.129.0, which I guess we could bump without issues :) From what I've seen, the implementation doesn't use sse-starlette for the SSE support
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.
@yashlamba fyi, maybe we can look into using a newer version of fastapi