Skip to content

Commit d9b1ba3

Browse files
committed
Timers now pass options to the task blocks (untested).
1 parent 8bcc8ee commit d9b1ba3

File tree

4 files changed

+11
-8
lines changed

4 files changed

+11
-8
lines changed

lib/concurrent/executor/timer_set.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,17 @@ def initialize(opts = {})
4242
#
4343
# @raise [ArgumentError] if the intended execution time is not in the future
4444
# @raise [ArgumentError] if no block is given
45-
def post(intended_time, &task)
45+
def post(intended_time, *args, &task)
4646
time = TimerSet.calculate_schedule_time(intended_time).to_f
4747
raise ArgumentError.new('no block given') unless block_given?
4848

4949
mutex.synchronize do
5050
return false unless running?
5151

5252
if (time - Time.now.to_f) <= 0.01
53-
@executor.post(&task)
53+
@executor.post(*args, &task)
5454
else
55-
@queue.push(Task.new(time, task))
55+
@queue.push(Task.new(time, args, task))
5656
check_processing_thread!
5757
end
5858

@@ -94,7 +94,7 @@ def self.calculate_schedule_time(intended_time, now = Time.now)
9494
# times.
9595
#
9696
# @!visibility private
97-
Task = Struct.new(:time, :op) do
97+
Task = Struct.new(:time, :args, :op) do
9898
include Comparable
9999

100100
def <=>(other)
@@ -137,7 +137,6 @@ def check_processing_thread!
137137
# @!visibility private
138138
def process_tasks
139139
loop do
140-
141140
mutex.synchronize do
142141
if @queue.empty?
143142
@thread = nil
@@ -148,7 +147,7 @@ def process_tasks
148147
interval = task.time - Time.now.to_f
149148

150149
if interval <= 0
151-
@executor.post(&task.op)
150+
@executor.post(*task.args, &task.op)
152151
@queue.pop
153152
else
154153
@condition.wait(mutex, [interval, 60].min)

lib/concurrent/utility/timer.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ module Concurrent
1010
# @yield the task to execute
1111
#
1212
# @return [Boolean] true
13-
def timer(seconds, &block)
13+
def timer(seconds, *args, &block)
1414
raise ArgumentError.new('no block given') unless block_given?
1515
raise ArgumentError.new('interval must be greater than or equal to zero') if seconds < 0
1616

17-
Concurrent.configuration.global_timer_set.post(seconds, &block)
17+
Concurrent.configuration.global_timer_set.post(seconds, *args, &block)
1818
true
1919
end
2020
module_function :timer

spec/concurrent/executor/timer_set_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ module Concurrent
3535
latch.wait(0.2).should be_true
3636
end
3737

38+
it 'passes all areguments to the task on execution'
39+
3840
it 'immediately posts a task when the delay is zero' do
3941
Thread.should_not_receive(:new).with(any_args)
4042
subject.post(0){ true }

spec/concurrent/utility/timer_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ module Concurrent
3333
}.to_not raise_error
3434
end
3535

36+
it 'passes all arguments to the block'
37+
3638
it 'runs the task on the global timer pool' do
3739
Concurrent.configuration.global_timer_set.should_receive(:post).with(0.1)
3840
Concurrent::timer(0.1){ :foo }

0 commit comments

Comments
 (0)