Class: APN::SenderDaemon
- Inherits:
-
Object
- Object
- APN::SenderDaemon
- Defined in:
- lib/apn/sender_daemon.rb
Overview
A wrapper designed to daemonize an APN::Sender instance to keep in running in the background. Connects worker’s output to a custom logger, if available. Creates a pid file suitable for monitoring with monit.
Based off delayed_job’s great example, except we can be much lighter by not loading the entire Rails environment. To use in a Rails app, script/generate apn_sender
.
Instance Method Summary collapse
- #daemonize ⇒ Object
-
#initialize(args) ⇒ SenderDaemon
constructor
A new instance of SenderDaemon.
- #run(worker_name = nil) ⇒ Object
Constructor Details
#initialize(args) ⇒ SenderDaemon
Returns a new instance of SenderDaemon.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/apn/sender_daemon.rb', line 16 def initialize(args) @options = {:worker_count => 1, :environment => :development, :delay => 5} optparse = OptionParser.new do |opts| opts. = "Usage: #{File.basename($0)} [options] start|stop|restart|run" opts.on('-h', '--help', 'Show this message') do puts opts exit 1 end opts.on('-e', '--environment=NAME', 'Specifies the environment to run this apn_sender under ([development]/production).') do |e| @options[:environment] = e end opts.on('--cert-path=NAME', 'Path to directory containing apn .pem certificates.') do |path| @options[:cert_path] = path end opts.on('-n', '--number-of-workers=WORKERS', "Number of unique workers to spawn") do |worker_count| @options[:worker_count] = worker_count.to_i rescue 1 end opts.on('-v', '--verbose', "Turn on verbose mode") do @options[:verbose] = true end opts.on('-V', '--very-verbose', "Turn on very verbose mode") do @options[:very_verbose] = true end opts.on('-d', '--delay=D', "Delay between rounds of work (seconds)") do |d| @options[:delay] = d end opts.on('-a', '--app=NAME', 'Specifies the application for this apn_sender') do |a| @options[:app] = a end end # If no arguments, give help screen @args = optparse.parse!(args.empty? ? ['-h'] : args) @options[:verbose] = true if @options[:very_verbose] end |
Instance Method Details
#daemonize ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/apn/sender_daemon.rb', line 54 def daemonize @options[:worker_count].times do |worker_index| process_name = @options[:worker_count] == 1 ? "apn_sender" : "apn_sender.#{worker_index}" Daemons.run_proc(process_name, :dir => "#{::RAILS_ROOT}/tmp/pids", :dir_mode => :normal, :ARGV => @args) do |*args| run process_name end end end |
#run(worker_name = nil) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/apn/sender_daemon.rb', line 63 def run(worker_name = nil) logger = Logger.new(File.join(::RAILS_ROOT, 'log', 'apn_sender.log')) worker = APN::Sender.new(@options) worker.logger = logger worker.verbose = @options[:verbose] worker.very_verbose = @options[:very_verbose] worker.work(@options[:delay]) rescue => e STDERR.puts e. logger.fatal(e) if logger && logger.respond_to?(:fatal) exit 1 end |