Sha256: 71de76a36fe8d1d64ae8f7d98c67eec3635ade925c29ddaca3653838fc415f37

Contents?: true

Size: 1.38 KB

Versions: 2

Compression:

Stored size: 1.38 KB

Contents

module Sidetiq
  module Middleware
    class History
      def call(worker, msg, queue, &block)
        if worker.kind_of?(Sidetiq::Schedulable)
          call_with_sidetiq_history(worker, msg, queue, &block)
        else
          yield
        end
      end

      private

      def call_with_sidetiq_history(worker, msg, queue)
        entry = new_history_entry

        yield
      rescue StandardError => e
        entry[:status] = :failure
        entry[:exception] = e.class.to_s
        entry[:error] = e.message
        entry[:backtrace] = e.backtrace

        raise e
      ensure
        save_entry_for_worker(entry, worker)
      end

      def new_history_entry
        {
          status: :success,
          error: "",
          exception: "",
          backtrace: "",
          node: "#{Socket.gethostname}:#{Process.pid}-#{Thread.current.object_id}",
          timestamp: Time.now.iso8601
        }
      end

      def save_entry_for_worker(entry, worker)
        Sidekiq.redis do |redis|
          redis.pipelined do |pipe|
            list_name = "sidetiq:#{worker.class.name}:history"

            pipe.lpush(list_name, JSON.dump(entry))
            pipe.ltrim(list_name, 0, Sidetiq.config.worker_history - 1)
          end
        end
      end
    end
  end
end

Sidekiq.configure_server do |config|
  config.server_middleware do |chain|
    chain.add Sidetiq::Middleware::History
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sidetiq-0.4.0.rc2 lib/sidetiq/middleware/history.rb
sidetiq-0.4.0.rc1 lib/sidetiq/middleware/history.rb