Skip to content

Commit ee9b7ed

Browse files
committed
Use deprecation helpers in ScheduledTask
1 parent 21173aa commit ee9b7ed

File tree

1 file changed

+41
-39
lines changed

1 file changed

+41
-39
lines changed

lib/concurrent/scheduled_task.rb

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,52 +5,54 @@
55
require 'concurrent/executor/executor'
66
require 'concurrent/executor/timer_set'
77
require 'concurrent/utility/monotonic_time'
8+
require 'concurrent/concern/deprecation'
89

910
module Concurrent
11+
include Concern::Deprecation
1012

1113
# `ScheduledTask` is a close relative of `Concurrent::Future` but with one
1214
# important difference: A `Future` is set to execute as soon as possible
1315
# whereas a `ScheduledTask` is set to execute after a specified delay. This
1416
# implementation is loosely based on Java's
15-
# [ScheduledExecutorService](http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ScheduledExecutorService.html).
17+
# [ScheduledExecutorService](http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ScheduledExecutorService.html).
1618
# It is a more feature-rich variant of {Concurrent.timer}.
17-
#
19+
#
1820
# The *intended* schedule time of task execution is set on object construction
1921
# with the `delay` argument. The delay is a numeric (floating point or integer)
2022
# representing a number of seconds in the future. Any other value or a numeric
2123
# equal to or less than zero will result in an exception. The *actual* schedule
2224
# time of task execution is set when the `execute` method is called.
23-
#
25+
#
2426
# The constructor can also be given zero or more processing options. Currently
2527
# the only supported options are those recognized by the
26-
# [Dereferenceable](Dereferenceable) module.
27-
#
28+
# [Dereferenceable](Dereferenceable) module.
29+
#
2830
# The final constructor argument is a block representing the task to be performed.
2931
# If no block is given an `ArgumentError` will be raised.
30-
#
32+
#
3133
# **States**
32-
#
34+
#
3335
# `ScheduledTask` mixes in the [Obligation](Obligation) module thus giving it
3436
# "future" behavior. This includes the expected lifecycle states. `ScheduledTask`
3537
# has one additional state, however. While the task (block) is being executed the
3638
# state of the object will be `:processing`. This additional state is necessary
37-
# because it has implications for task cancellation.
38-
#
39+
# because it has implications for task cancellation.
40+
#
3941
# **Cancellation**
40-
#
42+
#
4143
# A `:pending` task can be cancelled using the `#cancel` method. A task in any
4244
# other state, including `:processing`, cannot be cancelled. The `#cancel`
4345
# method returns a boolean indicating the success of the cancellation attempt.
44-
# A cancelled `ScheduledTask` cannot be restarted. It is immutable.
45-
#
46+
# A cancelled `ScheduledTask` cannot be restarted. It is immutable.
47+
#
4648
# **Obligation and Observation**
47-
#
49+
#
4850
# The result of a `ScheduledTask` can be obtained either synchronously or
4951
# asynchronously. `ScheduledTask` mixes in both the [Obligation](Obligation)
5052
# module and the
5153
# [Observable](http://ruby-doc.org/stdlib-2.0/libdoc/observer/rdoc/Observable.html)
5254
# module from the Ruby standard library. With one exception `ScheduledTask`
53-
# behaves identically to [Future](Observable) with regard to these modules.
55+
# behaves identically to [Future](Observable) with regard to these modules.
5456
#
5557
# @!macro copy_options
5658
#
@@ -59,83 +61,83 @@ module Concurrent
5961
# require 'concurrent'
6062
# require 'thread' # for Queue
6163
# require 'open-uri' # for open(uri)
62-
#
64+
#
6365
# class Ticker
6466
# def get_year_end_closing(symbol, year)
6567
# uri = "http://ichart.finance.yahoo.com/table.csv?s=#{symbol}&a=11&b=01&c=#{year}&d=11&e=31&f=#{year}&g=m"
6668
# data = open(uri) {|f| f.collect{|line| line.strip } }
6769
# data[1].split(',')[4].to_f
6870
# end
6971
# end
70-
#
72+
#
7173
# # Future
7274
# price = Concurrent::Future.execute{ Ticker.new.get_year_end_closing('TWTR', 2013) }
7375
# price.state #=> :pending
7476
# sleep(1) # do other stuff
7577
# price.value #=> 63.65
7678
# price.state #=> :fulfilled
77-
#
79+
#
7880
# # ScheduledTask
7981
# task = Concurrent::ScheduledTask.execute(2){ Ticker.new.get_year_end_closing('INTC', 2013) }
8082
# task.state #=> :pending
8183
# sleep(3) # do other stuff
8284
# task.value #=> 25.96
83-
#
85+
#
8486
# @example Successful task execution
85-
#
87+
#
8688
# task = Concurrent::ScheduledTask.new(2){ 'What does the fox say?' }
8789
# task.state #=> :unscheduled
8890
# task.execute
8991
# task.state #=> pending
90-
#
92+
#
9193
# # wait for it...
9294
# sleep(3)
93-
#
95+
#
9496
# task.unscheduled? #=> false
9597
# task.pending? #=> false
9698
# task.fulfilled? #=> true
9799
# task.rejected? #=> false
98100
# task.value #=> 'What does the fox say?'
99-
#
101+
#
100102
# @example One line creation and execution
101-
#
103+
#
102104
# task = Concurrent::ScheduledTask.new(2){ 'What does the fox say?' }.execute
103105
# task.state #=> pending
104-
#
106+
#
105107
# task = Concurrent::ScheduledTask.execute(2){ 'What do you get when you multiply 6 by 9?' }
106108
# task.state #=> pending
107-
#
109+
#
108110
# @example Failed task execution
109-
#
111+
#
110112
# task = Concurrent::ScheduledTask.execute(2){ raise StandardError.new('Call me maybe?') }
111113
# task.pending? #=> true
112-
#
114+
#
113115
# # wait for it...
114116
# sleep(3)
115-
#
117+
#
116118
# task.unscheduled? #=> false
117119
# task.pending? #=> false
118120
# task.fulfilled? #=> false
119121
# task.rejected? #=> true
120122
# task.value #=> nil
121-
# task.reason #=> #<StandardError: Call me maybe?>
122-
#
123+
# task.reason #=> #<StandardError: Call me maybe?>
124+
#
123125
# @example Task execution with observation
124-
#
126+
#
125127
# observer = Class.new{
126128
# def update(time, value, reason)
127129
# puts "The task completed at #{time} with value '#{value}'"
128130
# end
129131
# }.new
130-
#
132+
#
131133
# task = Concurrent::ScheduledTask.new(2){ 'What does the fox say?' }
132134
# task.add_observer(observer)
133135
# task.execute
134136
# task.pending? #=> true
135-
#
137+
#
136138
# # wait for it...
137139
# sleep(3)
138-
#
140+
#
139141
# #>> The task completed at 2013-11-07 12:26:09 -0500 with value 'What does the fox say?'
140142
#
141143
# @!macro monotonic_clock_warning
@@ -196,12 +198,12 @@ def initial_delay
196198
#
197199
# @deprecated use {#initial_delay} instead
198200
def delay
199-
warn '[DEPRECATED] use #initial_delay instead'
201+
deprecated_method 'delay', 'initial_delay'
200202
initial_delay
201203
end
202204

