Class: Concurrent::TimerTask
- Inherits:
-
Object
- Object
- Concurrent::TimerTask
- Includes:
- Dereferenceable, Runnable, Stoppable, Observable
- Defined in:
- lib/concurrent/timer_task.rb
Overview
A very common currency pattern is to run a thread that performs a task at regular intervals. The thread that peforms the task sleeps for the given interval then wakes up and performs the task. Lather, rinse, repeat… This pattern causes two problems. First, it is difficult to test the business logic of the task becuse the task itself is tightly coupled with the concurrency logic. Second, an exception in raised while performing the task can cause the entire thread to abend. In a long-running application where the task thread is intended to run for days/weeks/years a crashed task thread can pose a significant problem. TimerTask alleviates both problems.
When a TimerTask is launched it starts a thread for monitoring the execution interval. The TimerTask thread does not perform the task, however. Instead, the TimerTask launches the task on a separate thread. Should the task experience an unrecoverable crash only the task thread will crash. This makes the TimerTask very fault tolerant Additionally, the TimerTask thread can respond to the success or failure of the task, performing logging or ancillary operations. TimerTask can also be configured with a timeout value allowing it to kill a task that runs too long.
One other advantage of TimerTask is it forces the bsiness logic to be completely decoupled from the concurrency logic. The business logic can be tested separately then passed to the TimerTask for scheduling and running.
In some cases it may be necessary for a TimerTask to affect its own execution cycle. To facilitate this a reference to the task object is passed into the block as a block argument every time the task is executed.
The TimerTask class includes the Dereferenceable mixin module so the result of the last execution is always available via the #value method. Derefencing options can be passed to the TimerTask during construction or at any later time using the #set_deref_options method.
TimerTask supports notification through the Ruby standard library Observable module. On execution the TimerTask will notify the observers with threes arguments: time of execution, the result of the block (or nil on failure), and any raised exceptions (or nil on success). If the timeout interval is exceeded the observer will receive a Concurrent::TimeoutError object as the third argument.
Constant Summary collapse
- EXECUTION_INTERVAL =
Default
:execution_interval 60- TIMEOUT_INTERVAL =
Default
:timeout_interval 30
Constants included from Runnable
Instance Attribute Summary collapse
-
#execution_interval ⇒ Object
Number of seconds after the task completes before the task is performed again.
-
#timeout_interval ⇒ Object
Number of seconds the task can run before it is considered to have failed.
Instance Method Summary collapse
-
#initialize(opts = {}) {|task| ... } ⇒ TimerTask
constructor
Create a new TimerTask with the given task and configuration.
-
#kill ⇒ Boolean
(also: #terminate)
Terminate with extreme prejudice.
Methods included from Stoppable
Methods included from Runnable
included, #run, #run!, #running?, #stop
Methods included from Dereferenceable
#init_mutex, #mutex, #set_deref_options, #value
Constructor Details
#initialize(opts = {}) {|task| ... } ⇒ TimerTask
Calls Concurrent::Dereferenceable#set_deref_options passing opts. All options supported by Concurrent::Dereferenceable can be set during object initialization.
Create a new TimerTask with the given task and configuration.
191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/concurrent/timer_task.rb', line 191 def initialize(opts = {}, &block) raise ArgumentError.new('no block given') unless block_given? self.execution_interval = opts[:execution] || opts[:execution_interval] || EXECUTION_INTERVAL self.timeout_interval = opts[:timeout] || opts[:timeout_interval] || TIMEOUT_INTERVAL @run_now = opts[:now] || opts[:run_now] || false @task = block init_mutex (opts) end |
Instance Attribute Details
#execution_interval ⇒ Object
Number of seconds after the task completes before the task is performed again.
160 161 162 |
# File 'lib/concurrent/timer_task.rb', line 160 def execution_interval @execution_interval end |
#timeout_interval ⇒ Object
Number of seconds the task can run before it is considered to have failed. Failed tasks are forcibly killed.
164 165 166 |
# File 'lib/concurrent/timer_task.rb', line 164 def timeout_interval @timeout_interval end |
Instance Method Details
#kill ⇒ Boolean Also known as: terminate
Do not use this method unless #stop has failed.
Terminate with extreme prejudice. Useful in cases where #stop doesn’t work because one of the threads becomes unresponsive.
235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/concurrent/timer_task.rb', line 235 def kill return true unless running? mutex.synchronize do @running = false Thread.kill(@worker) unless @worker.nil? Thread.kill(@monitor) unless @monitor.nil? end return true rescue return false ensure @worker = @monitor = nil end |