Class: SmartfoxJruby::SfsRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/smartfox_jruby/sfs_runner.rb

Instance Method Summary collapse

Constructor Details

#initialize(home_dir, opts = {}) ⇒ SfsRunner

Returns a new instance of SfsRunner.



9
10
11
12
13
14
15
# File 'lib/smartfox_jruby/sfs_runner.rb', line 9

def initialize(home_dir, opts = {})
  @home_dir = home_dir
  @opts = opts
  @launched = false
  @fault = false
  @pid = nil
end

Instance Method Details

#fault?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/smartfox_jruby/sfs_runner.rb', line 68

def fault?
  @fault
end

#kill!Object



39
40
41
42
43
44
45
46
47
# File 'lib/smartfox_jruby/sfs_runner.rb', line 39

def kill!
  info "Checking running processes: #{pids.join(",")}"
  pids.each { |pid|
    pid = pid.try(:strip).try(:to_i)
    info "Killing the process with pid=#{pid}..."
    Process.kill("KILL", pid) if Process.alive?(pid)
    wait_with_timeout(5) { !Process.alive?(pid) } rescue ""
  }
end

#kill_and_wait!(opts = {}) ⇒ Object



55
56
57
58
# File 'lib/smartfox_jruby/sfs_runner.rb', line 55

def kill_and_wait!(opts = {})
  kill!
  wait_until_terminated(opts[:timeout])
end

#launched?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/smartfox_jruby/sfs_runner.rb', line 64

def launched?
  @launched
end

#run!Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/smartfox_jruby/sfs_runner.rb', line 17

def run!
  Thread.new do
    begin
      info "Changing dir to #{work_dir}"
      Dir.chdir(work_dir) do
        info "Running cmd #{cmd}"
        IO.popen(cmd) do |output|
          @pid = output.pid
          info "Running child with pid=#{output.pid}..."
          output.each do |line|
            debug(line.gsub(/\n/, ""))
            @launched = true if line =~ /SmartFoxServer 2X \(.+\) READY!/
          end
        end
        @fault = true
      end
    rescue Exception => e
      error "#{e}"
    end
  end
end

#run_and_wait!(opts = {}) ⇒ Object



49
50
51
52
# File 'lib/smartfox_jruby/sfs_runner.rb', line 49

def run_and_wait!(opts = {})
  run!
  wait_until_launched_or_fault(opts[:timeout])
end

#running?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/smartfox_jruby/sfs_runner.rb', line 60

def running?
  !pids.blank? && pids.map { |pid| Process.alive?(pid.to_i) }.include?(true)
end

#wait_until_launched_or_fault(timeout = nil) ⇒ Object



76
77
78
# File 'lib/smartfox_jruby/sfs_runner.rb', line 76

def wait_until_launched_or_fault(timeout = nil)
  wait_with_timeout(timeout) { launched? or fault? }
end

#wait_until_terminated(timeout = nil) ⇒ Object



72
73
74
# File 'lib/smartfox_jruby/sfs_runner.rb', line 72

def wait_until_terminated(timeout = nil)
  wait_with_timeout(timeout) { !running? }
end