Class: Pebbles::River::DaemonHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/pebbles/river/daemon_helper.rb

Overview

Simple helper class for easily writing daemons that run multiple queue workers. Handles command line parsing and daemonization.

Adapter should support:

  • ‘name`: Name of the process. Optional, defaults to `$0`.

  • ‘configure_start_command(command)`. Implement to add more options to the start command, eg. configuration options. Optional.

  • ‘on_start(options, helper)`. Implement to inject code before the program starts. Options are the start options, a hash. Optional.

  • ‘configure_supervisor(supervisor)`. This must call `add_listener` on the supervisor to add workers. Required.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter, options = {}) ⇒ DaemonHelper

Returns a new instance of DaemonHelper.



41
42
43
44
45
46
47
48
# File 'lib/pebbles/river/daemon_helper.rb', line 41

def initialize(adapter, options = {})
  @adapter = adapter

  @name = @adapter.name if @adapter.respond_to?(:name)
  @name ||= File.basename($0).gsub(/\.rb$/, '')

  @logger = options.fetch(:logger, Logger.new($stderr))
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



35
36
37
# File 'lib/pebbles/river/daemon_helper.rb', line 35

def logger
  @logger
end

Class Method Details

.run(adapter, options = {}) ⇒ Object



37
38
39
# File 'lib/pebbles/river/daemon_helper.rb', line 37

def self.run(adapter, options = {})
  new(adapter, options).run
end

Instance Method Details

#runObject



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
90
91
92
93
94
95
# File 'lib/pebbles/river/daemon_helper.rb', line 50

def run
  Mercenary.program(@name) do |p|
    p.syntax "#{@name} <subcommand> [OPTION ...]"
    p.command(:start) do |c|
      c.syntax 'start'
      c.description 'Starts daemon'
      c.option :daemonize, '-d', '--daemon', 'To daemonize; otherwise will run synchronously.'
      c.option :pidfile, '-p', '--pidfile PIDFILE', 'Path to pid file.'
      c.option :workers, Integer, '-w', '--workers N', 'Set number of workers per queue (defaults to 1).'
      c.action do |_, options|
        handle_exceptions do
          start(options)
        end
      end
      if @adapter.respond_to?(:configure_start_command)
        @adapter.configure_start_command(c)
      end
    end
    p.command(:stop) do |c|
      c.syntax 'stop'
      c.description 'Stops daemon'
      c.option :pidfile, '-p', '--pidfile PIDFILE', 'Path to pid file.'
      c.action do |_, options|
        handle_exceptions do
          stop(options)
        end
      end
    end
    p.command(:status) do |c|
      c.syntax 'status'
      c.description 'Prints daemon status'
      c.option :pidfile, '-p', '--pidfile PIDFILE', 'Path to pid file.'
      c.action do |_, options|
        handle_exceptions do
          status(options)
        end
      end
    end
  end

  if ARGV.any?
    abort "Unknown command '#{ARGV.first}'."
  else
    abort "Run with -h for help."
  end
end