class Kajiki::Runner

Public Class Methods

new(opts) click to toggle source

@param opts [Hash] the command-line options.

# File lib/kajiki/runner.rb, line 19
def initialize(opts)
  @opts = opts
end
run(opts, &block) click to toggle source

A wrapper to execute all commands. @param opts [Hash] the command-line options. @param block [Block] the command (String) will be passed.

# File lib/kajiki/runner.rb, line 12
def self.run(opts, &block)
  opts[:auto_default_actions] = true
  runner = new(opts)
  runner.execute_command(&block)
end

Public Instance Methods

execute_command(cmd = ARGV, &block) click to toggle source

Execute the command with the given block; usually called by ::run.

# File lib/kajiki/runner.rb, line 24
def execute_command(cmd = ARGV, &block)
  cmd = validate_command(cmd)
  validate_options
  send(cmd, &block)
end
start(&block) click to toggle source

Start the process with the given block. @param [Block]

# File lib/kajiki/runner.rb, line 53
def start(&block)
  fail 'No start block given.' if block.nil?
  check_existing_pid
  puts "Starting process..."
  Process.daemon if @opts[:daemonize]
  change_privileges if @opts[:auto_default_actions]
  redirect_outputs if @opts[:auto_default_actions]
  write_pid
  trap_default_signals if @opts[:auto_default_actions]
  block.call('start')
end
stop(&block) click to toggle source

Stop the process. @param [Block] will execute prior to shutdown, if given.

# File lib/kajiki/runner.rb, line 67
def stop(&block)
  block.call('stop') unless block.nil?
  pid = read_pid
  fail 'No valid PID file.' unless pid && pid > 0
  Process.kill('TERM', pid)
  delete_pid
  puts 'Process terminated.'
end
validate_command(cmd = ARGV) click to toggle source

Validate the given command. @param cmd [Array] only one command should be given. @return [String] extracts out of Array

# File lib/kajiki/runner.rb, line 33
def validate_command(cmd = ARGV)
  fail 'Specify one action.' unless cmd.count == 1
  cmd = cmd.shift
  fail 'Invalid action.' unless SUB_COMMANDS.include?(cmd)
  cmd
end
validate_options() click to toggle source

Validate the options; otherwise fails.

# File lib/kajiki/runner.rb, line 41
def validate_options
  if @opts[:daemonize]
    fail 'Must specify PID file.' unless @opts[:pid_given]
  end
  @opts[:pid] = File.expand_path(@opts[:pid]) if @opts[:pid_given]
  @opts[:log] = File.expand_path(@opts[:log]) if @opts[:log_given]
  @opts[:error] = File.expand_path(@opts[:error]) if @opts[:error_given]
  @opts[:config] = File.expand_path(@opts[:config]) if @opts[:config_given]
end