Class Daemons::Controller
In: lib/daemons/cmdline.rb
lib/daemons/controller.rb
Parent: Object

Methods

Constants

COMMANDS = [ 'start', 'stop', 'restart', 'run', 'zap', 'status'

Attributes

app_name  [R] 
group  [R] 
options  [R] 

Public Class methods

[Source]

    # File lib/daemons/controller.rb, line 21
21:     def initialize(options = {}, argv = [])
22:       @options = options
23:       @argv = argv
24:       
25:       # Allow an app_name to be specified. If not specified use the
26:       # basename of the script.
27:       @app_name = options[:app_name]
28:       
29:       if options[:script]
30:         @script = File.expand_path(options[:script])
31:     
32:         @app_name ||= File.split(@script)[1]
33:       end
34:     
35:       @app_name ||= 'unknown_application'
36:       
37:       @command, @controller_part, @app_part = Controller.split_argv(argv)
38:     
39:       #@options[:dir_mode] ||= :script
40:     
41:       @optparse = Optparse.new(self)
42:     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

[Source]

     # File lib/daemons/controller.rb, line 110
110:     def Controller.split_argv(argv)
111:       argv = argv.dup
112:       
113:       command = nil
114:       controller_part = []
115:       app_part = []
116:        
117:       if COMMANDS.include? argv[0]
118:         command = argv.shift
119:       end
120:       
121:       if i = argv.index('--')
122:         # Handle the case where no controller options are given, just
123:         # options after "--" as well (i == 0)
124:         controller_part = (i == 0 ? [] : argv[0..i-1])
125:         app_part = argv[i+1..-1]
126:       else
127:         controller_part = argv[0..-1]
128:       end
129:        
130:       return command, controller_part, app_part
131:     end

Public Instance methods

[Source]

     # File lib/daemons/cmdline.rb, line 104
104:     def catch_exceptions(&block)
105:       begin
106:         block.call
107:       rescue CmdException, OptionParser::ParseError => e
108:         puts "ERROR: #{e.to_s}"
109:         puts
110:         print_usage()
111:       rescue RuntimeException => e
112:         puts "ERROR: #{e.to_s}"
113:       end
114:     end

[Source]

     # File lib/daemons/cmdline.rb, line 89
 89:     def print_usage
 90:       puts "Usage: #{@app_name} <command> <options> -- <application options>"
 91:       puts
 92:       puts "* where <command> is one of:"
 93:       puts "  start         start an instance of the application"
 94:       puts "  stop          stop all instances of the application"
 95:       puts "  restart       stop all instances and restart them afterwards"
 96:       puts "  run           start the application and stay on top"
 97:       puts "  zap           set the application to a stopped state"
 98:       puts
 99:       puts "* and where <options> may contain several of the following:"
100:       
101:       puts @optparse.usage
102:     end

[Source]

    # File lib/daemons/controller.rb, line 54
54:     def run
55:       @options.update @optparse.parse(@controller_part).delete_if {|k,v| !v}
56:       
57:       setup_options()
58:       
59:       #pp @options
60: 
61:       @group = ApplicationGroup.new(@app_name, @options)
62:       @group.controller_argv = @controller_part
63:       @group.app_argv = @app_part
64:       
65:       @group.setup
66:       
67:       case @command
68:         when 'start'
69:           @group.new_application.start
70:         when 'run'
71:           @options[:ontop] ||= true
72:           @group.new_application.start
73:         when 'stop'
74:           @group.stop_all
75:         when 'restart'
76:           unless @group.applications.empty?
77:             @group.stop_all
78:             sleep 1
79:             @group.start_all
80:           end
81:         when 'zap'
82:           @group.zap_all
83:         when 'status'
84:           unless @group.applications.empty?
85:             @group.show_status
86:           else
87:             puts "#{@group.app_name}: no instances running"
88:           end
89:         when nil
90:           raise CmdException.new('no command given')
91:           #puts "ERROR: No command given"; puts
92:           
93:           #print_usage()
94:           #raise('usage function not implemented')
95:         else
96:           raise Error.new("command '#{@command}' not implemented")
97:       end
98:     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.

[Source]

    # File lib/daemons/controller.rb, line 50
50:     def setup_options
51:       #@options[:ontop] ||= true
52:     end

[Validate]