Class: Push::Daemon::App

Inherits:
Object
  • Object
show all
Extended by:
DatabaseReconnectable
Defined in:
lib/push/daemon/app.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DatabaseReconnectable

adaptor_errors, check_database_is_connected, database_connection_lost, reconnect_database, sleep_to_avoid_thrashing, with_database_reconnect_and_retry

Constructor Details

#initialize(name) ⇒ App

Returns a new instance of App.



49
50
51
52
53
54
55
56
# File 'lib/push/daemon/app.rb', line 49

def initialize(name)
  @name = name
  @configs = []
  @handlers = []
  @providers = []
  @queue = DeliveryQueue.new
  @database_connections = 0
end

Class Attribute Details

.appsObject (readonly)

Returns the value of attribute apps.



6
7
8
# File 'lib/push/daemon/app.rb', line 6

def apps
  @apps
end

Instance Attribute Details

#configsObject

Returns the value of attribute configs.



58
59
60
# File 'lib/push/daemon/app.rb', line 58

def configs
  @configs
end

#database_connectionsObject (readonly)

Returns the value of attribute database_connections.



59
60
61
# File 'lib/push/daemon/app.rb', line 59

def database_connections
  @database_connections
end

Class Method Details

.database_connectionsObject



45
46
47
# File 'lib/push/daemon/app.rb', line 45

def self.database_connections
  @apps.empty? ? 0 : @apps.values.collect{|x| x.database_connections }.inject(:+)
end

.deliver(notification) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/push/daemon/app.rb', line 29

def self.deliver(notification)
  if app = @apps[notification.app]
    app.deliver(notification)
  else
    Rapns::Daemon.logger.error("No such app '#{notification.app}' for notification #{notification.id}.")
  end
end

.loadObject



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/push/daemon/app.rb', line 11

def self.load
  with_database_reconnect_and_retry('App.load') do
    configurations = Push::Configuration.enabled
    configurations.each do |config|
      if @apps[config.app] == nil
        @apps[config.app] = App.new(config.app)
      end
      @apps[config.app].configs << config
    end
  end
end

.readyObject



23
24
25
26
27
# File 'lib/push/daemon/app.rb', line 23

def self.ready
  ready = []
  @apps.each { |app, runner| ready << app if runner.ready? }
  ready
end

.startObject



37
38
39
# File 'lib/push/daemon/app.rb', line 37

def self.start
  @apps.values.map(&:start)
end

.stopObject



41
42
43
# File 'lib/push/daemon/app.rb', line 41

def self.stop
  @apps.values.map(&:stop)
end

Instance Method Details

#deliver(notification) ⇒ Object



61
62
63
# File 'lib/push/daemon/app.rb', line 61

def deliver(notification)
  @queue.push(notification)
end

#ready?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/push/daemon/app.rb', line 83

def ready?
  @queue.notifications_processed?
end

#startObject



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/push/daemon/app.rb', line 65

def start
  @connection_pool = ConnectionPool.new
  @configs.each do |config|
    provider = load_provider(config.name, config.properties.merge({:connections => config.connections, :name => config.app}))
    @providers << provider
    @database_connections += provider.totalconnections
    @connection_pool.populate(provider)
  end
  @connection_pool.size.times do |i|
    @handlers << start_handler(i)
  end
end

#stopObject



78
79
80
81
# File 'lib/push/daemon/app.rb', line 78

def stop
  @handlers.map(&:stop)
  @providers.map(&:stop)
end