Class: Sidekiq::Status::ServerMiddleware

Inherits:
Object
  • Object
show all
Includes:
Storage
Defined in:
lib/sidekiq-status/server_middleware.rb

Overview

Should be in the server middleware chain

Constant Summary

Constants included from Storage

Sidekiq::Status::Storage::BATCH_LIMIT, Sidekiq::Status::Storage::RESERVED_FIELDS

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ ServerMiddleware

Parameterized initialization, use it when adding middleware to server chain chain.add Sidekiq::Status::ServerMiddleware, :expiration => 60 * 5

Options Hash (opts):

  • :expiration (Fixnum)

    ttl for complete jobs



10
11
12
# File 'lib/sidekiq-status/server_middleware.rb', line 10

def initialize(opts = {})
  @expiration = opts[:expiration]
end

Instance Method Details

#call(worker, msg, queue) ⇒ Object

Uses sidekiq's internal jid as id puts :working status into Redis hash initializes worker instance with id

Exception handler sets :failed status, re-inserts worker and re-throws the exception Worker::Stopped exception type are processed separately - :stopped status is set, no re-throwing



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sidekiq-status/server_middleware.rb', line 24

def call(worker, msg, queue)
  # a way of overriding default expiration time,
  # so worker wouldn't lose its data
  # and it allows also to overwrite global expiration time on worker basis
  if worker.respond_to? :expiration
    if !worker.expiration && worker.respond_to?(:expiration=)
      worker.expiration = @expiration
    else
      @expiration = worker.expiration
    end
  end

  store_status worker.jid, :working,  @expiration
  yield
  store_status worker.jid, :complete, @expiration
rescue Worker::Stopped
  store_status worker.jid, :stopped, @expiration
rescue SystemExit, Interrupt
  store_status worker.jid, :interrupted, @expiration
  raise
rescue
  store_status worker.jid, :failed,  @expiration
  raise
end