Sha256: 57681ea84bb36fcb24685d16e19f90f48536251f76b26a63d489ebdcab83c2b8

Contents?: true

Size: 831 Bytes

Versions: 1

Compression:

Stored size: 831 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.2.2 lib/ezlog/sidekiq/job_logger.rb