Sha256: f309ad07f7aaafe38c56ab6be1673befac6180ddd9e56dfd5a6907e9f7162957

Contents?: true

Size: 1.34 KB

Versions: 8

Compression:

Stored size: 1.34 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|
          list_name = "sidetiq:#{worker.class.name}:history"

          redis.lpush(list_name, Sidekiq.dump_json(entry))
          redis.ltrim(list_name, 0, Sidetiq.config.worker_history - 1)
        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

8 entries across 8 versions & 1 rubygems

Version Path
sidetiq-0.6.3 lib/sidetiq/middleware/history.rb
sidetiq-0.6.2 lib/sidetiq/middleware/history.rb
sidetiq-0.6.1 lib/sidetiq/middleware/history.rb
sidetiq-0.6.0 lib/sidetiq/middleware/history.rb
sidetiq-0.5.0 lib/sidetiq/middleware/history.rb
sidetiq-0.4.3 lib/sidetiq/middleware/history.rb
sidetiq-0.4.2 lib/sidetiq/middleware/history.rb
sidetiq-0.4.1 lib/sidetiq/middleware/history.rb