class Cassie::Support::ServerProcess

Attributes

errors[R]

@return [Array<String>] The Cassandra output lines tagged with ERROR

pid[R]

Public Class Methods

all() click to toggle source

Scan the system for cassandra processes running @return [Array<ServerProcess>] Running cassandra processes @raise [RuntimeError] if scanning with ps system calls fails.

# File lib/cassie/support/server_process.rb, line 12
def self.all
  pids.map{|pid| new(pid)}
end
log_path() click to toggle source

The path to the active cassandra binary's log file Does not yet respect a configured log path, and assumes the path is relative to bin/casandra at ../logs/system.log @!parse attr_reader :log_path

# File lib/cassie/support/server_process.rb, line 20
def self.log_path
  which = Cassie::Support::SystemCommand.new("which", ["cassandra"])
  which.succeed

  bin_path = which.output.tr("\n", '')
  bin_path.sub('bin/cassandra', 'logs/system.log')
end
new(pid=nil) click to toggle source

Starts a cassandra server process. {#running?} will be true if it started correctly.

# File lib/cassie/support/server_process.rb, line 29
def initialize(pid=nil)
  @pid = pid
  @errors = []

  if pid
    @running = true
  else
    start_cassandra
  end
end

Protected Class Methods

pids() click to toggle source
# File lib/cassie/support/server_process.rb, line 67
def self.pids
  ps = Cassie::Support::SystemCommand.new("ps", ["-awx"])
  ps.succeed
  cassandra_awx = ps.output.split("\n").grep(/cassandra/)
  cassandra_awx.map{ |p| p.split(' ').first.to_i }
end

Public Instance Methods

command() click to toggle source
# File lib/cassie/support/server_process.rb, line 61
def command
  details[:command]
end
running?() click to toggle source

@return [Boolean] If the cassandra server started correctly. See {#errors} if false.

# File lib/cassie/support/server_process.rb, line 41
def running?
  !!@running
end
stop() click to toggle source

Stops the cassandra server processes, synchronously. @raise [RuntimeError] if the process could not be killed.

# File lib/cassie/support/server_process.rb, line 47
def stop
  self.class.pids.each do|pid|
    Process.kill("TERM", pid)
    loop do
      sleep(0.1)
      begin
        Process.getpgid( pid )
      rescue Errno::ESRCH
        break
      end
    end
  end
end

Protected Instance Methods

details() click to toggle source
# File lib/cassie/support/server_process.rb, line 86
def details
  @details ||= fetch_details
end
fetch_details() click to toggle source
# File lib/cassie/support/server_process.rb, line 90
def fetch_details
  # http://linuxcommand.org/man_pages/ps1.html (key long descriptions)
  # virtual memory size of the process in KiB
  # ps -p 62027 -o pid,user,ltime,vsize,pcpu,args -ww
  # PID USER     TIME      VSZ  %CPU ARGS
  # 62027 eprothro   44:24  6201356   1.0 /Library/Java/Java
  ps = Cassie::Support::SystemCommand.new("ps", ["-p", pid, "-o", "pid,user,etime,vsize,pcpu,args", "-ww"])
  ps.succeed
  puts ps.output
  elements = ps.output.split("\n")[1]
  elements = elements.split(" ")
  raise "Error fetching details, pid fetched doesn't match pid queried" unless elements.shift.to_i == self.pid

  {}.tap do |h|
    h[:user] = elements.shift
    offset = Time.now.getlocal.to_s.split(' ').last
    h[:started_at] = DateTime.parse("#{elements.shift} #{offset}")
    h[:memory] = elements.shift * 1024
    h[:cpu] = elements.shift / 100.0
  end
  #{p.split(' ').first.ljust(5,' ')} | #{p.split(' ').last}"
end
start_cassandra() click to toggle source
# File lib/cassie/support/server_process.rb, line 74
def start_cassandra
  start_pids = self.class.pids
  cassandra = Cassie::Support::SystemCommand.new("cassandra")
  cassandra.run
  new_pids = self.class.pids - start_pids
  Cassie.logger.warn "[WARN] - Multiple cassandra processes started, using first one." if new_pids.length > 1

  @running = !!(cassandra.output =~ /state jump to NORMAL/)
  @errors = cassandra.output.split("\n").grep(/ERROR/)
  @pid = new_pids.first
end