Skip to content

Commit ac73803

Browse files
committed
Refactor: use method activity
1 parent af2c5c2 commit ac73803

File tree

4 files changed

+18
-16
lines changed

4 files changed

+18
-16
lines changed

Diff for: polling/infrequent/activities.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ class ComposeGreetingInput:
1111
name: str
1212

1313

14-
@activity.defn
15-
async def compose_greeting(input: ComposeGreetingInput) -> str:
16-
attempt = activity.info().attempt - 1
17-
test_service = TestService(attempt=attempt)
18-
# If this raises an exception because it's not done yet, the activity will
19-
# continually be scheduled for retry
20-
return await test_service.get_service_result(input)
14+
class ComposeGreeting:
15+
def __init__(self):
16+
self.test_service = TestService()
17+
18+
@activity.defn
19+
async def compose_greeting(self, input: ComposeGreetingInput) -> str:
20+
# If this raises an exception because it's not done yet, the activity will
21+
# continually be scheduled for retry
22+
return await self.test_service.get_service_result(input)

Diff for: polling/infrequent/run_worker.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@
33
from temporalio.client import Client
44
from temporalio.worker import Worker
55

6-
from polling.infrequent.activities import compose_greeting
6+
from polling.infrequent.activities import ComposeGreeting
77
from polling.infrequent.workflows import GreetingWorkflow
88

99

1010
async def main():
1111
client = await Client.connect("localhost:7233")
12-
12+
activities = ComposeGreeting()
1313
worker = Worker(
1414
client,
1515
task_queue="infrequent-activity-retry-task-queue",
1616
workflows=[GreetingWorkflow],
17-
activities=[compose_greeting],
17+
activities=[activities.compose_greeting],
1818
)
1919
await worker.run()
2020

Diff for: polling/infrequent/workflows.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
from temporalio.common import RetryPolicy
55

66
with workflow.unsafe.imports_passed_through():
7-
from polling.infrequent.activities import ComposeGreetingInput, compose_greeting
7+
from polling.infrequent.activities import ComposeGreeting, ComposeGreetingInput
88

99

1010
@workflow.defn
1111
class GreetingWorkflow:
1212
@workflow.run
1313
async def run(self, name: str) -> str:
14-
return await workflow.execute_activity(
15-
compose_greeting,
14+
return await workflow.execute_activity_method(
15+
ComposeGreeting.compose_greeting,
1616
ComposeGreetingInput("Hello", name),
1717
start_to_close_timeout=timedelta(seconds=2),
1818
retry_policy=RetryPolicy(

Diff for: polling/test_service.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class TestService:
2-
def __init__(self, attempt=0, error_attempts=5):
3-
self.try_attempts = attempt
4-
self.error_attempts = error_attempts
2+
def __init__(self):
3+
self.try_attempts = 0
4+
self.error_attempts = 5
55

66
async def get_service_result(self, input):
77
print(

0 commit comments

Comments
 (0)