Skip to content

Commit 1ac7d36

Browse files
committed
review changes
1 parent 465bdc9 commit 1ac7d36

6 files changed

Lines changed: 47 additions & 42 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Prerequisites:
3131
* [rails_app](rails_app) - Basic Rails API application using Temporal workflows and activities.
3232
* [sorbet_generic](sorbet_generic) - Proof of concept of how to do _advanced_ Sorbet typing with the SDK.
3333
* [worker_specific_task_queues](worker_specific_task_queues) - Use a unique Task Queue for each Worker to run a sequence of Activities on the same Worker.
34+
* [polling/infrequent](polling/infrequent/) - implement an infrequent polling mechanism using Temporal's automatic Activity Retry feature
3435

3536
## Development
3637

polling/infrequent/compose_greeting_activity.rb

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@
33
require 'temporalio/activity'
44
require_relative 'test_service'
55

6-
module InfrequentPolling
7-
class ComposeGreetingActivity < Temporalio::Activity::Definition
8-
def execute(input)
9-
activity_info = Temporalio::Activity::Context.current.info
10-
TestService.get_service_result(input, activity_info)
11-
rescue TestService::TestServiceError => e
12-
raise Temporalio::Error::ApplicationError.new(
13-
e.message,
14-
category: Temporalio::Error::ApplicationError::Category::BENIGN
15-
)
6+
module Polling
7+
module Infrequent
8+
class ComposeGreetingActivity < Temporalio::Activity::Definition
9+
def execute(input)
10+
activity_info = Temporalio::Activity::Context.current.info
11+
TestService.get_service_result(input, activity_info)
12+
rescue TestService::TestServiceError => e
13+
raise Temporalio::Error::ApplicationError.new(
14+
e.message,
15+
category: Temporalio::Error::ApplicationError::Category::BENIGN
16+
)
17+
end
1618
end
1719
end
1820
end

polling/infrequent/greeting_workflow.rb

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,20 @@
44
require_relative 'compose_greeting_activity'
55
require_relative 'test_service'
66

7-
module InfrequentPolling
8-
class GreetingWorkflow < Temporalio::Workflow::Definition
9-
def execute(name)
10-
Temporalio::Workflow.execute_activity(
11-
ComposeGreetingActivity,
12-
TestService::ComposeGreetingInput.new('Hello', name),
13-
retry_policy: Temporalio::RetryPolicy.new(
14-
initial_interval: 60, # seconds
15-
backoff_coefficient: 1.0
16-
),
17-
start_to_close_timeout: 2
18-
)
7+
module Polling
8+
module Infrequent
9+
class GreetingWorkflow < Temporalio::Workflow::Definition
10+
def execute(name)
11+
Temporalio::Workflow.execute_activity(
12+
ComposeGreetingActivity,
13+
{ greeting: 'Hello', name: name },
14+
retry_policy: Temporalio::RetryPolicy.new(
15+
initial_interval: 1, # seconds
16+
backoff_coefficient: 1.0
17+
),
18+
start_to_close_timeout: 2
19+
)
20+
end
1921
end
2022
end
2123
end

polling/infrequent/starter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
# Run workflow
1010
puts 'Executing workflow'
1111
result = client.execute_workflow(
12-
InfrequentPolling::GreetingWorkflow,
12+
Polling::Infrequent::GreetingWorkflow,
1313
'World',
1414
id: "infrequent-polling-sample-workflow-id-#{Time.now.to_i}",
1515
task_queue: 'infrequent-polling-sample'

polling/infrequent/test_service.rb

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
# frozen_string_literal: true
22

3-
require 'temporalio/activity'
3+
module Polling
4+
module Infrequent
5+
# A mock external service with simulated errors.
6+
module TestService
7+
class TestServiceError < StandardError; end
48

5-
# A mock external service with simulated errors.
6-
module TestService
7-
class TestServiceError < StandardError; end
9+
@attempts = Hash.new(0)
10+
ERROR_ATTEMPTS = 5
811

9-
@attempts = Hash.new(0)
10-
ERROR_ATTEMPTS = 5
12+
def get_service_result(input, activity_info)
13+
workflow_id = activity_info.workflow_id
14+
@attempts[workflow_id] ||= 0
15+
@attempts[workflow_id] += 1
1116

12-
ComposeGreetingInput = Struct.new(:greeting, :name)
17+
puts "Attempt #{@attempts[workflow_id]} of #{ERROR_ATTEMPTS} to invoke service"
1318

14-
def get_service_result(input, activity_info)
15-
workflow_id = activity_info.workflow_id
16-
@attempts[workflow_id] ||= 0
17-
@attempts[workflow_id] += 1
19+
raise TestServiceError, 'service is down' unless @attempts[workflow_id] == ERROR_ATTEMPTS
1820

19-
puts "Attempt #{@attempts[workflow_id]} of #{ERROR_ATTEMPTS} to invoke service"
20-
21-
raise TestServiceError, 'service is down' unless @attempts[workflow_id] == ERROR_ATTEMPTS
22-
23-
"#{input['greeting']}, #{input['name']}!"
21+
"#{input['greeting']}, #{input['name']}!"
22+
end
23+
module_function :get_service_result
24+
end
2425
end
25-
module_function :get_service_result
2626
end

polling/infrequent/worker.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
worker = Temporalio::Worker.new(
1212
client:,
1313
task_queue: 'infrequent-polling-sample',
14-
workflows: [InfrequentPolling::GreetingWorkflow],
15-
activities: [InfrequentPolling::ComposeGreetingActivity]
14+
workflows: [Polling::Infrequent::GreetingWorkflow],
15+
activities: [Polling::Infrequent::ComposeGreetingActivity]
1616
)
1717

1818
puts 'Starting worker (ctrl+c to exit)'

0 commit comments

Comments
 (0)