Sha256: d1166b11f3bafc8efae82404de0bcd99c3fc95c435055475d72f9872c59c7233

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

module Sidekiq
  module Statistic
    class Middleware
      attr_accessor :msg

      def call(worker, msg, queue)
        worker_status = { last_job_status: 'passed'.freeze }
        start = Time.now

        yield
      rescue => e
        worker_status[:last_job_status] = 'failed'.freeze

        raise e
      ensure
        finish = Time.now
        worker_status[:last_runtime] = finish.utc
        worker_status[:time] = (finish - start).to_f.round(3)
        worker_status[:class] = msg['wrapped'.freeze] || worker.class.to_s

        save_entry_for_worker worker_status
      end

      def save_entry_for_worker(worker_status)
        status = worker_status.dup
        time = worker_status[:last_runtime]
        realtime_hash = "#{REDIS_HASH}:realtime:#{time.sec}"
        worker_key = "#{time.to_date}:#{status.delete :class}"

        Sidekiq.redis do |redis|
          redis.pipelined do
            redis.hincrby REDIS_HASH, "#{worker_key}:#{status[:last_job_status]}", 1
            redis.hmset REDIS_HASH, "#{worker_key}:last_job_status", status[:last_job_status],
                                    "#{worker_key}:last_time", status[:last_runtime]
            redis.lpush "#{worker_key}:timeslist", status[:time]

            redis.hincrby realtime_hash, "#{status[:last_job_status]}:#{worker_status[:class]}", 1
            redis.expire realtime_hash, 2
          end
        end
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sidekiq-statistic-1.0.0 lib/sidekiq/statistic/middleware.rb