203205
# The monotonic time at which the the task is scheduled to be executed.
204-
#
206+
#
205207
# @return [Float] the schedule time or nil if `unscheduled`
206208
def schedule_time
207209
synchronize { @time }
@@ -234,7 +236,7 @@ def processing?
234236
#
235237
# @deprecated Use {#processing?} instead.
236238
def in_progress?
237-
warn '[DEPRECATED] use #processing? instead'
239+
deprecated_method 'in_progress?', 'processing?'
238240
processing?
239241
end
240242

@@ -260,7 +262,7 @@ def cancel
260262
#
261263
# @deprecated Use {#cancel} instead.
262264
def stop
263-
warn '[DEPRECATED] use #cancel instead'
265+
deprecated_method 'stop', 'cancel'
264266
cancel
265267
end
266268

@@ -363,7 +365,7 @@ def ns_reschedule(delay)
363365
# @!visibility private
364366
def calculate_delay!(delay)
365367
if delay.is_a?(Time)
366-
warn '[DEPRECATED] Use an interval not a clock time; schedule is now based on a monotonic clock'
368+
deprecated 'Use an interval not a clock time; schedule is now based on a monotonic clock'
367369
now = Time.now
368370
raise ArgumentError.new('schedule time must be in the future') if delay <= now
369371
delay.to_f - now.to_f

0 commit comments

Comments
 (0)