Class: Daemons::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/daemons/cmdline.rb,
lib/daemons/controller.rb

Constant Summary collapse

COMMANDS =
%w(start stop restart run zap reload status)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, argv = []) ⇒ Controller

Returns a new instance of Controller.


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/daemons/controller.rb', line 12

def initialize(options = {}, argv = [])
  @options = options
  @argv = argv

  # Allow an app_name to be specified. If not specified use the
  # basename of the script.
  @app_name = options[:app_name]

  if options[:script]
    @script = File.expand_path(options[: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_nameObject (readonly)

Returns the value of attribute app_name


4
5
6
# File 'lib/daemons/controller.rb', line 4

def app_name
  @app_name
end

#groupObject (readonly)

Returns the value of attribute group


6
7
8
# File 'lib/daemons/controller.rb', line 6

def group
  @group
end

#optionsObject (readonly)

Returns the value of attribute options


8
9
10
# File 'lib/daemons/controller.rb', line 8

def options
  @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


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/daemons/controller.rb', line 100

def self.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

  [command, controller_part, app_part]
end

Instance Method Details

#catch_exceptions(&block) ⇒ Object


98
99
100
101
102
103
104
105
106
107
108
# File 'lib/daemons/cmdline.rb', line 98

def catch_exceptions(&block)
  begin
    block.call
  rescue CmdException, OptionParser::ParseError => e
    puts "ERROR: #{e}"
    puts
    print_usage
  rescue RuntimeException => e
    puts "ERROR: #{e}"
  end
end

81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/daemons/cmdline.rb', line 81

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 '  reload        send a SIGHUP to all instances of the application'
  puts '  run           start the application and stay on top'
  puts '  zap           set the application to a stopped state'
  puts '  status        show status (PID) of application instances'
  puts
  puts '* and where <options> may contain several of the following:'

  puts @optparse.usage
end

#runObject


44
45
46
47
48
49
50
51
52
53
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
# File 'lib/daemons/controller.rb', line 44

def run
  @options.update @optparse.parse(@controller_part).delete_if { |k, v| !v }

  setup_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(@options[:no_wait])
    when 'restart'
      unless @group.applications.empty?
        @group.stop_all
        sleep(1)
        @group.start_all
      else
        $stderr.puts "#{@group.app_name}: warning: no instances running. Starting..."
        @group.new_application.start
      end
    when 'reload'
      @group.reload_all
    when 'zap'
      @group.zap_all
    when 'status'
      unless @group.applications.empty?
        @group.show_status
        exit 3 if not @group.running?   # exit with status 3 to indicate that no apps are running
      else
        $stderr.puts "#{@group.app_name}: no instances running"
        exit 3                          # exit with status 3 to indicate that no apps are running
      end
    when nil
      fail CmdException.new('no command given')
    else
      fail Error.new("command '#{@command}' not implemented")
  end
end

#setup_optionsObject

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.


40
41
42
# File 'lib/daemons/controller.rb', line 40

def setup_options
  # @options[:ontop] ||= true
end