Sha256: d8afcd678420e5a8cdb4c99ced588490ec4840bda73c57649ccbe1ef54cc57ad

Contents?: true

Size: 1.78 KB

Versions: 22

Compression:

Stored size: 1.78 KB

Contents

module Rpush
  module Daemon
    class Synchronizer
      extend Loggable
      extend StringHelpers

      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

      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

      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

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

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

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
rpush-2.7.0-java lib/rpush/daemon/synchronizer.rb
rpush-2.7.0 lib/rpush/daemon/synchronizer.rb
rpush-2.6.0-java lib/rpush/daemon/synchronizer.rb
rpush-2.6.0 lib/rpush/daemon/synchronizer.rb
rpush-2.5.0 lib/rpush/daemon/synchronizer.rb
rpush-2.4.0-java lib/rpush/daemon/synchronizer.rb
rpush-2.4.0 lib/rpush/daemon/synchronizer.rb
rpush-2.3.2-java lib/rpush/daemon/synchronizer.rb
rpush-2.3.2 lib/rpush/daemon/synchronizer.rb
rpush-2.3.1-java lib/rpush/daemon/synchronizer.rb
rpush-2.3.1 lib/rpush/daemon/synchronizer.rb
rpush-2.3.0-java lib/rpush/daemon/synchronizer.rb
rpush-2.3.0 lib/rpush/daemon/synchronizer.rb
rpush-2.3.0.rc1 lib/rpush/daemon/synchronizer.rb
rpush-2.2.0-java lib/rpush/daemon/synchronizer.rb
rpush-2.2.0 lib/rpush/daemon/synchronizer.rb
rpush-2.1.0-java lib/rpush/daemon/synchronizer.rb
rpush-2.1.0 lib/rpush/daemon/synchronizer.rb
rpush-2.0.1-java lib/rpush/daemon/synchronizer.rb
rpush-2.0.1 lib/rpush/daemon/synchronizer.rb