Sha256: 89504ce9598c000318e6cd56c7511660a4087bec67a2fd1da470a132828808c3

Contents?: true

Size: 1.23 KB

Versions: 15

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true

module Sidekiq
  class JobLogger
    def initialize(logger = Sidekiq.logger)
      @logger = logger
    end

    def call(item, queue)
      start = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
      @logger.info("start")

      yield

      Sidekiq::Context.add(:elapsed, elapsed(start))
      @logger.info("done")
    rescue Exception
      Sidekiq::Context.add(:elapsed, elapsed(start))
      @logger.info("fail")

      raise
    end

    def prepare(job_hash, &block)
      # If we're using a wrapper class, like ActiveJob, use the "wrapped"
      # attribute to expose the underlying thing.
      h = {
        class: job_hash["display_class"] || job_hash["wrapped"] || job_hash["class"],
        jid: job_hash["jid"]
      }
      h[:bid] = job_hash["bid"] if job_hash.has_key?("bid")
      h[:tags] = job_hash["tags"] if job_hash.has_key?("tags")

      Thread.current[:sidekiq_context] = h
      level = job_hash["log_level"]
      if level
        @logger.log_at(level, &block)
      else
        yield
      end
    ensure
      Thread.current[:sidekiq_context] = nil
    end

    private

    def elapsed(start)
      (::Process.clock_gettime(::Process::CLOCK_MONOTONIC) - start).round(3)
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
sidekiq-6.5.12 lib/sidekiq/job_logger.rb
sidekiq-6.5.11 lib/sidekiq/job_logger.rb
sidekiq-6.5.10 lib/sidekiq/job_logger.rb
sidekiq-6.5.9 lib/sidekiq/job_logger.rb
sidekiq-6.5.8 lib/sidekiq/job_logger.rb
sidekiq-6.5.7 lib/sidekiq/job_logger.rb
sidekiq-6.5.6 lib/sidekiq/job_logger.rb
sidekiq-6.5.5 lib/sidekiq/job_logger.rb
sidekiq-6.5.4 lib/sidekiq/job_logger.rb
sidekiq-6.5.3 lib/sidekiq/job_logger.rb
sidekiq-6.5.2 lib/sidekiq/job_logger.rb
sidekiq-6.5.1 lib/sidekiq/job_logger.rb
sidekiq-6.5.0 lib/sidekiq/job_logger.rb
sidekiq-6.4.2 lib/sidekiq/job_logger.rb
sidekiq-6.4.1 lib/sidekiq/job_logger.rb