Class: Sidekiq::Status::ClientMiddleware

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

Overview

Should be in the client middleware chain

Constant Summary

Constants included from Storage

Storage::BATCH_LIMIT, Storage::RESERVED_FIELDS

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ ClientMiddleware

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

Parameters:

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

    middleware initialization options

Options Hash (opts):

  • :expiration (Fixnum)

    ttl for complete jobs


11
12
13
# File 'lib/sidekiq-status/client_middleware.rb', line 11

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

Instance Method Details

#call(worker_class, msg, queue, redis_pool = nil) ⇒ Object

Uses msg id and puts :queued status in the job's Redis hash

Parameters:

  • worker_class (Class)

    if includes Sidekiq::Status::Worker, the job gets processed with the plugin

  • msg (Array)

    job arguments

  • queue (String)

    the queue's name

  • redis_pool (ConnectionPool) (defaults to: nil)

    optional redis connection pool


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

def call(worker_class, msg, queue, redis_pool=nil)

  # Determine the actual job class
  klass = msg["args"][0]["job_class"] || worker_class rescue worker_class
  job_class = if klass.is_a?(Class)
                klass
              elsif Module.const_defined?(klass)
                Module.const_get(klass)
              else
                nil
              end

  # Store data if the job is a Sidekiq::Status::Worker
  if job_class && job_class.ancestors.include?(Sidekiq::Status::Worker)
     = {
      jid: msg['jid'],
      status: :queued,
      worker: Sidekiq::Job.new(msg, queue).display_class,
      args: display_args(msg, queue)
    }
    store_for_id msg['jid'], , job_class.new.expiration || @expiration, redis_pool
  end

  yield

end

#display_args(msg, queue) ⇒ Object


47
48
49
50
51
52
53
# File 'lib/sidekiq-status/client_middleware.rb', line 47

def display_args(msg, queue)
  job = Sidekiq::Job.new(msg, queue)
  return job.display_args.to_a.empty? ? nil : job.display_args.to_json
rescue Exception => e
  # For Sidekiq ~> 2.7
  return msg['args'].to_a.empty? ? nil : msg['args'].to_json
end