Class: Kajiki::Runner
Class Method Summary collapse
-
.run(opts, &block) ⇒ Object
A wrapper to execute all commands.
Instance Method Summary collapse
-
#execute_command(cmd = ARGV, &block) ⇒ Object
Execute the command with the given block; usually called by ::run.
-
#initialize(opts) ⇒ Runner
constructor
A new instance of Runner.
-
#start(&block) ⇒ Object
Start the process with the given block.
-
#stop(&block) ⇒ Object
Stop the process.
-
#validate_command(cmd = ARGV) ⇒ String
Validate the given command.
-
#validate_options ⇒ Object
Validate the options; otherwise fails.
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.
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.
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) send(cmd, &block) end |
#start(&block) ⇒ Object
Start the process with the given 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.
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.
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_options ⇒ Object
Validate the options; otherwise fails.
41 42 43 44 45 46 47 48 |
# File 'lib/kajiki/runner.rb', line 41 def if @opts[:daemonize] fail 'Must specify PID file.' unless @opts[:pid_given] end @opts[:pid] = File.(@opts[:pid]) if @opts[:pid_given] @opts[:log] = File.(@opts[:log]) if @opts[:log_given] @opts[:error] = File.(@opts[:error]) if @opts[:error_given] end |