1
0
Fork 0
mirror of https://github.com/mperham/sidekiq.git synced 2022-11-09 13:52:34 -05:00
mperham--sidekiq/lib/sidekiq/job_logger.rb
Mike Perham 29dca70e24
Introduce Sidekiq::Capsule (#5487)
* Initial work on Sidekiq::Config

* Initial work on Sidekiq::Config

* reduce dependencies in deploy marks

* bare sidekiq and webapp

* Modify runtime to work with Capsules

* Cleanup

* Rename test files to remove test_ prefix

* Update test suite and standard rules to be more compliant

* Move constant definition outside code, per standard formatting

* Loads of changes for introduction of Capsules

* Remove Redis adapter abstraction

* update capsule overview

* Ensure Sidekiq.redis uses the correct pool for jobs running within a Capsule

* Use default_capsule for safety

* Slow down the beat to halve its Redis overhead

* move config fixtures into cfg/

* Add capsule middleware test

* use accessor
2022-08-25 10:15:11 -07:00

51 lines
1.2 KiB
Ruby

# frozen_string_literal: true
module Sidekiq
class JobLogger
def initialize(logger)
@logger = logger
end
def call(item, queue)
start = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
@logger.info("start")
yield
Sidekiq::Context.add(:elapsed, elapsed(start))
@logger.info("done")
rescue Exception
Sidekiq::Context.add(:elapsed, elapsed(start))
@logger.info("fail")
raise
end
def prepare(job_hash, &block)
# If we're using a wrapper class, like ActiveJob, use the "wrapped"
# attribute to expose the underlying thing.
h = {
class: job_hash["display_class"] || job_hash["wrapped"] || job_hash["class"],
jid: job_hash["jid"]
}
h[:bid] = job_hash["bid"] if job_hash.has_key?("bid")
h[:tags] = job_hash["tags"] if job_hash.has_key?("tags")
Thread.current[:sidekiq_context] = h
level = job_hash["log_level"]
if level
@logger.log_at(level, &block)
else
yield
end
ensure
Thread.current[:sidekiq_context] = nil
end
private
def elapsed(start)
(::Process.clock_gettime(::Process::CLOCK_MONOTONIC) - start).round(3)
end
end
end