Skip to content

Commit

Permalink
Refactor: use method activity
Browse files Browse the repository at this point in the history
  • Loading branch information
pfranck committed Jul 8, 2024
1 parent af2c5c2 commit b6ee329
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 15 deletions.
15 changes: 8 additions & 7 deletions polling/infrequent/activities.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ class ComposeGreetingInput:
greeting: str
name: str

class ComposeGreeting:
def __init__(self, test_service: TestService = None):
self.test_service = test_service or TestService()

@activity.defn
async def compose_greeting(input: ComposeGreetingInput) -> str:
attempt = activity.info().attempt - 1
test_service = TestService(attempt=attempt)
# If this raises an exception because it's not done yet, the activity will
# continually be scheduled for retry
return await test_service.get_service_result(input)
@activity.defn
async def compose_greeting(self, input: ComposeGreetingInput) -> str:
# If this raises an exception because it's not done yet, the activity will
# continually be scheduled for retry
return await self.test_service.get_service_result(input)
6 changes: 3 additions & 3 deletions polling/infrequent/run_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
from temporalio.client import Client
from temporalio.worker import Worker

from polling.infrequent.activities import compose_greeting
from polling.infrequent.activities import ComposeGreeting
from polling.infrequent.workflows import GreetingWorkflow


async def main():
client = await Client.connect("localhost:7233")

activities = ComposeGreeting()
worker = Worker(
client,
task_queue="infrequent-activity-retry-task-queue",
workflows=[GreetingWorkflow],
activities=[compose_greeting],
activities=[activities.compose_greeting],
)
await worker.run()

Expand Down
6 changes: 3 additions & 3 deletions polling/infrequent/workflows.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
from temporalio.common import RetryPolicy

with workflow.unsafe.imports_passed_through():
from polling.infrequent.activities import ComposeGreetingInput, compose_greeting
from polling.infrequent.activities import ComposeGreetingInput, ComposeGreeting


@workflow.defn
class GreetingWorkflow:
@workflow.run
async def run(self, name: str) -> str:
return await workflow.execute_activity(
compose_greeting,
return await workflow.execute_activity_method(
ComposeGreeting.compose_greeting,
ComposeGreetingInput("Hello", name),
start_to_close_timeout=timedelta(seconds=2),
retry_policy=RetryPolicy(
Expand Down
4 changes: 2 additions & 2 deletions polling/test_service.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class TestService:
def __init__(self, attempt=0, error_attempts=5):
self.try_attempts = attempt
def __init__(self, error_attempts=5):
self.try_attempts = 0
self.error_attempts = error_attempts

async def get_service_result(self, input):
Expand Down

0 comments on commit b6ee329

Please sign in to comment.