Class: Concurrent::ScheduledTask

Inherits:
IVar
  • Object
show all
Defined in:
lib/concurrent/scheduled_task.rb

Overview

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from IVar

#fail, #set

Methods included from Observable

#count_observers, #delete_observer, #delete_observers, #with_observer

Methods included from Obligation

#completed?, #exception, #fulfilled?, #incomplete?, #no_error!, #pending?, #reason, #rejected?, #state, #unscheduled?, #value, #value!, #wait

Methods included from Dereferenceable

#value

Constructor Details

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

Returns a new instance of ScheduledTask.

Raises:

  • (ArgumentError)


12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/concurrent/scheduled_task.rb', line 12

def initialize(intended_time, opts = {}, &block)
  raise ArgumentError.new('no block given') unless block_given?
  TimerSet.calculate_schedule_time(intended_time) # raises exceptons

  super(NO_VALUE, opts)

  self.observers = CopyOnNotifyObserverSet.new
  @intended_time = intended_time
  @state         = :unscheduled
  @task          = block
  @executor      = OptionsParser::get_executor_from(opts) || Concurrent.configuration.global_operation_pool
end

Instance Attribute Details

#schedule_timeObject (readonly)

Returns the value of attribute schedule_time.



10
11
12
# File 'lib/concurrent/scheduled_task.rb', line 10

def schedule_time
  @schedule_time
end

Class Method Details

.execute(intended_time, opts = {}, &block) ⇒ Object

Since:

  • 0.5.0



35
36
37
# File 'lib/concurrent/scheduled_task.rb', line 35

def self.execute(intended_time, opts = {}, &block)
  return ScheduledTask.new(intended_time, opts, &block).execute
end

Instance Method Details

#add_observer(*args, &block) ⇒ Object



56
57
58
59
60
# File 'lib/concurrent/scheduled_task.rb', line 56

def add_observer(*args, &block)
  if_state(:unscheduled, :pending, :in_progress) do
    observers.add_observer(*args, &block)
  end
end

#cancelObject Also known as: stop



47
48
49
50
51
52
53
# File 'lib/concurrent/scheduled_task.rb', line 47

def cancel
  if_state(:unscheduled, :pending) do
    @state = :cancelled
    event.set
    true
  end
end

#cancelled?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/concurrent/scheduled_task.rb', line 39

def cancelled?
  state == :cancelled
end

#executeObject

Since:

  • 0.5.0



26
27
28
29
30
31
32
# File 'lib/concurrent/scheduled_task.rb', line 26

def execute
  if compare_and_set_state(:pending, :unscheduled)
    @schedule_time = TimerSet.calculate_schedule_time(@intended_time)
    Concurrent::timer(@schedule_time.to_f - Time.now.to_f) { @executor.post &method(:process_task) }
    self
  end
end

#in_progress?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/concurrent/scheduled_task.rb', line 43

def in_progress?
  state == :in_progress
end