Class: Pebbles::River::DaemonHelper
- Inherits:
-
Object
- Object
- Pebbles::River::DaemonHelper
- 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
-
#logger ⇒ Object
Returns the value of attribute logger.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(adapter, options = {}) ⇒ DaemonHelper
constructor
A new instance of DaemonHelper.
- #run ⇒ Object
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, = {}) @adapter = adapter @name = @adapter.name if @adapter.respond_to?(:name) @name ||= File.basename($0).gsub(/\.rb$/, '') @logger = .fetch(:logger, Logger.new($stderr)) end |
Instance Attribute Details
#logger ⇒ Object
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, = {}) new(adapter, ).run end |
Instance Method Details
#run ⇒ Object
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 |_, | handle_exceptions do start() 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 |_, | handle_exceptions do stop() 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 |_, | handle_exceptions do status() end end end end if ARGV.any? abort "Unknown command '#{ARGV.first}'." else abort "Run with -h for help." end end |