Class: Daemons::Controller
- Inherits:
-
Object
- Object
- Daemons::Controller
- Defined in:
- lib/daemons/cmdline.rb,
lib/daemons/controller.rb
Constant Summary collapse
- COMMANDS =
[ 'start', 'stop', 'restart', 'run', 'zap', 'status' ]
Instance Attribute Summary collapse
-
#app_name ⇒ Object
readonly
Returns the value of attribute app_name.
-
#group ⇒ Object
readonly
Returns the value of attribute group.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
-
.split_argv(argv) ⇒ Object
Split an argv array.
Instance Method Summary collapse
- #catch_exceptions(&block) ⇒ Object
-
#initialize(options = {}, argv = []) ⇒ Controller
constructor
A new instance of Controller.
- #print_usage ⇒ Object
- #run ⇒ Object
-
#setup_options ⇒ Object
This function is used to do a final update of the options passed to the application before they are really used.
Constructor Details
#initialize(options = {}, argv = []) ⇒ Controller
Returns a new instance of Controller.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/daemons/controller.rb', line 21 def initialize( = {}, argv = []) @options = @argv = argv # Allow an app_name to be specified. If not specified use the # basename of the script. @app_name = [:app_name] if [:script] @script = File.([:script]) @app_name ||= File.split(@script)[1] end @app_name ||= 'unknown_application' @command, @controller_part, @app_part = Controller.split_argv(argv) #@options[:dir_mode] ||= :script @optparse = Optparse.new(self) end |
Instance Attribute Details
#app_name ⇒ Object (readonly)
Returns the value of attribute app_name.
5 6 7 |
# File 'lib/daemons/controller.rb', line 5 def app_name @app_name end |
#group ⇒ Object (readonly)
Returns the value of attribute group.
7 8 9 |
# File 'lib/daemons/controller.rb', line 7 def group @group end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
9 10 11 |
# File 'lib/daemons/controller.rb', line 9 def @options end |
Class Method Details
.split_argv(argv) ⇒ Object
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
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/daemons/controller.rb', line 113 def Controller.split_argv(argv) argv = argv.dup command = nil controller_part = [] app_part = [] if COMMANDS.include? argv[0] command = argv.shift end if i = argv.index('--') # Handle the case where no controller options are given, just # options after "--" as well (i == 0) controller_part = (i == 0 ? [] : argv[0..i-1]) app_part = argv[i+1..-1] else controller_part = argv[0..-1] end return command, controller_part, app_part end |
Instance Method Details
#catch_exceptions(&block) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/daemons/cmdline.rb', line 103 def catch_exceptions(&block) begin block.call rescue CmdException, OptionParser::ParseError => e puts "ERROR: #{e.to_s}" puts print_usage() rescue RuntimeException => e puts "ERROR: #{e.to_s}" end end |
#print_usage ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/daemons/cmdline.rb', line 88 def print_usage puts "Usage: #{@app_name} <command> <options> -- <application options>" puts puts "* where <command> is one of:" puts " start start an instance of the application" puts " stop stop all instances of the application" puts " restart stop all instances and restart them afterwards" puts " run start the application and stay on top" puts " zap set the application to a stopped state" puts puts "* and where <options> may contain several of the following:" puts @optparse.usage end |
#run ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/daemons/controller.rb', line 54 def run @options.update @optparse.parse(@controller_part).delete_if {|k,v| !v} () #pp @options @group = ApplicationGroup.new(@app_name, @options) @group.controller_argv = @controller_part @group.app_argv = @app_part @group.setup case @command when 'start' @group.new_application.start when 'run' @options[:ontop] ||= true @group.new_application.start when 'stop' @group.stop_all when 'restart' unless @group.applications.empty? @group.stop_all sleep 1 @group.start_all else puts "Warning: no instances running. Starting..." @group.new_application.start end when 'zap' @group.zap_all when 'status' unless @group.applications.empty? @group.show_status else puts "#{@group.app_name}: no instances running" end when nil raise CmdException.new('no command given') #puts "ERROR: No command given"; puts #print_usage() #raise('usage function not implemented') else raise Error.new("command '#{@command}' not implemented") end end |
#setup_options ⇒ Object
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.
50 51 52 |
# File 'lib/daemons/controller.rb', line 50 def #@options[:ontop] ||= true end |