Sha256: 2a060b04c9fe0b88265db4731ccdfba59df0a32cc4949cf18efabd14d7dd86c2

Contents?: true

Size: 1.24 KB

Versions: 2

Compression:

Stored size: 1.24 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

      with_elapsed_time_context(start) do
        @logger.info("done")
      end
    rescue Exception
      with_elapsed_time_context(start) do
        @logger.info("fail")
      end

      raise
    end

    def with_job_hash_context(job_hash, &block)
      @logger.with_context(job_hash_context(job_hash), &block)
    end

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

    def with_elapsed_time_context(start, &block)
      @logger.with_context(elapsed_time_context(start), &block)
    end

    def elapsed_time_context(start)
      {elapsed: elapsed(start).to_s}
    end

    private

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sidekiq-6.0.0 lib/sidekiq/job_logger.rb
sidekiq-6.0.0.pre1 lib/sidekiq/job_logger.rb