-
Notifications
You must be signed in to change notification settings - Fork 41
Add graph-level webhooks for execution failure and completion #607
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
Brijesh-Thakkar
wants to merge
2
commits into
exospherehost:main
Choose a base branch
from
Brijesh-Thakkar:fix/graph-failed-webhook
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
2 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
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,64 +1,101 @@ | ||
| from beanie import PydanticObjectId | ||
| from app.models.executed_models import ExecutedRequestModel, ExecutedResponseModel | ||
|
|
||
| from fastapi import HTTPException, status, BackgroundTasks | ||
|
|
||
| from app.models.executed_models import ExecutedRequestModel, ExecutedResponseModel | ||
| from app.models.db.state import State | ||
| from app.models.state_status_enum import StateStatusEnum | ||
| from app.singletons.logs_manager import LogsManager | ||
| from app.tasks.create_next_states import create_next_states | ||
|
|
||
| logger = LogsManager().get_logger() | ||
|
|
||
| async def executed_state(namespace_name: str, state_id: PydanticObjectId, body: ExecutedRequestModel, x_exosphere_request_id: str, background_tasks: BackgroundTasks) -> ExecutedResponseModel: | ||
|
|
||
| async def executed_state( | ||
| namespace_name: str, | ||
| state_id: PydanticObjectId, | ||
| body: ExecutedRequestModel, | ||
| x_exosphere_request_id: str, | ||
| background_tasks: BackgroundTasks, | ||
| ) -> ExecutedResponseModel: | ||
| try: | ||
| logger.info(f"Executed state {state_id} for namespace {namespace_name}", x_exosphere_request_id=x_exosphere_request_id) | ||
| logger.info( | ||
| f"Executed state {state_id} for namespace {namespace_name}", | ||
| x_exosphere_request_id=x_exosphere_request_id, | ||
| ) | ||
|
|
||
| state = await State.find_one(State.id == state_id) | ||
| if not state or not state.id: | ||
| raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="State not found") | ||
| raise HTTPException( | ||
| status_code=status.HTTP_404_NOT_FOUND, | ||
| detail="State not found", | ||
| ) | ||
|
|
||
| if state.status != StateStatusEnum.QUEUED: | ||
| raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="State is not queued") | ||
|
|
||
| next_state_ids = [] | ||
| raise HTTPException( | ||
| status_code=status.HTTP_400_BAD_REQUEST, | ||
| detail="State is not queued", | ||
| ) | ||
|
|
||
| next_state_ids: list[PydanticObjectId] = [] | ||
|
|
||
| # ---- Handle outputs ---- | ||
| if len(body.outputs) == 0: | ||
| state.status = StateStatusEnum.EXECUTED | ||
| state.outputs = {} | ||
| await state.save() | ||
|
|
||
| next_state_ids.append(state.id) | ||
|
|
||
| else: | ||
| else: | ||
| # First output updates the current state | ||
| state.outputs = body.outputs[0] | ||
| state.status = StateStatusEnum.EXECUTED | ||
| await state.save() | ||
|
|
||
| next_state_ids.append(state.id) | ||
|
|
||
| # Remaining outputs create new states | ||
| new_states = [] | ||
| for output in body.outputs[1:]: | ||
| new_states.append(State( | ||
| node_name=state.node_name, | ||
| namespace_name=state.namespace_name, | ||
| identifier=state.identifier, | ||
| graph_name=state.graph_name, | ||
| run_id=state.run_id, | ||
| status=StateStatusEnum.EXECUTED, | ||
| inputs=state.inputs, | ||
| outputs=output, | ||
| error=None, | ||
| parents=state.parents | ||
| )) | ||
|
|
||
| if len(new_states) > 0: | ||
| inserted_ids = (await State.insert_many(new_states)).inserted_ids | ||
| new_states.append( | ||
| State( | ||
| node_name=state.node_name, | ||
| namespace_name=state.namespace_name, | ||
| identifier=state.identifier, | ||
| graph_name=state.graph_name, | ||
| run_id=state.run_id, | ||
| status=StateStatusEnum.EXECUTED, | ||
| inputs=state.inputs, | ||
| outputs=output, | ||
| error=None, | ||
| parents=state.parents, | ||
| ) | ||
| ) | ||
|
|
||
| if new_states: | ||
| inserted_ids = ( | ||
| await State.insert_many(new_states) | ||
| ).inserted_ids | ||
| next_state_ids.extend(inserted_ids) | ||
|
|
||
| background_tasks.add_task(create_next_states, next_state_ids, state.identifier, state.namespace_name, state.graph_name, state.parents) | ||
| # ---- Create next states ---- | ||
| background_tasks.add_task( | ||
| create_next_states, | ||
| next_state_ids, | ||
| state.identifier, | ||
| state.namespace_name, | ||
| state.graph_name, | ||
| state.parents, | ||
| ) | ||
Brijesh-Thakkar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return ExecutedResponseModel(status=StateStatusEnum.EXECUTED) | ||
| return ExecutedResponseModel( | ||
| status=StateStatusEnum.EXECUTED | ||
| ) | ||
Brijesh-Thakkar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| except Exception as e: | ||
| logger.error(f"Error executing state {state_id} for namespace {namespace_name}", x_exosphere_request_id=x_exosphere_request_id, error=e) | ||
| raise e | ||
| logger.error( | ||
| f"Error executing state {state_id} for namespace {namespace_name}", | ||
| x_exosphere_request_id=x_exosphere_request_id, | ||
| error=e, | ||
| ) | ||
| raise | ||
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,11 @@ | ||
| from pydantic import BaseModel, Field | ||
| from typing import List, Dict, Optional | ||
|
|
||
|
|
||
| class WebhookConfig(BaseModel): | ||
| url: str = Field(..., description="Webhook endpoint URL") | ||
| events: List[str] = Field(default_factory=list, description="Subscribed events") | ||
| headers: Optional[Dict[str, str]] = Field( | ||
| default=None, | ||
| description="Optional HTTP headers for webhook requests" | ||
| ) | ||
Brijesh-Thakkar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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,32 @@ | ||
| import logging | ||
| from datetime import datetime | ||
Brijesh-Thakkar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| from typing import Optional | ||
|
|
||
| import httpx | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
Brijesh-Thakkar marked this conversation as resolved.
Show resolved
Hide resolved
Brijesh-Thakkar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| async def dispatch_webhook( | ||
| *, | ||
| url: str, | ||
| payload: dict, | ||
| headers: Optional[dict] = None, | ||
| ) -> None: | ||
| """ | ||
| Dispatch a webhook event. | ||
| This must never raise exceptions (best-effort delivery). | ||
| """ | ||
| try: | ||
| async with httpx.AsyncClient(timeout=5) as client: | ||
| await client.post( | ||
| url, | ||
| json=payload, | ||
| headers=headers or {}, | ||
| ) | ||
Brijesh-Thakkar marked this conversation as resolved.
Show resolved
Hide resolved
Brijesh-Thakkar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| except Exception as exc: | ||
| logger.warning( | ||
| "Webhook dispatch failed", | ||
| exc_info=exc, | ||
| extra={"url": url}, | ||
| ) | ||
Brijesh-Thakkar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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.