-
Notifications
You must be signed in to change notification settings - Fork 24
wip: first attempt to workflow client #109
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
AstraBert
wants to merge
8
commits into
main
Choose a base branch
from
clelia/workflows-client
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 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2120c0a
wip: first attempt to workflow client
AstraBert 16e01a9
Merge branch 'main' into clelia/workflows-client
AstraBert b4bc1df
chore: use openapi as a base for client
AstraBert 8bb81c8
chore: implement events streaming the good ol way
AstraBert beecc82
Revert "chore: implement events streaming the good ol way"
AstraBert bc97f89
Revert "chore: use openapi as a base for client"
AstraBert 62f136d
chore: implement suggestions from first review
AstraBert f95b057
Merge branch 'main' into clelia/workflows-client
AstraBert 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 |
---|---|---|
|
@@ -62,3 +62,5 @@ repos: | |
rev: v0.23.1 | ||
hooks: | ||
- id: toml-sort-fix | ||
|
||
exclude: ^(src/workflows/openapi_generated_client/) |
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,54 @@ | ||
import asyncio | ||
|
||
from workflows.client.client import WorkflowClient | ||
|
||
from workflows.events import StartEvent | ||
from pydantic import Field | ||
|
||
from typing import Literal | ||
|
||
|
||
class InputNumbers(StartEvent): | ||
a: int | ||
b: int | ||
operation: Literal["sum", "subtraction"] = Field(default="sum") | ||
|
||
|
||
async def main() -> None: | ||
client = WorkflowClient(protocol="http", host="localhost", port=8000) | ||
workflows = await client.list_workflows() | ||
print("===== AVAILABLE WORKFLOWS ====") | ||
print(workflows) | ||
is_healthy = await client.is_healthy() | ||
print("==== HEALTH CHECK ====") | ||
print("Healthy" if is_healthy else "Not Healty :(") | ||
ping_time = await client.ping() | ||
print("==== PING TIME ====") | ||
print(ping_time, "ms") | ||
handler = await client.run_workflow_nowait( | ||
"add_or_subtract", | ||
start_event=InputNumbers(a=1, b=3, operation="sum"), | ||
context=None, | ||
) | ||
print("==== STARTING THE WORKFLOW ===") | ||
print(f"Workflow running with handler: {handler}") | ||
print("=== STREAMING EVENTS ===") | ||
async for event in client.get_workflow_events(handler): | ||
print("Received data:", event) | ||
# Poll for result | ||
result = handler.status.value | ||
while result == "running": | ||
try: | ||
result = await client.get_workflow_result(handler) | ||
if result != "running": | ||
break | ||
await asyncio.sleep(1) | ||
except Exception as e: | ||
print(f"Error: {e}") | ||
await asyncio.sleep(1) | ||
|
||
print(f"Final result: {result}") | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |
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,61 @@ | ||
from workflows import Workflow, step, Context | ||
from workflows.events import StartEvent, StopEvent, Event | ||
from pydantic import Field | ||
from workflows.server import WorkflowServer | ||
|
||
from typing import Literal | ||
|
||
|
||
class InputNumbers(StartEvent): | ||
a: int | ||
b: int | ||
operation: Literal["sum", "subtraction"] = Field(default="sum") | ||
|
||
|
||
class CalculationEvent(Event): | ||
result: int | ||
|
||
|
||
class OutputEvent(StopEvent): | ||
message: str | ||
|
||
|
||
class AddOrSubtractWorkflow(Workflow): | ||
@step | ||
async def first_step( | ||
self, ev: InputNumbers, ctx: Context | ||
) -> CalculationEvent | None: | ||
ctx.write_event_to_stream(ev) | ||
result = ev.a + ev.b if ev.operation == "sum" else ev.a - ev.b | ||
async with ctx.store.edit_state() as state: | ||
state.operation = ev.operation | ||
state.a = ev.a | ||
state.b = ev.b | ||
state.result = result | ||
ctx.write_event_to_stream(CalculationEvent(result=result)) | ||
return CalculationEvent(result=result) | ||
|
||
@step | ||
async def second_step(self, ev: CalculationEvent, ctx: Context) -> OutputEvent: | ||
state = await ctx.store.get_state() | ||
return OutputEvent( | ||
message=f"You approved the result from your operation ({state.operation}) between {state.a} and {state.b}: {ev.result}" | ||
) | ||
|
||
|
||
async def main() -> None: | ||
server = WorkflowServer() | ||
server.add_workflow("add_or_subtract", AddOrSubtractWorkflow(timeout=1000)) | ||
server.add_workflow("add_or_subtract_2", AddOrSubtractWorkflow(timeout=1000)) | ||
try: | ||
await server.serve("localhost", 8000) | ||
except KeyboardInterrupt: | ||
return | ||
except Exception as e: | ||
raise ValueError(f"An error occurred: {e}") | ||
|
||
|
||
if __name__ == "__main__": | ||
import asyncio | ||
|
||
asyncio.run(main()) |
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,3 @@ | ||
from .client import WorkflowClient | ||
|
||
__all__ = ["WorkflowClient"] |
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.
we should bound this to
<1
. Looks like they're working on some breaking changes