Sha256: 3305a3751649f71f7fe0746a73e121820975cce6f2ba3ab1168b5ee0ae324450

Contents?: true

Size: 993 Bytes

Versions: 2

Compression:

Stored size: 993 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
          begin
            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
      end

      def with_job_hash_context(_job_hash, &_block)
        yield
      end
      alias prepare with_job_hash_context

      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

2 entries across 2 versions & 1 rubygems

Version Path
ezlog-0.10.7 lib/ezlog/sidekiq/job_logger.rb
ezlog-0.10.5 lib/ezlog/sidekiq/job_logger.rb