| Class | Daemons::Controller |
| In: |
lib/daemons/cmdline.rb
lib/daemons/controller.rb |
| Parent: | Object |
| COMMANDS | = | [ 'start', 'stop', 'restart', 'run', 'zap', 'reload', 'status' |
| app_name | [R] | |
| group | [R] | |
| options | [R] |
# 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 108
108: def catch_exceptions(&block)
109: begin
110: block.call
111: rescue CmdException, OptionParser::ParseError => e
112: puts "ERROR: #{e.to_s}"
113: puts
114: print_usage()
115: rescue RuntimeException => e
116: puts "ERROR: #{e.to_s}"
117: end
118: end
# File lib/daemons/cmdline.rb, line 91
91: def print_usage
92: puts "Usage: #{@app_name} <command> <options> -- <application options>"
93: puts
94: puts "* where <command> is one of:"
95: puts " start start an instance of the application"
96: puts " stop stop all instances of the application"
97: puts " restart stop all instances and restart them afterwards"
98: puts " reload send a SIGHUP to all instances of the application"
99: puts " run start the application and stay on top"
100: puts " zap set the application to a stopped state"
101: puts " status show status (PID) of application instances"
102: puts
103: puts "* and where <options> may contain several of the following:"
104:
105: puts @optparse.usage
106: 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