Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 07bdc7c

Browse files
committed
update-with-start examples
1 parent 5f51915 commit 07bdc7c

File tree

4 files changed

+168
-0
lines changed

4 files changed

+168
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
TASK_QUEUE = "uws"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import asyncio
2+
3+
from temporalio.client import Client
4+
5+
from message_passing.update_with_start import TASK_QUEUE
6+
from message_passing.update_with_start.workflows import (
7+
LockService,
8+
TransactionRequest,
9+
TransactionWorkflow,
10+
)
11+
12+
13+
async def financial_transaction_with_early_return():
14+
client = await Client.connect("localhost:7233")
15+
# The user wants to kick off a long-running transaction workflow and get an early-return result.
16+
17+
# No network call here
18+
transaction = client.start_workflow(
19+
TransactionWorkflow.run,
20+
args=[TransactionRequest(amount=77.7)],
21+
id="transaction-abc123",
22+
task_queue=TASK_QUEUE,
23+
lazy=True,
24+
)
25+
26+
# Send the MultiOp gRPC
27+
confirmation_token = await transaction.execute_update(
28+
TransactionWorkflow.get_confirmation
29+
)
30+
final_report = await transaction.result()
31+
32+
print(f"got confirmation token: {confirmation_token}")
33+
print(f"got final report: {final_report}")
34+
35+
36+
async def use_a_lock_service():
37+
client = await Client.connect("localhost:7233")
38+
39+
# The user wants to acquire a lock lease from a lock service
40+
41+
lock_service = client.start_workflow(
42+
LockService.run,
43+
id="lock-service-id",
44+
task_queue="uws",
45+
lazy=True,
46+
)
47+
48+
lock = await lock_service.execute_update(LockService.acquire_lock)
49+
print(f"acquired lock: {lock}")
50+
51+
52+
async def main():
53+
await financial_transaction_with_early_return()
54+
await use_a_lock_service()
55+
56+
57+
if __name__ == "__main__":
58+
asyncio.run(main())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import asyncio
2+
import logging
3+
4+
from temporalio.client import Client
5+
from temporalio.worker import Worker
6+
7+
from message_passing.update_with_start import TASK_QUEUE, workflows
8+
9+
interrupt_event = asyncio.Event()
10+
11+
12+
async def main():
13+
logging.basicConfig(level=logging.INFO)
14+
15+
client = await Client.connect("localhost:7233")
16+
17+
async with Worker(
18+
client,
19+
task_queue=TASK_QUEUE,
20+
workflows=[workflows.LockService, workflows.TransactionWorkflow],
21+
):
22+
logging.info("Worker started, ctrl+c to exit")
23+
await interrupt_event.wait()
24+
logging.info("Shutting down")
25+
26+
27+
if __name__ == "__main__":
28+
loop = asyncio.new_event_loop()
29+
try:
30+
loop.run_until_complete(main())
31+
except KeyboardInterrupt:
32+
interrupt_event.set()
33+
loop.run_until_complete(loop.shutdown_asyncgens())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
from asyncio import Future
2+
from dataclasses import dataclass
3+
from typing import Literal
4+
from uuid import uuid4
5+
6+
from temporalio import workflow
7+
8+
#
9+
# LockService (lazy init)
10+
# A workflow acts as an always-available service and supports an `acquire_lock` update.
11+
#
12+
13+
14+
@dataclass
15+
class Lock:
16+
token: str
17+
18+
19+
@workflow.defn
20+
class LockService:
21+
@workflow.run
22+
async def run(self) -> None:
23+
await Future()
24+
25+
@workflow.update
26+
async def acquire_lock(self) -> Lock:
27+
# TODO: implement lock service sample
28+
token = str(uuid4())
29+
return Lock(token)
30+
31+
32+
#
33+
# FinancialTransaction (early return)
34+
# The workflow is a multi-stage financial transaction and supports a low-latency `get_confirmation` update.
35+
#
36+
37+
38+
@dataclass
39+
class TransactionRequest:
40+
amount: float
41+
42+
43+
@dataclass
44+
class TransactionReport:
45+
id: str
46+
final_amount: float
47+
status: Literal["complete", "failed"]
48+
49+
50+
@dataclass
51+
class TransactionConfirmation:
52+
id: str
53+
status: Literal["confirmed"]
54+
55+
56+
@workflow.defn
57+
class TransactionWorkflow:
58+
def __init__(self):
59+
self.ready_to_issue_final_report = False
60+
61+
@workflow.run
62+
async def run(self, request: TransactionRequest) -> TransactionReport:
63+
await workflow.wait_condition(lambda: self.ready_to_issue_final_report)
64+
return TransactionReport(
65+
id=workflow.info().workflow_id,
66+
status="complete",
67+
final_amount=request.amount * 0.97,
68+
)
69+
70+
@workflow.update
71+
async def get_confirmation(self) -> TransactionConfirmation:
72+
self.ready_to_issue_final_report = True
73+
return TransactionConfirmation(
74+
id=workflow.info().workflow_id,
75+
status="confirmed",
76+
)

0 commit comments

Comments
 (0)