Class: Concurrent::ScheduledTask
- Inherits:
-
IVar
- Object
- Concurrent::Synchronization::Object
- IVar
- Concurrent::ScheduledTask
- Includes:
- Comparable
- Defined in:
- lib/concurrent/scheduled_task.rb
Overview
Time calculations one all platforms and languages are sensitive to changes to the system clock. To alleviate the potential problems associated with changing the system clock while an application is running, most modern operating systems provide a monotonic clock that operates independently of the system clock. A monotonic clock cannot be used to determine human-friendly clock times. A monotonic clock is used exclusively for calculating time intervals. Not all Ruby platforms provide access to an operating system monotonic clock. On these platforms a pure-Ruby monotonic clock will be used as a fallback. An operating system monotonic clock is both faster and more reliable than the pure-Ruby implementation. The pure-Ruby implementation should be fast and reliable enough for most non-realtime operations. At this time the common Ruby platforms that provide access to an operating system monotonic clock are MRI 2.1 and above and JRuby (all versions).
ScheduledTask is a close relative of Concurrent::Future but with one
important difference: A Future is set to execute as soon as possible
whereas a ScheduledTask is set to execute after a specified delay. This
implementation is loosely based on Java's
ScheduledExecutorService.
It is a more feature-rich variant of timer.
The intended schedule time of task execution is set on object construction
with the delay argument. The delay is a numeric (floating point or integer)
representing a number of seconds in the future. Any other value or a numeric
equal to or less than zero will result in an exception. The actual schedule
time of task execution is set when the execute method is called.
The constructor can also be given zero or more processing options. Currently the only supported options are those recognized by the Dereferenceable module.
The final constructor argument is a block representing the task to be performed.
If no block is given an ArgumentError will be raised.
States
ScheduledTask mixes in the Obligation module thus giving it
"future" behavior. This includes the expected lifecycle states. ScheduledTask
has one additional state, however. While the task (block) is being executed the
state of the object will be :processing. This additional state is necessary
because it has implications for task cancellation.
Cancellation
A :pending task can be cancelled using the #cancel method. A task in any
other state, including :processing, cannot be cancelled. The #cancel
method returns a boolean indicating the success of the cancellation attempt.
A cancelled ScheduledTask cannot be restarted. It is immutable.
Obligation and Observation
The result of a ScheduledTask can be obtained either synchronously or
asynchronously. ScheduledTask mixes in both the Obligation
module and the
Observable
module from the Ruby standard library. With one exception ScheduledTask
behaves identically to Future with regard to these modules.
Copy Options
Object references in Ruby are mutable. This can lead to serious
problems when the Concern::Obligation#value of an object is a mutable reference. Which
is always the case unless the value is a Fixnum, Symbol, or similar
"primative" data type. Each instance can be configured with a few
options that can help protect the program from potentially dangerous
operations. Each of these options can be optionally set when the oject
instance is created:
:dup_on_derefWhen true the object will call the#dupmethod on thevalueobject every time the#valuemethid is called (default: false):freeze_on_derefWhen true the object will call the#freezemethod on thevalueobject every time the#valuemethod is called (default: false):copy_on_derefWhen given aProcobject theProcwill be run every time the#valuemethod is called. TheProcwill be given the currentvalueas its only argument and the result returned by the block will be the return value of the#valuecall. Whennilthis option will be ignored (default: nil)
When multiple deref options are set the order of operations is strictly defined. The order of deref operations is:
:copy_on_deref:dup_on_deref:freeze_on_deref
Because of this ordering there is no need to #freeze an object created by a
provided :copy_on_deref block. Simply set :freeze_on_deref to true.
Setting both :dup_on_deref to true and :freeze_on_deref to true is
as close to the behavior of a "pure" functional language (like Erlang, Clojure,
or Haskell) as we are likely to get in Ruby.
Class Method Summary collapse
-
.execute(delay, opts = {}, &task) ⇒ ScheduledTask
Create a new
ScheduledTaskobject with the given block, execute it, and return the:pendingobject.
Instance Method Summary collapse
-
#cancel ⇒ Boolean
Cancel this task and prevent it from executing.
-
#cancelled? ⇒ Boolean
Has the task been cancelled?.
-
#delay ⇒ Float
deprecated
Deprecated.
use #initial_delay instead
-
#execute ⇒ ScheduledTask
Execute an
:unscheduledScheduledTask. -
#in_progress? ⇒ Boolean
deprecated
Deprecated.
Use #processing? instead.
-
#initial_delay ⇒ Float
The
delayvalue given at instanciation. -
#initialize(delay, opts = {}) { ... } ⇒ ScheduledTask
constructor
Schedule a task for execution at a specified future time.
-
#processing? ⇒ Boolean
In the task execution in progress?.
-
#reschedule(delay) ⇒ Boolean
Reschedule the task using the given delay and the current time.
-
#reset ⇒ Boolean
Reschedule the task using the original delay and the current time.
-
#schedule_time ⇒ Float
The monotonic time at which the the task is scheduled to be executed.
-
#stop ⇒ Boolean
deprecated
Deprecated.
Use #cancel instead.
Methods inherited from IVar
#add_observer, #fail, #set, #try_set
Methods included from Concern::Observable
#add_observer, #count_observers, #delete_observer, #delete_observers, #with_observer
Methods included from Concern::Obligation
#complete?, #completed?, #exception, #fulfilled?, #incomplete?, #pending?, #reason, #rejected?, #state, #unscheduled?, #value, #value!, #wait, #wait!
Methods included from Concern::Dereferenceable
Methods inherited from Concurrent::Synchronization::Object
Constructor Details
#initialize(delay, opts = {}) { ... } ⇒ ScheduledTask
Scheduling is now based on a monotonic clock. This makes the timer much more accurate, but only when scheduling based on a delay interval. Scheduling a task based on a clock time is deprecated. It will still work but will not be supported in the 1.0 release.
Schedule a task for execution at a specified future time.
173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/concurrent/scheduled_task.rb', line 173 def initialize(delay, opts = {}, &task) raise ArgumentError.new('no block given') unless block_given? super(IVar::NO_VALUE, opts, &nil) synchronize do @delay = calculate_delay!(delay) # may raise exception ns_set_state(:unscheduled) @parent = opts.fetch(:timer_set, Concurrent.global_timer_set) @args = get_arguments_from(opts) @task = task @time = nil @executor = Executor.(opts) || Concurrent.global_io_executor self.observers = Collection::CopyOnNotifyObserverSet.new end end |
Class Method Details
.execute(delay, opts = {}, &task) ⇒ ScheduledTask
Scheduling is now based on a monotonic clock. This makes the timer much more accurate, but only when scheduling based on a delay interval. Scheduling a task based on a clock time is deprecated. It will still work but will not be supported in the 1.0 release.
Create a new ScheduledTask object with the given block, execute it, and return the
:pending object.
313 314 315 |
# File 'lib/concurrent/scheduled_task.rb', line 313 def self.execute(delay, opts = {}, &task) new(delay, opts, &task).execute end |
Instance Method Details
#cancel ⇒ Boolean
Cancel this task and prevent it from executing. A task can only be cancelled if it is pending or unscheduled.
247 248 249 250 251 252 253 254 255 256 |
# File 'lib/concurrent/scheduled_task.rb', line 247 def cancel if compare_and_set_state(:cancelled, :pending, :unscheduled) complete(false, nil, CancelledOperationError.new) # To avoid deadlocks this call must occur outside of #synchronize # Changing the state above should prevent redundant calls @parent.send(:remove_task, self) else false end end |
#cancelled? ⇒ Boolean
Has the task been cancelled?
222 223 224 |
# File 'lib/concurrent/scheduled_task.rb', line 222 def cancelled? synchronize { ns_check_state?(:cancelled) } end |
#delay ⇒ Float
use #initial_delay instead
The delay value given at instanciation.
200 201 202 203 |
# File 'lib/concurrent/scheduled_task.rb', line 200 def delay deprecated_method 'delay', 'initial_delay' initial_delay end |
#execute ⇒ ScheduledTask
Execute an :unscheduled ScheduledTask. Immediately sets the state to :pending
and starts counting down toward execution. Does nothing if the ScheduledTask is
in any state other than :unscheduled.
294 295 296 297 298 299 |
# File 'lib/concurrent/scheduled_task.rb', line 294 def execute if compare_and_set_state(:pending, :unscheduled) synchronize{ ns_schedule(@delay) } end self end |
#in_progress? ⇒ Boolean
Use #processing? instead.
In the task execution in progress?
238 239 240 241 |
# File 'lib/concurrent/scheduled_task.rb', line 238 def in_progress? deprecated_method 'in_progress?', 'processing?' processing? end |
#initial_delay ⇒ Float
The delay value given at instanciation.
191 192 193 |
# File 'lib/concurrent/scheduled_task.rb', line 191 def initial_delay synchronize { @delay } end |
#processing? ⇒ Boolean
In the task execution in progress?
229 230 231 |
# File 'lib/concurrent/scheduled_task.rb', line 229 def processing? synchronize { ns_check_state?(:processing) } end |
#reschedule(delay) ⇒ Boolean
Reschedule the task using the given delay and the current time.
A task can only be reset while it is :pending.
285 286 287 |
# File 'lib/concurrent/scheduled_task.rb', line 285 def reschedule(delay) synchronize{ ns_reschedule(calculate_delay!(delay)) } end |
#reset ⇒ Boolean
Reschedule the task using the original delay and the current time.
A task can only be reset while it is :pending.
273 274 275 |
# File 'lib/concurrent/scheduled_task.rb', line 273 def reset synchronize{ ns_reschedule(@delay) } end |
#schedule_time ⇒ Float
The monotonic time at which the the task is scheduled to be executed.
208 209 210 |
# File 'lib/concurrent/scheduled_task.rb', line 208 def schedule_time synchronize { @time } end |
#stop ⇒ Boolean
Use #cancel instead.
Cancel this task and prevent it from executing. A task can only be
cancelled if it is :pending or :unscheduled.
264 265 266 267 |
# File 'lib/concurrent/scheduled_task.rb', line 264 def stop deprecated_method 'stop', 'cancel' cancel end |