Sha256: 0849ea3f85c015135bf674fb25b5fe58366d3214eb60ff16373996bb6479fa38

Contents?: true

Size: 832 Bytes

Versions: 1

Compression:

Stored size: 832 Bytes

Contents

require 'sidekiq'

module Ezlog
  module Sidekiq
    class JobLogger
      include LogContextHelper

      def call(job_hash, _queue)
        within_log_context(JobContext.from_job_hash(job_hash)) do
          logger.info "#{job_hash['class']} started"
          benchmark { yield }
          logger.info message: "#{job_hash['class']} finished"
        rescue Exception
          logger.info message: "#{job_hash['class']} failed"
          raise
        end
      end

      private

      def benchmark
        start_time = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
        yield
      ensure
        end_time = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
        add_to_log_context duration_sec: (end_time - start_time).round(3)
      end

      def logger
        ::Sidekiq.logger
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ezlog-0.3.1 lib/ezlog/sidekiq/job_logger.rb