Sha256: 1d55af6edae9e65dc2fef65019c00e62c67b9e418e4f870a50fb7cc3e23a61b4

Contents?: true

Size: 874 Bytes

Versions: 5

Compression:

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

      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

5 entries across 5 versions & 1 rubygems

Version Path
ezlog-0.4.0 lib/ezlog/sidekiq/job_logger.rb
ezlog-0.3.5 lib/ezlog/sidekiq/job_logger.rb
ezlog-0.3.4 lib/ezlog/sidekiq/job_logger.rb
ezlog-0.3.3 lib/ezlog/sidekiq/job_logger.rb
ezlog-0.3.2 lib/ezlog/sidekiq/job_logger.rb