Class: Kajiki::Runner

Inherits:
Object
  • Object
show all
Includes:
Handler
Defined in:
lib/kajiki/runner.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Handler

#change_privileges, #check_existing_pid, #delete_pid, #pid_exists?, #pid_file_exists?, #read_pid, #redirect_outputs, #trap_default_signals, #write_pid

Constructor Details

#initialize(opts) ⇒ Runner

Returns a new instance of Runner.

Parameters:

  • opts (Hash)

    the command-line options.



19
20
21
# File 'lib/kajiki/runner.rb', line 19

def initialize(opts)
  @opts = opts
end

Class Method Details

.run(opts, &block) ⇒ Object

A wrapper to execute all commands.

Parameters:

  • opts (Hash)

    the command-line options.

  • block (Block)

    the command (String) will be passed.



12
13
14
15
16
# 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

Instance Method Details

#execute_command(cmd = ARGV, &block) ⇒ Object

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



24
25
26
27
28
# 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) ⇒ Object

Start the process with the given block.

Parameters:

  • (Block)


52
53
54
55
56
57
58
59
60
61
62
# File 'lib/kajiki/runner.rb', line 52

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) ⇒ Object

Stop the process.

Parameters:

  • will (Block)

    execute prior to shutdown, if given.



66
67
68
69
70
71
72
73
# File 'lib/kajiki/runner.rb', line 66

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) ⇒ String

Validate the given command.

Parameters:

  • cmd (Array) (defaults to: ARGV)

    only one command should be given.

Returns:

  • (String)

    extracts out of Array



33
34
35
36
37
38
# 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_optionsObject

Validate the options; otherwise fails.



41
42
43
44
45
46
47
48
# 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]
end