Skip to content

Commit 3822299

Browse files
authored
Merge pull request #18 from northpowered/5-http-workers
5 http workers
2 parents 3736510 + 3ad056b commit 3822299

20 files changed

+864
-210
lines changed

example_app.py

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,62 @@
1-
# Import `BoostApp` class
2-
from temporal_boost import BoostApp, BoostLoggerConfig, BoostHTTPRoute
1+
"""
2+
For development purposes
3+
"""
34

5+
# Import `BoostApp` class
6+
from temporal_boost import BoostApp, BoostLoggerConfig
47
from temporalio import activity
58
from temporalio import workflow
9+
from datetime import timedelta
10+
from dataclasses import dataclass
611

7-
8-
from aiohttp import web
12+
from example_asgi_app import fastapi_app
913

1014
# Create `BoostApp` object
11-
app: BoostApp = BoostApp(
12-
logger_config=BoostLoggerConfig(json=False),
13-
)
15+
app: BoostApp = BoostApp(logger_config=BoostLoggerConfig(json=False), use_pydantic=True)
16+
17+
18+
@dataclass
19+
class TestModel:
20+
foo: str
21+
bar: int
1422

1523

1624
# Describe your activities/workflows
1725
@activity.defn(name="test_boost_activity_1")
18-
async def test_boost_activity_1(foo: str, bar: str) -> str:
19-
app.logger.info("This is built-in logger")
20-
return f"1_{foo}{bar}"
26+
async def test_boost_activity_1(payload: TestModel) -> TestModel:
27+
payload.foo = f"{payload.foo}+activity1"
28+
payload.bar = payload.bar + 1
29+
return payload
2130

2231

2332
@activity.defn(name="test_boost_activity_2")
24-
async def test_boost_activity_2(foo: str, bar: str) -> str:
25-
return f"2_{foo}{bar}"
33+
async def test_boost_activity_2(payload: TestModel) -> TestModel:
34+
payload.foo = f"{payload.foo}+activity2"
35+
payload.bar = payload.bar + 1
36+
return payload
2637

2738

28-
@workflow.defn(name="TestCronWorkflow", sandboxed=False)
29-
class TestCronWorkflow:
39+
@workflow.defn(sandboxed=False)
40+
class MyWorkflow:
3041
@workflow.run
31-
async def run(self) -> None:
32-
print("With is cron workflow")
33-
return None
42+
async def run(self, foo: str):
43+
start_payload: TestModel = TestModel(foo="hello", bar=0)
44+
print(type(start_payload))
45+
result_1 = await workflow.execute_activity(
46+
test_boost_activity_1,
47+
start_payload,
48+
task_queue="task_q_1",
49+
start_to_close_timeout=timedelta(minutes=1),
50+
)
51+
print(type(result_1))
52+
result_2 = await workflow.execute_activity(
53+
test_boost_activity_2,
54+
result_1,
55+
task_queue="task_q_2",
56+
start_to_close_timeout=timedelta(minutes=1),
57+
)
58+
print(type(result_2))
59+
return result_2
3460

3561

3662
# Add async workers to your app
@@ -42,28 +68,12 @@ async def run(self) -> None:
4268
metrics_endpoint="0.0.0.0:9000",
4369
)
4470
app.add_worker("worker_2", "task_q_2", activities=[test_boost_activity_2])
45-
# Example of CRON worker
46-
app.add_worker(
47-
"test_cron",
48-
"task_q_3",
49-
workflows=[TestCronWorkflow],
50-
cron_schedule="* * * * *",
51-
cron_runner=TestCronWorkflow.run,
52-
)
5371

72+
app.add_worker("worker_3", "task_q_3", workflows=[MyWorkflow])
5473

55-
async def aaa(request):
56-
import json
57-
q: dict = {"foo": "bar"}
58-
return web.Response(text=json.dumps(q), content_type="application/json")
74+
# app.add_http_worker("test_http_worker_!", "0.0.0.0", 8000, routes=[])
5975

60-
61-
app.add_http_worker(
62-
"test_http_1", host="127.0.0.1", port=8899,
63-
routes=[
64-
BoostHTTPRoute("/", aaa)
65-
]
66-
)
76+
app.add_asgi_worker("asgi_worker", fastapi_app, "0.0.0.0", 8000)
6777

6878
# Run your app and start workers with CLI
6979
app.run()

example_asgi_app.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from fastapi import FastAPI, Response
2+
3+
4+
async def foo():
5+
return Response(status_code=200)
6+
7+
8+
fastapi_app: FastAPI = FastAPI(docs_url="/doc")
9+
10+
fastapi_app.add_api_route("/foo", foo)

example_starter.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
For development purposes
3+
"""
4+
5+
import asyncio
6+
from temporalio.client import Client
7+
8+
9+
async def main():
10+
11+
client = await Client.connect("localhost:7233")
12+
13+
# Run workflow
14+
result = await client.execute_workflow(
15+
"MyWorkflow",
16+
"blabla",
17+
id="pydantic_converter-workflow-id",
18+
task_queue="task_q_3",
19+
)
20+
print(result)
21+
22+
23+
if __name__ == "__main__":
24+
asyncio.run(main())

gen_doc.py

Lines changed: 0 additions & 52 deletions
This file was deleted.

0 commit comments

Comments
 (0)