Class: Sidekiq::Result::ServerMiddleware

Inherits:
Object
  • Object
show all
Includes:
Storage
Defined in:
lib/sidekiq_result/server_middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ ServerMiddleware

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

Parameters:

  • opts (Hash) (defaults to: {})

    middleware initialization options

Options Hash (opts):

  • :expiration (Fixnum)

    ttl for saving complete jobs

[View source]

9
10
11
# File 'lib/sidekiq_result/server_middleware.rb', line 9

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

Instance Method Details

#call(worker, msg, queue) ⇒ Object

Uses sidekiq’s internal jid as id saves result of worker’s method into the specified key

Parameters:

  • worker (Worker)

    worker instance, processed here if it’s class includesResult::Worker

  • msg (Array)

    job args, should have jid format

  • queue (String)

    queue name

[View source]

18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/sidekiq_result/server_middleware.rb', line 18

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
  result = yield
  if worker.respond_to?(:store_result?) && worker.store_result?
    set_object_for_id(worker.jid, result, @expiration)
  end
end