Class: Sidekiq::Capsule

Inherits:
Object
  • Object
show all
Includes:
Component
Defined in:
lib/sidekiq/capsule.rb

Overview

A Sidekiq::Capsule is the set of resources necessary to process one or more queues with a given concurrency. One “default” Capsule is started but the user may declare additional Capsules in their initializer.

This capsule will pull jobs from the “single” queue and process the jobs with one thread, meaning the jobs will be processed serially.

Sidekiq.configure_server do |config|

config.capsule("single-threaded") do |cap|
  cap.concurrency = 1
  cap.queues = %w(single)
end

end

Instance Attribute Summary collapse

Attributes included from Component

#config

Instance Method Summary collapse

Methods included from Component

#fire_event, #handle_exception, #hostname, #identity, #process_nonce, #safe_thread, #tid, #watchdog

Constructor Details

#initialize(name, config) ⇒ Capsule

Returns a new instance of Capsule.



27
28
29
30
31
32
33
34
# File 'lib/sidekiq/capsule.rb', line 27

def initialize(name, config)
  @name = name
  @config = config
  @queues = ["default"]
  @weights = {"default" => 0}
  @concurrency = config[:concurrency]
  @mode = :strict
end

Instance Attribute Details

#concurrencyObject

Returns the value of attribute concurrency.



23
24
25
# File 'lib/sidekiq/capsule.rb', line 23

def concurrency
  @concurrency
end

#modeObject (readonly)

Returns the value of attribute mode.



24
25
26
# File 'lib/sidekiq/capsule.rb', line 24

def mode
  @mode
end

#nameObject (readonly)

Returns the value of attribute name.



21
22
23
# File 'lib/sidekiq/capsule.rb', line 21

def name
  @name
end

#queuesObject

Returns the value of attribute queues.



22
23
24
# File 'lib/sidekiq/capsule.rb', line 22

def queues
  @queues
end

#weightsObject (readonly)

Returns the value of attribute weights.



25
26
27
# File 'lib/sidekiq/capsule.rb', line 25

def weights
  @weights
end

Instance Method Details

#client_middleware {|@client_chain| ... } ⇒ Object

Allow the middleware to be different per-capsule. Avoid if possible and add middleware globally so all capsules share the same chains. Easier to debug that way.

Yields:

  • (@client_chain)


75
76
77
78
79
# File 'lib/sidekiq/capsule.rb', line 75

def client_middleware
  @client_chain ||= config.client_middleware.copy_for(self)
  yield @client_chain if block_given?
  @client_chain
end

#fetcherObject



36
37
38
39
40
41
42
# File 'lib/sidekiq/capsule.rb', line 36

def fetcher
  @fetcher ||= begin
    inst = (config[:fetch_class] || Sidekiq::BasicFetch).new(self)
    inst.setup(config[:fetch_setup]) if inst.respond_to?(:setup)
    inst
  end
end

#local_redis_poolObject



91
92
93
94
95
# File 'lib/sidekiq/capsule.rb', line 91

def local_redis_pool
  # connection pool is lazy, it will not create connections unless you actually need them
  # so don't be skimpy!
  @redis ||= config.new_redis_pool(@concurrency, name)
end

#loggerObject



123
124
125
# File 'lib/sidekiq/capsule.rb', line 123

def logger
  config.logger
end

#lookup(name) ⇒ Object



119
120
121
# File 'lib/sidekiq/capsule.rb', line 119

def lookup(name)
  config.lookup(name)
end

#redisObject

Raises:

  • (ArgumentError)


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/sidekiq/capsule.rb', line 97

def redis
  raise ArgumentError, "requires a block" unless block_given?
  redis_pool.with do |conn|
    retryable = true
    begin
      yield conn
    rescue RedisClientAdapter::BaseError => ex
      # 2550 Failover can cause the server to become a replica, need
      # to disconnect and reopen the socket to get back to the primary.
      # 4495 Use the same logic if we have a "Not enough replicas" error from the primary
      # 4985 Use the same logic when a blocking command is force-unblocked
      # The same retry logic is also used in client.rb
      if retryable && ex.message =~ /READONLY|NOREPLICAS|UNBLOCKED/
        conn.close
        retryable = false
        retry
      end
      raise
    end
  end
end

#redis_poolObject



87
88
89
# File 'lib/sidekiq/capsule.rb', line 87

def redis_pool
  Thread.current[:sidekiq_redis_pool] || local_redis_pool
end

#server_middleware {|@server_chain| ... } ⇒ Object

Yields:

  • (@server_chain)


81
82
83
84
85
# File 'lib/sidekiq/capsule.rb', line 81

def server_middleware
  @server_chain ||= config.server_middleware.copy_for(self)
  yield @server_chain if block_given?
  @server_chain
end

#stopObject



44
45
46
# File 'lib/sidekiq/capsule.rb', line 44

def stop
  fetcher&.bulk_requeue([])
end