Sha256: 32a20ebfc1face2b39d13799d7b2992028441f19a510deb97f1191643f8c593e

Contents?: true

Size: 1.6 KB

Versions: 35

Compression:

Stored size: 1.6 KB

Contents

module Katello
  class CandlepinEventListener
    STATUS_CACHE_KEY = 'candlepin_events_status'.freeze

    Event = Struct.new(:subject, :content)

    cattr_accessor :client_factory

    @failed_count = 0
    @processed_count = 0

    def self.logger
      ::Foreman::Logging.logger('katello/candlepin_events')
    end

    def self.running?
      @running == true && @client&.running?
    end

    def self.close
      return unless running?

      logger.info("Closing candlepin event listener")
      @client&.close
      reset
    end

    def self.reset
      @processed_count = 0
      @failed_count = 0
      @running = false
      Rails.cache.delete(STATUS_CACHE_KEY)
    end

    def self.run
      @client = client_factory.call
      @client.subscribe do |message|
        handle_message(message)
      end

      @running = true
    end

    def self.status(refresh: true)
      Rails.cache.fetch(STATUS_CACHE_KEY, force: refresh) do
        {
          processed_count: @processed_count,
          failed_count: @failed_count,
          running: running?
        }
      end
    end

    def self.handle_message(message)
      ::Katello::Util::Support.with_db_connection(logger) do
        subject = "#{message.headers['EVENT_TARGET']}.#{message.headers['EVENT_TYPE']}".downcase
        cp_event = Event.new(subject, message.body)
        ::Katello::Candlepin::EventHandler.new(logger).handle(cp_event)
      end
      @processed_count += 1
    rescue => e
      @failed_count += 1
      logger.error("Error handling Candlepin event")
      logger.error(e.message)
      logger.error(e.backtrace.join("\n"))
    end
  end
end

Version data entries

35 entries across 35 versions & 1 rubygems

Version Path
katello-3.18.5 app/services/katello/candlepin_event_listener.rb
katello-3.18.4 app/services/katello/candlepin_event_listener.rb
katello-3.18.3.1 app/services/katello/candlepin_event_listener.rb
katello-3.18.3 app/services/katello/candlepin_event_listener.rb
katello-3.18.2.1 app/services/katello/candlepin_event_listener.rb
katello-3.18.2 app/services/katello/candlepin_event_listener.rb
katello-3.17.3 app/services/katello/candlepin_event_listener.rb
katello-3.18.1.1 app/services/katello/candlepin_event_listener.rb
katello-3.17.2 app/services/katello/candlepin_event_listener.rb
katello-3.18.1 app/services/katello/candlepin_event_listener.rb
katello-3.18.0 app/services/katello/candlepin_event_listener.rb
katello-3.17.1 app/services/katello/candlepin_event_listener.rb
katello-3.16.2 app/services/katello/candlepin_event_listener.rb
katello-3.18.0.rc2.1 app/services/katello/candlepin_event_listener.rb
katello-3.18.0.rc2 app/services/katello/candlepin_event_listener.rb
katello-3.17.0 app/services/katello/candlepin_event_listener.rb
katello-3.18.0.rc1 app/services/katello/candlepin_event_listener.rb
katello-3.17.0.rc2.2 app/services/katello/candlepin_event_listener.rb
katello-3.17.0.rc2.1 app/services/katello/candlepin_event_listener.rb
katello-3.16.1.2 app/services/katello/candlepin_event_listener.rb