Class: Resque::Plugins::Aps::Daemon

Inherits:
Object
  • Object
show all
Extended by:
Helper
Includes:
Helper
Defined in:
lib/resque/plugins/aps/daemon.rb

Class Method Summary collapse

Methods included from Helper

logger

Class Method Details

.handle_aps_queues(max_age = 60) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/resque/plugins/aps/daemon.rb', line 28

def handle_aps_queues(max_age = 60)
  Resque.aps_application_names(0, 0).each do |app_name|
    count_not = Resque.aps_notification_count_for_application(app_name)
    if count_not > 0
      count_apps = Resque.aps_applications_queued_count(app_name).to_i
      if count_apps == 0
        Resque.enqueue(Resque::Plugins::Aps::Application, app_name, true)
      elsif Resque.aps_age(app_name) >= max_age
        Resque.enqueue(Resque::Plugins::Aps::Application, app_name, false)
      else
        logger.error "Unable to queue APS application: #{app_name}"
      end
    end
  end
end

.handle_shutdownObject



53
54
55
56
57
# File 'lib/resque/plugins/aps/daemon.rb', line 53

def handle_shutdown
  exit if @shutdown
  yield
  exit if @shutdown
end

.poll_sleepObject

Sleeps and returns true



60
61
62
63
64
65
# File 'lib/resque/plugins/aps/daemon.rb', line 60

def poll_sleep
  @sleeping = true
  handle_shutdown { sleep 5 }
  @sleeping = false
  true
end

.register_signal_handlersObject

For all signals, set the shutdown flag and wait for current poll/enqueing to finish (should be almost istant). In the case of sleeping, exit immediately.



47
48
49
50
51
# File 'lib/resque/plugins/aps/daemon.rb', line 47

def register_signal_handlers
  trap("TERM") { shutdown }
  trap("INT")  { shutdown }
  trap('QUIT') { shutdown } unless defined? JRUBY_VERSION
end

.runObject

Schedule all jobs and continually look for delayed jobs (never returns)



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/resque/plugins/aps/daemon.rb', line 13

def run
  # trap signals
  register_signal_handlers

  # Now start the scheduling part of the loop.
  loop do
    handle_aps_queues(30)
    poll_sleep
  end

  # never gets here.
rescue
  logger.error "#{$!}: #{$!.backtrace.join("\n")}"
end

.shutdownObject

Sets the shutdown flag, exits if sleeping



68
69
70
71
# File 'lib/resque/plugins/aps/daemon.rb', line 68

def shutdown
  @shutdown = true
  exit if @sleeping
end