# File lib/daemons/controller.rb, line 22 22: def initialize(options = {}, argv = []) 23: @options = options 24: @argv = argv 25: 26: # Allow an app_name to be specified. If not specified use the 27: # basename of the script. 28: @app_name = options[:app_name] 29: 30: if options[:script] 31: @script = File.expand_path(options[:script]) 32: 33: @app_name ||= File.split(@script)[1] 34: end 35: 36: @app_name ||= 'unknown_application' 37: 38: @command, @controller_part, @app_part = Controller.split_argv(argv) 39: 40: #@options[:dir_mode] ||= :script 41: 42: @optparse = Optparse.new(self) 43: end
Split an argv array. argv is assumed to be in the following format:
['command', 'controller option 1', 'controller option 2', ..., '--', 'app option 1', ...]
command must be one of the commands listed in COMMANDS
Returns: the command as a string, the controller options as an array, the appliation options as an array
# File lib/daemons/controller.rb, line 116 116: def Controller.split_argv(argv) 117: argv = argv.dup 118: 119: command = nil 120: controller_part = [] 121: app_part = [] 122: 123: if COMMANDS.include? argv[0] 124: command = argv.shift 125: end 126: 127: if i = argv.index('--') 128: # Handle the case where no controller options are given, just 129: # options after "--" as well (i == 0) 130: controller_part = (i == 0 ? [] : argv[0..i-1]) 131: app_part = argv[i+1..1] 132: else 133: controller_part = argv[0..1] 134: end 135: 136: return command, controller_part, app_part 137: end
# File lib/daemons/cmdline.rb, line 110 110: def catch_exceptions(&block) 111: begin 112: block.call 113: rescue CmdException, OptionParser::ParseError => e 114: puts "ERROR: #{e.to_s}" 115: puts 116: print_usage() 117: rescue RuntimeException => e 118: puts "ERROR: #{e.to_s}" 119: end 120: end
# File lib/daemons/cmdline.rb, line 93 93: def print_usage 94: puts "Usage: #{@app_name} <command> <options> -- <application options>" 95: puts 96: puts "* where <command> is one of:" 97: puts " start start an instance of the application" 98: puts " stop stop all instances of the application" 99: puts " restart stop all instances and restart them afterwards" 100: puts " reload send a SIGHUP to all instances of the application" 101: puts " run start the application and stay on top" 102: puts " zap set the application to a stopped state" 103: puts " status show status (PID) of application instances" 104: puts 105: puts "* and where <options> may contain several of the following:" 106: 107: puts @optparse.usage 108: end
# File lib/daemons/controller.rb, line 55 55: def run 56: @options.update @optparse.parse(@controller_part).delete_if {|k,v| !v} 57: 58: setup_options() 59: 60: #pp @options 61: 62: @group = ApplicationGroup.new(@app_name, @options) 63: @group.controller_argv = @controller_part 64: @group.app_argv = @app_part 65: 66: @group.setup 67: 68: case @command 69: when 'start' 70: @group.new_application.start 71: when 'run' 72: @options[:ontop] ||= true 73: @group.new_application.start 74: when 'stop' 75: @group.stop_all(@options[:no_wait]) 76: when 'restart' 77: unless @group.applications.empty? 78: @group.stop_all 79: sleep(1) 80: @group.start_all 81: else 82: puts "Warning: no instances running. Starting..." 83: @group.new_application.start 84: end 85: when 'reload' 86: @group.reload_all 87: when 'zap' 88: @group.zap_all 89: when 'status' 90: unless @group.applications.empty? 91: @group.show_status 92: else 93: puts "#{@group.app_name}: no instances running" 94: end 95: when nil 96: raise CmdException.new('no command given') 97: #puts "ERROR: No command given"; puts 98: 99: #print_usage() 100: #raise('usage function not implemented') 101: else 102: raise Error.new("command '#{@command}' not implemented") 103: end 104: end
This function is used to do a final update of the options passed to the application before they are really used.
Note that this function should only update @options and no other variables.
# File lib/daemons/controller.rb, line 51 51: def setup_options 52: #@options[:ontop] ||= true 53: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.