Skip to content

Commit 2eea7ba

Browse files
pr feedback
1 parent d944c45 commit 2eea7ba

6 files changed

Lines changed: 25 additions & 10 deletions

File tree

test/updatable_timer/updatable_timer_workflow_test.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,32 @@ def test_workflow
1717
task_queue: "tq-#{SecureRandom.uuid}",
1818
workflows: [UpdatableTimerWorkflow]
1919
)
20-
worker.run do
20+
handle = worker.run do
2121
day_from_now = (Time.now(in: 'utc') + (24 * 60 * 60)).to_r
2222
hour_from_now = (Time.now(in: 'utc') + (60 * 60)).to_r
2323
handle = env.client.start_workflow(
2424
UpdatableTimerWorkflow, day_from_now,
2525
id: "wf-#{SecureRandom.uuid}", task_queue: worker.task_queue
2626
)
2727
assert_equal day_from_now, Rational(handle.query(UpdatableTimerWorkflow.wake_up_time))
28+
env.sleep(10)
2829
handle.signal(UpdatableTimerWorkflow.update_wake_up_time, hour_from_now)
2930
assert_equal hour_from_now, Rational(handle.query(UpdatableTimerWorkflow.wake_up_time))
3031

3132
handle.result
33+
handle
3234
end
35+
timer_events = handle.fetch_history_events.filter_map do |e|
36+
timer_id = (e.timer_started_event_attributes ||
37+
e.timer_canceled_event_attributes ||
38+
e.timer_fired_event_attributes)&.timer_id
39+
[e.event_type, timer_id] if timer_id
40+
end.compact
41+
assert_equal(
42+
[[:EVENT_TYPE_TIMER_STARTED, '1'], [:EVENT_TYPE_TIMER_CANCELED, '1'], [:EVENT_TYPE_TIMER_STARTED, '2'],
43+
[:EVENT_TYPE_TIMER_FIRED, '2']],
44+
timer_events
45+
)
3346
end
3447
end
3548
end

updatable_timer/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Then in another terminal, use the Ruby client to the workflow from this director
1616

1717
The Ruby code will invoke the workflow which will create a timer that will resolve in a day.
1818

19-
Finally run the updater to change the timer to 10 seconds from now:
19+
Finally in a third terminal, run the updater to change the timer to 10 seconds from now:
2020

2121
```bash
2222
bundle exec ruby wake_up_timer_updater.rb

updatable_timer/starter.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
client = Temporalio::Client.connect('localhost:7233', 'default', logger:)
99

1010
# Run workflow
11-
logger.info('Starting timer')
11+
logger.info('Starting workflow')
1212
client.execute_workflow(
1313
UpdatableTimer::UpdatableTimerWorkflow, (Time.now(in: 'utc') + (24 * 60 * 60)).to_r,
14-
id: 'updatable-timer-sample-workflow-id', task_queue: 'updatable-timer'
14+
id: 'updatable-timer-sample-workflow-id', task_queue: 'updatable-timer-sample'
1515
)
16-
logger.info('Timer complete')
16+
logger.info('Workflow complete')

updatable_timer/updatable_timer.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
# frozen_string_literal: true
22

33
require 'temporalio/workflow'
4-
require 'temporalio/error'
54

65
module UpdatableTimer
76
class UpdatableTimer
87
def initialize(wake_up_time)
98
@wake_up_time = wake_up_time
10-
@wake_up_time_updated = false
119
end
1210

1311
attr_reader :wake_up_time
@@ -34,7 +32,7 @@ def sleep
3432
Temporalio::Workflow.wait_condition { @wake_up_time_updated }
3533
end
3634
rescue Timeout::Error
37-
next
35+
break
3836
end
3937
end
4038
Temporalio::Workflow.logger.info('sleep_until completed')

updatable_timer/updatable_timer_workflow.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@
55

66
module UpdatableTimer
77
class UpdatableTimerWorkflow < Temporalio::Workflow::Definition
8-
def execute(wake_up_time)
8+
workflow_init
9+
def initialize(wake_up_time)
910
@timer = UpdatableTimer.new(Time.at(Rational(wake_up_time)))
11+
end
12+
13+
def execute(_wake_up_time)
1014
@timer.sleep
1115
end
1216

updatable_timer/worker.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# Create worker with the activities and workflow
1616
worker = Temporalio::Worker.new(
1717
client:,
18-
task_queue: 'updatable-timer',
18+
task_queue: 'updatable-timer-sample',
1919
workflows: [UpdatableTimer::UpdatableTimerWorkflow]
2020
)
2121

0 commit comments

Comments
 (0)