Class: Concurrent::ScheduledTask

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

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)


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

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
end

Instance Attribute Details

#schedule_timeObject (readonly)

Returns the value of attribute schedule_time.



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

def schedule_time
  @schedule_time
end

Class Method Details

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

Since:

  • 0.5.0



33
34
35
# File 'lib/concurrent/scheduled_task.rb', line 33

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

Instance Method Details

#add_observer(*args, &block) ⇒ Object



54
55
56
57
58
# File 'lib/concurrent/scheduled_task.rb', line 54

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

#cancelObject Also known as: stop



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

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

#cancelled?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/concurrent/scheduled_task.rb', line 37

def cancelled?
  state == :cancelled
end

#executeObject

Since:

  • 0.5.0



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

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, &method(:process_task))
    self
  end
end

#in_progress?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/concurrent/scheduled_task.rb', line 41

def in_progress?
  state == :in_progress
end