Class: Pigeon::Scheduler

Inherits:
Object
  • Object
show all
Defined in:
lib/pigeon/scheduler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(queue = nil, *processors) ⇒ Scheduler

Creates a new scheduler. If queue is specified, then that queue will become the default queue. One or more processors can be supplied to work with this queue, though they should be supplied bound to the queue in order to properly receive tasks.



15
16
17
18
19
20
21
22
23
# File 'lib/pigeon/scheduler.rb', line 15

def initialize(queue = nil, *processors)
  @queues = {
    nil => queue || Pigeon::Queue.new
  }
  
  processors.flatten!
  
  @processors = processors.empty? ? [ Pigeon::Processor.new(@queues[nil]) ] : processors
end

Instance Attribute Details

#processorsObject (readonly)

Returns the value of attribute processors.



5
6
7
# File 'lib/pigeon/scheduler.rb', line 5

def processors
  @processors
end

#queuesObject (readonly)

Properties ===========================================================



4
5
6
# File 'lib/pigeon/scheduler.rb', line 4

def queues
  @queues
end

Instance Method Details

#<<(task) ⇒ Object

Add a single task to the schedule. When subclassing, override the private enqueue_task method instead.



35
36
37
# File 'lib/pigeon/scheduler.rb', line 35

def <<(task)
  enqueue_task(task)
end

#add(*tasks) ⇒ Object

Adds one or more tasks to the schedule, where the tasks can be provided individually, as a list, or as an array.



27
28
29
30
31
# File 'lib/pigeon/scheduler.rb', line 27

def add(*tasks)
  tasks.flatten.each do |task|
    enqueue_task(task)
  end
end

#default_queueObject

Returns the default queue used for scheduling.



40
41
42
# File 'lib/pigeon/scheduler.rb', line 40

def default_queue
  @queues[nil]
end

#default_queue=(queue) ⇒ Object

Used to assign the default queue.



45
46
47
# File 'lib/pigeon/scheduler.rb', line 45

def default_queue=(queue)
  @queues[nil] = queue
end

#empty?Boolean

Returns true if there are no scheduled tasks, false otherwise.

Returns:

  • (Boolean)


88
89
90
# File 'lib/pigeon/scheduler.rb', line 88

def empty?
  self.queue_size == 0
end

#pause!Object

Pauses the scheduler which will prevent additional tasks from being initiated. Any tasks in progress will continue to run. Tasks can still be added but will not be executed until the scheduler is running.



62
63
64
# File 'lib/pigeon/scheduler.rb', line 62

def pause!
  @state = :paused
end

#paused?Boolean

Returns true if the scheduler is paused, false otherwise.

Returns:

  • (Boolean)


78
79
80
# File 'lib/pigeon/scheduler.rb', line 78

def paused?
  @state == :paused
end

#processors_countObject

Returns the number of processors that are attached to this scheduler.



107
108
109
# File 'lib/pigeon/scheduler.rb', line 107

def processors_count
  @processors.length
end

#queue(queue_name) ⇒ Object

Returns the queue with the given name if one is defined, nil otherwise.



50
51
52
# File 'lib/pigeon/scheduler.rb', line 50

def queue(queue_name)
  @queues[queue_name]
end

#queue_countObject

Returns the number of queues that have been defined, including the default queue if any.



102
103
104
# File 'lib/pigeon/scheduler.rb', line 102

def queue_count
  @queues.length
end

#queue_lengthObject Also known as: queue_size

Returns the number of tasks that have been queued up.



93
94
95
96
97
# File 'lib/pigeon/scheduler.rb', line 93

def queue_length
  @queues.inject(0) do |length, (name, queue)|
    length + queue.length
  end
end

#run!Object

Sets the scheduler running.



55
56
57
# File 'lib/pigeon/scheduler.rb', line 55

def run!
  @state = :running
end

#running?Boolean

Returns true if the scheduler is running, false otherwise.

Returns:

  • (Boolean)


73
74
75
# File 'lib/pigeon/scheduler.rb', line 73

def running?
  @state == :running
end

#stop!Object

Stops the scheduler and clears out the queue. No new tasks will be accepted until the scheduler is in a paused or running state.



68
69
70
# File 'lib/pigeon/scheduler.rb', line 68

def stop!
  @state = :stopped
end

#stopped?Boolean

Returns true if the scheduler is stopped, false otherwise.

Returns:

  • (Boolean)


83
84
85
# File 'lib/pigeon/scheduler.rb', line 83

def stopped?
  @state == :stopped
end