Sha256: f34f317465b26028e20d4f4c1bae9cda14e690df914e912a593523c70bd9f507

Contents?: true

Size: 1.51 KB

Versions: 5

Compression:

Stored size: 1.51 KB

Contents

module Sidekiq
  ##
  # Sidekiq::Component assumes a config instance is available at @config
  module Component # :nodoc:
    attr_reader :config

    def watchdog(last_words)
      yield
    rescue Exception => ex
      handle_exception(ex, {context: last_words})
      raise ex
    end

    def safe_thread(name, &block)
      Thread.new do
        Thread.current.name = name
        watchdog(name, &block)
      end
    end

    def logger
      config.logger
    end

    def redis(&block)
      config.redis(&block)
    end

    def tid
      Thread.current["sidekiq_tid"] ||= (Thread.current.object_id ^ ::Process.pid).to_s(36)
    end

    def hostname
      ENV["DYNO"] || Socket.gethostname
    end

    def process_nonce
      @@process_nonce ||= SecureRandom.hex(6)
    end

    def identity
      @@identity ||= "#{hostname}:#{::Process.pid}:#{process_nonce}"
    end

    def handle_exception(ex, ctx = {})
      config.handle_exception(ex, ctx)
    end

    def fire_event(event, options = {})
      oneshot = options.fetch(:oneshot, true)
      reverse = options[:reverse]
      reraise = options[:reraise]
      logger.debug("Firing #{event} event") if oneshot

      arr = config[:lifecycle_events][event]
      arr.reverse! if reverse
      arr.each do |block|
        block.call
      rescue => ex
        handle_exception(ex, {context: "Exception during Sidekiq lifecycle event.", event: event})
        raise ex if reraise
      end
      arr.clear if oneshot # once we've fired an event, we never fire it again
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
sidekiq-7.0.3 lib/sidekiq/component.rb
sidekiq-7.0.2 lib/sidekiq/component.rb
sidekiq-7.0.1 lib/sidekiq/component.rb
sidekiq-7.0.0 lib/sidekiq/component.rb
sidekiq-7.0.0.beta1 lib/sidekiq/component.rb