Class: Rpush::Daemon::Synchronizer

Inherits:
Object
  • Object
show all
Extended by:
Loggable, StringHelpers
Defined in:
lib/rpush/daemon/synchronizer.rb

Constant Summary collapse

APP_ATTRIBUTES_TO_CHECK =
[:certificate, :environment, :auth_key, :client_id, :client_secret].freeze

Class Method Summary collapse

Methods included from Loggable

log_debug, log_error, log_info, log_warn

Methods included from StringHelpers

pluralize

Class Method Details

.attribute_changed?(app, attr) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
56
57
58
59
# File 'lib/rpush/daemon/synchronizer.rb', line 52

def self.attribute_changed?(app, attr)
  if app.respond_to?(attr)
    old_app = AppRunner.app_with_id(app.id)
    app.send(attr) != old_app.send(attr)
  else
    false
  end
end

.changed_attributes(app) ⇒ Object



48
49
50
# File 'lib/rpush/daemon/synchronizer.rb', line 48

def self.changed_attributes(app)
  APP_ATTRIBUTES_TO_CHECK.select { |attr| attribute_changed?(app, attr) }
end

.syncObject



9
10
11
12
13
14
15
16
# File 'lib/rpush/daemon/synchronizer.rb', line 9

def self.sync
  apps = Rpush::Daemon.store.all_apps
  apps.each { |app| sync_app(app) }
  removed = AppRunner.app_ids - apps.map(&:id)
  removed.each { |app_id| AppRunner.stop_app(app_id) }

  ProcTitle.update
end

.sync_app(app) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rpush/daemon/synchronizer.rb', line 18

def self.sync_app(app)
  if !AppRunner.app_running?(app)
    AppRunner.start_app(app)
  elsif (changed_attrs = changed_attributes(app)).count > 0
    changed_attrs_str = changed_attrs.map(&:to_s).join(", ")
    log_info("[#{app.name}] #{changed_attrs_str} changed, restarting...")
    AppRunner.stop_app(app.id)
    AppRunner.start_app(app)
  else
    sync_dispatcher_count(app)
  end
end

.sync_dispatcher_count(app) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rpush/daemon/synchronizer.rb', line 31

def self.sync_dispatcher_count(app)
  num_dispatchers = AppRunner.num_dispatchers_for_app(app)
  diff = num_dispatchers - app.connections
  return if diff == 0

  if diff > 0
    AppRunner.decrement_dispatchers(app, diff)
    start_stop_str = "Stopped"
  else
    AppRunner.increment_dispatchers(app, diff.abs)
    start_stop_str = "Started"
  end

  num_dispatchers = AppRunner.num_dispatchers_for_app(app)
  log_info("[#{app.name}] #{start_stop_str} #{pluralize(diff.abs, 'dispatcher')}. #{num_dispatchers} running.")
end