Class: Daemons::Controller

### Home Classes Methods

### In Files

- lib/daemons/cmdline.rb
- lib/daemons/controller.rb

### Parent

Object

### Methods

- ::new
- ::split_argv
- #catch_exceptions
- #print_usage
- #run
- #setup_options

### Files

- README
- Releases
- TODO

### Class/Module Index

Quicksearch

- Daemons
- Daemons::Application
- Daemons::ApplicationGroup
- Daemons::CmdException
- Daemons::Controller
- Daemons::Error
- Daemons::Exception
- Daemons::Monitor
- Daemons::Optparse
- Daemons::Pid
- Daemons::PidFile
- Daemons::PidMem
- Daemons::RuntimeException
- Daemons::SystemError
- CurrentProcess
- Daemonize
- Object

No matching classes.

# Daemons::Controller

### Constants

COMMANDS

### Attributes

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

### Public Class Methods

new(options = {}, argv = []) click to toggle source

    # File lib/daemons/controller.rb, line 22
    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

split_argv(argv) click to toggle source

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
    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

### Public Instance Methods

catch_exceptions(&block) click to toggle source

    # File lib/daemons/cmdline.rb, line 107
    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() click to toggle source

    # File lib/daemons/cmdline.rb, line 90
    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

run() click to toggle source

    # File lib/daemons/controller.rb, line 55
    def run
      @options.update @optparse.parse(@controller_part).delete_if {|k,v| !v}

      setup_options()

      #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(@options[:no_wait])
        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 'reload'
          @group.reload_all
        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() click to toggle source

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
    def setup_options
      #@options[:ontop] ||= true
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.