Class: Concurrent::TimerTask

Inherits:
Object
  • Object
show all
Includes:
Dereferenceable, Runnable, Observable
Defined in:
lib/concurrent/timer_task.rb

Constant Summary collapse

EXECUTION_INTERVAL =
60
TIMEOUT_INTERVAL =
30

Constants included from Runnable

Runnable::LifecycleError

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Runnable

included, #run, #run!, #running?, #stop

Methods included from Dereferenceable

#set_deref_options, #value

Constructor Details

#initialize(opts = {}, &block) ⇒ TimerTask

Returns a new instance of TimerTask.

Raises:

  • (ArgumentError)


21
22
23
24
25
26
27
28
29
30
# File 'lib/concurrent/timer_task.rb', line 21

def initialize(opts = {}, &block)
  raise ArgumentError.new('no block given') unless block_given?

  @execution_interval = opts[:execution] || opts[:execution_interval] || EXECUTION_INTERVAL
  @timeout_interval = opts[:timeout] || opts[:timeout_interval] || TIMEOUT_INTERVAL
  @run_now = opts[:now] || opts[:run_now] || false

  @task = block
  set_deref_options(opts)
end

Instance Attribute Details

#execution_intervalObject

Returns the value of attribute execution_interval.



18
19
20
# File 'lib/concurrent/timer_task.rb', line 18

def execution_interval
  @execution_interval
end

#timeout_intervalObject

Returns the value of attribute timeout_interval.



19
20
21
# File 'lib/concurrent/timer_task.rb', line 19

def timeout_interval
  @timeout_interval
end

Instance Method Details

#killObject Also known as: terminate



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/concurrent/timer_task.rb', line 32

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