Class: Rpush::Daemon::Synchronizer

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

Class Method Summary collapse

Methods included from Loggable

log_error, log_info, log_warn

Methods included from StringHelpers

pluralize

Class Method Details

.certificate_changed?(app) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.certificate_changed?(app)
  old_app = AppRunner.app_with_id(app.id)
  app.certificate != old_app.certificate
end

.environment_changed?(app) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.environment_changed?(app)
  old_app = AppRunner.app_with_id(app.id)
  app.environment != old_app.environment
end

.syncObject



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

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



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

def self.sync_app(app)
  if !AppRunner.app_running?(app)
    AppRunner.start_app(app)
  elsif certificate_changed?(app)
    log_info("[#{app.name}] Certificate changed, restarting...")
    AppRunner.stop_app(app.id)
    AppRunner.start_app(app)
  elsif environment_changed?(app)
    log_info("[#{app.name}] Environment changed, restarting...")
    AppRunner.stop_app(app.id)
    AppRunner.start_app(app)
  else
    sync_dispatcher_count(app)
  end
end

.sync_dispatcher_count(app) ⇒ Object



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

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