Class: Bullring::DrubiedProcess

Inherits:
Object
  • Object
show all
Defined in:
lib/bullring/util/drubied_process.rb

Instance Method Summary collapse

Constructor Details

#initialize(options, &block) ⇒ DrubiedProcess

options looks like:

{

:process => {
  :host => [the hostname / IP address that the process listens on],
  :port => [the port that the process listens on],
  :command => [the command to run the process],
  :args => [the arguments to the process command]
}

}

The provided block will be called whenever this process connects (or reconnects) to a process, including when the process is restarted. This makes the block a good place to put initialization code for the process.



26
27
28
29
30
# File 'lib/bullring/util/drubied_process.rb', line 26

def initialize(options, &block)
  @options = options
  @after_connect_block = block
  connect_to_process!
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/bullring/util/drubied_process.rb', line 52

def method_missing(m, *args, &block)  
  restart_if_needed!

  @process.logger = Bullring.logger
  result = @process.send(m, *args, &block)
  @process.logger = nil

  result
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/bullring/util/drubied_process.rb', line 32

def alive?
  begin
    (@process.nil? || @process.alive?).tap{|is_alive| Bullring.logger.debug {"#{caller_name} #{is_alive ? 'is alive!' : 'is not alive!'}"}}
  rescue DRb::DRbConnError => e
    Bullring.logger.debug {"#{caller_name}: Checking if server alive and got a connection error: " + e.inspect}
    return false
  rescue StandardError => e # things like name errors, in case server doesn't have an alive? method
    Bullring.logger.debug {"#{caller_name}: Checking if server alive and got an error: " + e.inspect}
    true
  rescue
    Bullring.logger.debug {"#{caller_name}: Checking if server alive and got an unknown error"}
    true
  end
end

#connect_to_process!Object

Creates a druby connection to the process, starting it up if necessary



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/bullring/util/drubied_process.rb', line 69

def connect_to_process!
  Bullring.logger.debug{"#{caller_name}: Connecting to process..."}

  if !process_port_active?
    Bullring.logger.debug {"#{caller_name}: Spawning process..."}

    # Spawn the process in its own process group so it stays alive even if this process dies
    pid = Process.spawn([@options[:process][:command], @options[:process][:args]].flatten.join(" "), {:pgroup => true})
    Process.detach(pid)

    time_sleeping = 0
    while (!process_port_active?)
      sleep(0.2)
      if (time_sleeping += 0.2) > @options[:process][:max_bringup_time]
        Bullring.logger.error {"#{caller_name}: Timed out waiting to bring up the process"}
        raise StandardError, "#{caller_name}: Timed out waiting to bring up the process", caller
      end
    end
  end

  if !@local_service.nil?
    @local_service.stop_service
    Bullring.logger.debug {"#{caller_name}: Stopped local service on #{@local_service.uri}"}
  end
  
  @local_service = DRb.start_service "druby://127.0.0.1:0"
  Bullring.logger.debug {"#{caller_name}: Started local service on #{@local_service.uri}"}

  @process = DRbObject.new nil, "druby://#{host}:#{port}"
  
  @after_connect_block.call(@process) if !@after_connect_block.nil?
end

#process_port_active?Boolean

Returns:

  • (Boolean)


62
63
64
65
66
# File 'lib/bullring/util/drubied_process.rb', line 62

def process_port_active?
  in_use = Network::is_port_in_use?(host,port)
  Bullring.logger.debug {"#{caller_name}: Port #{port} on #{host} is #{in_use ? 'active' : 'inactive'}."}
  in_use
end

#restart_if_needed!Object



47
48
49
50
# File 'lib/bullring/util/drubied_process.rb', line 47

def restart_if_needed!
  Bullring.logger.debug {"#{caller_name}: In restart_if_needed!"}
  alive? || connect_to_process!
end