class Rspawn::CLI

Public Class Methods

new(args = [], options = {}, config = {}) click to toggle source
Calls superclass method
# File lib/rspawn/cli.rb, line 17
def initialize(args = [], options = {}, config = {})
  super(args, options, config)
  @class_options = config[:shell].base.options
  @config = get_config(args.first)
end

Public Instance Methods

restart(key) click to toggle source
# File lib/rspawn/cli.rb, line 31
def restart(key)
  start_action(options['command'], 'restart')
end
restart_all() click to toggle source
# File lib/rspawn/cli.rb, line 37
def restart_all
  get_keys.each do |key|
    option, command = get_config(key).option
    RspawnWorker.spawn!(option, ['restart', command])
  end
end
rm(key) click to toggle source
# File lib/rspawn/cli.rb, line 65
def rm(key)
  @config.remove
end
start(key) click to toggle source
# File lib/rspawn/cli.rb, line 25
def start(key)
  start_action(options['command'], 'start')
end
status(key = nil) click to toggle source
# File lib/rspawn/cli.rb, line 51
def status(key = nil)
  result = {}
  get_keys(key).each do |proc_name|
    option, command = get_config(proc_name).option
    option[:status] = capture_stdout do
      RspawnWorker.spawn!(option, ["status"])
    end
    option[:command] = command
    result[proc_name] = option
  end
  puts JSON.pretty_generate(result)
end
stop(key) click to toggle source
# File lib/rspawn/cli.rb, line 45
def stop(key)
  option, command = @config.option
  RspawnWorker.spawn!(option, ["stop"])
end
tail(key) click to toggle source
# File lib/rspawn/cli.rb, line 70
def tail(key)
  option, command = @config.option(nil)
  tail_log(option[:log_file])
end
version() click to toggle source
# File lib/rspawn/cli.rb, line 76
def version
  puts VERSION
end

Private Instance Methods

capture_stdout() { || ... } click to toggle source
# File lib/rspawn/cli.rb, line 103
def capture_stdout
  out = StringIO.new
  $stdout = out
  yield
  return out.string
ensure
  $stdout = STDOUT
end
get_config(key) click to toggle source
# File lib/rspawn/cli.rb, line 99
def get_config(key)
  Config.new(@class_options['root'], key, @class_options)
end
get_keys(key = nil) click to toggle source
# File lib/rspawn/cli.rb, line 88
def get_keys(key = nil)
  keys = key.nil? ? Dir.glob(@class_options['root'] + "/*") : [key]
  keys = keys.map { |file| File.basename(file) }
end
start_action(command, action) click to toggle source
# File lib/rspawn/cli.rb, line 81
def start_action(command, action)
  option, command = @config.option(command)
  @config.save(option, command)
  RspawnWorker.spawn!(option, [action, command])
  tail_log(option[:log_file]) if @class_options['tail']
end
tail_log(log_file) click to toggle source
# File lib/rspawn/cli.rb, line 93
def tail_log(log_file)
  sleep 1
  system("tail -F #{log_file}*")        
rescue Exception => e
end