Skip to content

Commit d793b15

Browse files
authored
Merge branch 'main' into feat/sentry-v2-example
2 parents 46cd182 + 0763309 commit d793b15

File tree

9 files changed

+64
-54
lines changed

9 files changed

+64
-54
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
os: [ubuntu-latest, macos-intel, macos-arm, windows-latest]
1717
include:
1818
- os: macos-intel
19-
runsOn: macos-12
19+
runsOn: macos-13
2020
- os: macos-arm
2121
runsOn: macos-14
2222
# macOS ARM 3.8 does not have an available Python build at

polling/frequent/activities.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,19 @@
11
import asyncio
2-
import time
3-
from dataclasses import dataclass
42

53
from temporalio import activity
64

7-
from polling.test_service import TestService
8-
9-
10-
@dataclass
11-
class ComposeGreetingInput:
12-
greeting: str
13-
name: str
5+
from polling.test_service import ComposeGreetingInput, get_service_result
146

157

168
@activity.defn
179
async def compose_greeting(input: ComposeGreetingInput) -> str:
18-
test_service = TestService()
1910
while True:
2011
try:
2112
try:
22-
result = await test_service.get_service_result(input)
13+
result = await get_service_result(input)
2314
activity.logger.info(f"Exiting activity ${result}")
2415
return result
25-
except Exception as e:
16+
except Exception:
2617
# swallow exception since service is down
2718
activity.logger.debug("Failed, trying again shortly", exc_info=True)
2819

polling/infrequent/activities.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
1-
from dataclasses import dataclass
2-
31
from temporalio import activity
42

5-
from polling.test_service import TestService
6-
7-
8-
@dataclass
9-
class ComposeGreetingInput:
10-
greeting: str
11-
name: str
3+
from polling.test_service import ComposeGreetingInput, get_service_result
124

135

146
@activity.defn
157
async def compose_greeting(input: ComposeGreetingInput) -> str:
16-
test_service = TestService()
178
# If this raises an exception because it's not done yet, the activity will
189
# continually be scheduled for retry
19-
return await test_service.get_service_result(input)
10+
return await get_service_result(input)
Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
1-
from dataclasses import dataclass
1+
from typing import Any, NoReturn
22

33
from temporalio import activity
44

55

6-
@dataclass
7-
class ComposeGreetingInput:
8-
greeting: str
9-
name: str
10-
11-
126
@activity.defn
13-
async def compose_greeting(input: ComposeGreetingInput) -> str:
7+
async def compose_greeting(input: Any) -> NoReturn:
148
raise RuntimeError("Service is down")

polling/periodic_sequence/workflows.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@
66
from temporalio.exceptions import ActivityError
77

88
with workflow.unsafe.imports_passed_through():
9-
from polling.periodic_sequence.activities import (
10-
ComposeGreetingInput,
11-
compose_greeting,
12-
)
9+
from polling.periodic_sequence.activities import compose_greeting
10+
from polling.test_service import ComposeGreetingInput
1311

1412

1513
@workflow.defn

polling/test_service.py

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
1-
class TestService:
2-
def __init__(self):
3-
self.try_attempts = 0
4-
self.error_attempts = 5
5-
6-
async def get_service_result(self, input):
7-
print(
8-
f"Attempt {self.try_attempts}"
9-
f" of {self.error_attempts} to invoke service"
10-
)
11-
self.try_attempts += 1
12-
if self.try_attempts % self.error_attempts == 0:
13-
return f"{input.greeting}, {input.name}!"
14-
raise Exception("service is down")
1+
from dataclasses import dataclass
2+
from typing import Counter
3+
4+
from temporalio import activity
5+
6+
attempts = Counter[str]()
7+
ERROR_ATTEMPTS = 5
8+
9+
10+
@dataclass
11+
class ComposeGreetingInput:
12+
greeting: str
13+
name: str
14+
15+
16+
async def get_service_result(input):
17+
workflow_id = activity.info().workflow_id
18+
attempts[workflow_id] += 1
19+
20+
print(f"Attempt {attempts[workflow_id]} of {ERROR_ATTEMPTS} to invoke service")
21+
if attempts[workflow_id] == ERROR_ATTEMPTS:
22+
return f"{input.greeting}, {input.name}!"
23+
raise Exception("service is down")

tests/hello/hello_update_test.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99

1010

1111
async def test_update_workflow(client: Client, env: WorkflowEnvironment):
12-
if env.supports_time_skipping:
13-
pytest.skip(
14-
"Time-skipping test server currently has issue with update: https://github.com/temporalio/sdk-java/issues/1903"
15-
)
1612
task_queue_name = str(uuid.uuid4())
1713
async with Worker(client, task_queue=task_queue_name, workflows=[GreetingWorkflow]):
1814
handle = await client.start_workflow(

tests/polling/infrequent/__init__.py

Whitespace-only changes.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import uuid
2+
3+
import pytest
4+
from temporalio.client import Client
5+
from temporalio.testing import WorkflowEnvironment
6+
from temporalio.worker import Worker
7+
8+
from polling.infrequent.activities import compose_greeting
9+
from polling.infrequent.workflows import GreetingWorkflow
10+
11+
12+
async def test_infrequent_polling_workflow(client: Client, env: WorkflowEnvironment):
13+
if not env.supports_time_skipping:
14+
pytest.skip("Too slow to test with time-skipping disabled")
15+
16+
# Start a worker that hosts the workflow and activity implementations.
17+
task_queue = f"tq-{uuid.uuid4()}"
18+
async with Worker(
19+
client,
20+
task_queue=task_queue,
21+
workflows=[GreetingWorkflow],
22+
activities=[compose_greeting],
23+
):
24+
handle = await client.start_workflow(
25+
GreetingWorkflow.run,
26+
"Temporal",
27+
id=f"infrequent-polling-{uuid.uuid4()}",
28+
task_queue=task_queue,
29+
)
30+
result = await handle.result()
31+
assert result == "Hello, Temporal!"

0 commit comments

Comments
 (0)