Skip to content
This repository was archived by the owner on Feb 20, 2025. It is now read-only.

Commit 842283d

Browse files
committed
feat: add example
1 parent 4008257 commit 842283d

File tree

5 files changed

+65
-7
lines changed

5 files changed

+65
-7
lines changed

examples/v2/trigger.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from examples.v2.workflows import ExampleWorkflowInput, example_workflow
2+
3+
4+
def main() -> None:
5+
example_workflow.run(
6+
input=ExampleWorkflowInput(message="Hello, world!"),
7+
)
8+
9+
10+
if __name__ == "__main__":
11+
main()

examples/v2/worker.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from examples.v2.workflows import example_workflow, hatchet
2+
from hatchet_sdk import Context
3+
from hatchet_sdk.v2 import BaseWorkflowImpl
4+
5+
6+
class ExampleV2Workflow(BaseWorkflowImpl):
7+
config = example_workflow.config
8+
9+
@hatchet.step(timeout="11s", retries=3)
10+
def step1(self, context: Context) -> None:
11+
input = example_workflow.workflow_input(context)
12+
13+
print(input.message)
14+
15+
return None
16+
17+
18+
def main() -> None:
19+
worker = hatchet.worker("test-worker", max_runs=1)
20+
worker.register_workflow(ExampleV2Workflow())
21+
worker.start()
22+
23+
24+
if __name__ == "__main__":
25+
main()

examples/v2/workflows.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from dotenv import load_dotenv
2+
from pydantic import BaseModel
3+
4+
from hatchet_sdk.v2 import Hatchet
5+
6+
load_dotenv()
7+
8+
hatchet = Hatchet(debug=True)
9+
10+
11+
class ExampleWorkflowInput(BaseModel):
12+
message: str
13+
14+
15+
example_workflow = hatchet.declare_workflow(
16+
name="example-workflow",
17+
on_events=["example-event"],
18+
timeout="10m",
19+
input_validator=ExampleWorkflowInput,
20+
)

hatchet_sdk/clients/admin.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ def _prepare_workflow_request(
9191
payload_data = json.dumps(input)
9292
_options = options.model_dump()
9393

94+
_options.pop("namespace")
95+
9496
try:
9597
_options["additional_metadata"] = json.dumps(
9698
options.additional_metadata

hatchet_sdk/v2/workflows.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@ def run(self, input: TWorkflowInput | None = None) -> Any:
178178
workflow_name=self.config.name, input=input.model_dump() if input else {}
179179
)
180180

181+
def workflow_input(self, ctx: Context) -> TWorkflowInput:
182+
return cast(TWorkflowInput, ctx.workflow_input())
183+
181184

182185
class BaseWorkflowImpl:
183186
"""
@@ -186,9 +189,10 @@ class BaseWorkflowImpl:
186189
Configuration is passed to the workflow implementation via the `config` attribute.
187190
"""
188191

189-
declaration: WorkflowDeclaration = WorkflowDeclaration(
190-
config=WorkflowConfig(), hatchet=None
191-
)
192+
config: WorkflowConfig = WorkflowConfig()
193+
194+
def __init__(self) -> None:
195+
self.config.name = self.config.name or str(self.__class__.__name__)
192196

193197
def get_service_name(self, namespace: str) -> str:
194198
return f"{namespace}{self.config.name.lower()}"
@@ -219,10 +223,6 @@ def steps(self) -> list[Step[Any]]:
219223
def create_action_name(self, namespace: str, step: Step[Any]) -> str:
220224
return self.get_service_name(namespace) + ":" + step.name
221225

222-
def __init__(self) -> None:
223-
self.config = self.declaration.config
224-
self.config.name = self.config.name or str(self.__class__.__name__)
225-
226226
def get_name(self, namespace: str) -> str:
227227
return namespace + self.config.name
228228

0 commit comments

Comments
 (0)