Class: CloudCrowd::Dispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/cloud_crowd/dispatcher.rb

Overview

The dispatcher is responsible for distributing work_units to the worker nodes.

It automatically performs the distribution on a set schedule, but can also be signaled to perform distribution immediately

Instance Method Summary collapse

Constructor Details

#initialize(distribution_interval) ⇒ Dispatcher

Starts distributing jobs every “distribution_interval” seconds


11
12
13
14
15
# File 'lib/cloud_crowd/dispatcher.rb', line 11

def initialize(distribution_interval)
  @mutex  = Mutex.new
  @awaken = ConditionVariable.new
  distribute_periodically(distribution_interval)
end

Instance Method Details

#distribute!Object

Sends a signal to the distribution thread. If it's asleep, it will wake up and perform a distribution.


19
20
21
22
23
# File 'lib/cloud_crowd/dispatcher.rb', line 19

def distribute!
  @mutex.synchronize do
    @awaken.signal
  end
end

#distribute_periodically(interval) ⇒ Object (private)

Perform distribution of work units in a background thread


28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/cloud_crowd/dispatcher.rb', line 28

def distribute_periodically(interval)
  Thread.new{
    loop do
      perform_distribution
      # Sleep for "interval" seconds.
      # If awaken isn't signaled, timeout and attempt distribution
      @mutex.synchronize do
        @awaken.wait(@mutex, interval)
      end
    end
  }
end

#perform_distributionObject (private)


41
42
43
44
45
46
47
48
49
# File 'lib/cloud_crowd/dispatcher.rb', line 41

def perform_distribution
  #CloudCrowd.log "Distributing jobs to nodes"
  begin
    WorkUnit.distribute_to_nodes
  rescue StandardError => e
    CloudCrowd.log "Exception: #{e}"
    CloudCrowd.log e.backtrace
  end
end