Class: Legion::Supervisor

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, processes: 1, port: 42042) ⇒ Supervisor

Returns a new instance of Supervisor.



13
14
15
16
17
18
19
# File 'lib/legion/supervisor.rb', line 13

def initialize(klass, processes: 1, port: 42042)
  @klass = klass
  @processes = processes
  @port = port
  @local_instances = []
  @remote_instances = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



48
49
50
# File 'lib/legion/supervisor.rb', line 48

def method_missing(name, *args)
  get_remote_instance.send("#{name}_async", *args)
end

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



5
6
7
# File 'lib/legion/supervisor.rb', line 5

def klass
  @klass
end

#local_instancesObject (readonly)

Returns the value of attribute local_instances.



5
6
7
# File 'lib/legion/supervisor.rb', line 5

def local_instances
  @local_instances
end

#portObject (readonly)

Returns the value of attribute port.



5
6
7
# File 'lib/legion/supervisor.rb', line 5

def port
  @port
end

#processesObject (readonly)

Returns the value of attribute processes.



5
6
7
# File 'lib/legion/supervisor.rb', line 5

def processes
  @processes
end

#remote_instancesObject (readonly)

Returns the value of attribute remote_instances.



5
6
7
# File 'lib/legion/supervisor.rb', line 5

def remote_instances
  @remote_instances
end

Instance Method Details

#get_remote_instanceObject



39
40
41
42
43
44
45
46
# File 'lib/legion/supervisor.rb', line 39

def get_remote_instance
  @index ||= 0
  remote_instance = remote_instances[@index]
  sleep 0.01 while remote_instance.busy?
  @index += 1
  @index = 0 if @index >= remote_instances.length
  remote_instance
end

#respond_to?(name) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
# File 'lib/legion/supervisor.rb', line 52

def respond_to?(name)
  return true if super
  klass.instance_methods.include? name.to_sym
end

#startObject



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/legion/supervisor.rb', line 21

def start
  DRb.start_service
  (1..processes).each do |i|
    local_instance = klass.new
    local_instance.start_remote_instance(port: port)
    @port += 1
    local_instances << local_instance
    remote_instance = DRbObject.new_with_uri(local_instance.uri)
    verify_remote_instance(remote_instance)
    remote_instances << remote_instance
  end
end

#stopObject



34
35
36
37
# File 'lib/legion/supervisor.rb', line 34

def stop
  remote_instances.each { |inst| inst.exit }
  DRb.stop_service
end