Class: ModernTimes::Base::Supervisor

Inherits:
Object
  • Object
show all
Defined in:
lib/modern_times/base/supervisor.rb

Direct Known Subclasses

JMS::Supervisor

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(manager, worker_klass, supervisor_options, worker_options) ⇒ Supervisor

Create new supervisor to manage a number of similar workers supervisor_options are those options defined on the Worker’s Supervisor line worker_options are options passed in when creating a new instance



9
10
11
12
13
14
15
16
17
# File 'lib/modern_times/base/supervisor.rb', line 9

def initialize(manager, worker_klass, supervisor_options, worker_options)
  @stopped        = false
  @manager        = manager
  @worker_klass   = worker_klass
  @name           = worker_options[:name] || worker_klass.default_name
  @worker_options = worker_options
  @workers        = []
  @worker_mutex   = Mutex.new
end

Instance Attribute Details

#managerObject (readonly)

Returns the value of attribute manager.



4
5
6
# File 'lib/modern_times/base/supervisor.rb', line 4

def manager
  @manager
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/modern_times/base/supervisor.rb', line 4

def name
  @name
end

#worker_klassObject (readonly)

Returns the value of attribute worker_klass.



4
5
6
# File 'lib/modern_times/base/supervisor.rb', line 4

def worker_klass
  @worker_klass
end

#worker_optionsObject (readonly)

Returns the value of attribute worker_options.



4
5
6
# File 'lib/modern_times/base/supervisor.rb', line 4

def worker_options
  @worker_options
end

#workersObject (readonly)

Returns the value of attribute workers.



4
5
6
# File 'lib/modern_times/base/supervisor.rb', line 4

def workers
  @workers
end

Instance Method Details

#create_mbean(domain) ⇒ Object



84
85
86
# File 'lib/modern_times/base/supervisor.rb', line 84

def create_mbean(domain)
  SupervisorMBean.new(mbean_name(domain), mbean_description, self, {})
end

#joinObject



72
73
74
# File 'lib/modern_times/base/supervisor.rb', line 72

def join
  @workers.each { |worker| worker.join }
end

#mbean_descriptionObject



80
81
82
# File 'lib/modern_times/base/supervisor.rb', line 80

def mbean_description
  "Supervisor for #{@worker_klass.name} under #{@name}"
end

#mbean_name(domain) ⇒ Object



76
77
78
# File 'lib/modern_times/base/supervisor.rb', line 76

def mbean_name(domain)
  "#{domain}.Worker.#{@name}"
end

#stopObject



61
62
63
64
65
66
# File 'lib/modern_times/base/supervisor.rb', line 61

def stop
  @worker_mutex.synchronize do
    @stopped = true
    @workers.each { |worker| worker.stop }
  end
end

#stopped?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/modern_times/base/supervisor.rb', line 68

def stopped?
  @stopped
end

#worker_countObject



19
20
21
# File 'lib/modern_times/base/supervisor.rb', line 19

def worker_count
  @workers.size
end

#worker_count=(count) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/modern_times/base/supervisor.rb', line 23

def worker_count=(count)
  @worker_mutex.synchronize do
    ModernTimes.logger.info "#{@worker_klass.name}: Changing number of workers from #{@workers.size} to #{count}"
    raise "#{@worker_klass.name}: Can't change worker_count, this manager has been stopped" if stopped?
    curr_count = @workers.size
    if curr_count < count
      (curr_count...count).each do |index|
        worker = @worker_klass.new(@worker_options)
        worker.index = index
        if index == 0
          # HornetQ hack:  If I create the session in the jmx thread, it dies with no feedback
          #tmp_thread = Thread.new do
            worker.setup
          #end
          #tmp_thread.join
        end
        worker.thread = Thread.new do
          java.lang.Thread.current_thread.name = "ModernTimes worker: #{worker}"
          #ModernTimes.logger.debug "#{worker}: Started thread with priority #{Thread.current.priority}"
          worker.start
        end
        @workers << worker
      end
    elsif curr_count > count
      (count...curr_count).each { |index| @workers[index].stop }
      (count...curr_count).each { |index| @workers[index].thread.join }
      @workers = @workers[0, count]
    else
      return
    end
    manager.save_persist_state
  end
end

#worker_statusesObject



57
58
59
# File 'lib/modern_times/base/supervisor.rb', line 57

def worker_statuses
  @workers.map { |w| w.status }
end