Sha256: f9dc083abf785ee3e9a9b652bf5da178b681ce6e04995b378fc9c638ded401a4

Contents?: true

Size: 951 Bytes

Versions: 3

Compression:

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

      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

3 entries across 3 versions & 1 rubygems

Version Path
ezlog-0.5.2 lib/ezlog/sidekiq/job_logger.rb
ezlog-0.5.1 lib/ezlog/sidekiq/job_logger.rb
ezlog-0.5.0 lib/ezlog/sidekiq/job_logger.rb