Sha256: fa68643a336100310941051bd5c2ca8e80c09c239b06d19c523176e7a14d36ab

Contents?: true

Size: 1.6 KB

Versions: 17

Compression:

Stored size: 1.6 KB

Contents

module Workhorse
  class Daemon::ShellHandler
    def self.run(options = {}, &block)
      unless ARGV.count == 1
        usage
        exit 99
      end

      lockfile_path = options.delete(:lockfile) || 'workhorse.lock'
      lockfile = File.open(lockfile_path, 'a')
      lockfile.flock(File::LOCK_EX || File::LOCK_NB)

      daemon = Workhorse::Daemon.new(options, &block)

      begin
        case ARGV.first
        when 'start'
          exit daemon.start
        when 'stop'
          exit daemon.stop
        when 'kill'
          exit daemon.stop(true)
        when 'status'
          exit daemon.status
        when 'watch'
          exit daemon.watch
        when 'restart'
          exit daemon.restart
        when 'usage'
          usage
          exit 99
        else
          usage
        end

        exit 0
      rescue => e
        warn "#{e.message}\n#{e.backtrace.join("\n")}"
        exit 99
      ensure
        lockfile.flock(File::LOCK_UN)
      end
    end

    def self.usage
      warn <<USAGE
Usage: #{$PROGRAM_NAME} start|stop|status|watch|restart|usage

Options:

  start
    Start the daemon

  stop
    Stop the daemon

  kill
    Kill the daemon

  status
    Query the status of the daemon. Exit with status 1 if any worker is
    not running.

  watch
    Checks the status (running or stopped) and whether it is as
    expected. Starts the daemon if it is expected to run but is not.

  restart
    Shortcut for consecutive 'stop' and 'start'.

  usage
    Show this message

Exit status:
 0  if OK,
 1  if at least one worker has an unexpected status,
 99 on all other errors.
USAGE
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
workhorse-1.1.0 lib/workhorse/daemon/shell_handler.rb
workhorse-1.0.1 lib/workhorse/daemon/shell_handler.rb
workhorse-1.0.0 lib/workhorse/daemon/shell_handler.rb
workhorse-1.0.0.beta2 lib/workhorse/daemon/shell_handler.rb
workhorse-1.0.0.beta1 lib/workhorse/daemon/shell_handler.rb
workhorse-1.0.0.beta0 lib/workhorse/daemon/shell_handler.rb
workhorse-0.6.9 lib/workhorse/daemon/shell_handler.rb
workhorse-0.6.8 lib/workhorse/daemon/shell_handler.rb
workhorse-0.6.7 lib/workhorse/daemon/shell_handler.rb
workhorse-0.6.6 lib/workhorse/daemon/shell_handler.rb
workhorse-0.6.5 lib/workhorse/daemon/shell_handler.rb
workhorse-0.6.4 lib/workhorse/daemon/shell_handler.rb
workhorse-0.6.3 lib/workhorse/daemon/shell_handler.rb
workhorse-0.6.2 lib/workhorse/daemon/shell_handler.rb
workhorse-0.6.1 lib/workhorse/daemon/shell_handler.rb
workhorse-0.6.0 lib/workhorse/daemon/shell_handler.rb
workhorse-0.5.1 lib/workhorse/daemon/shell_handler.rb