Class: Sidekiq::Capsule

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
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.



30
31
32
33
34
35
36
37
# File 'lib/sidekiq/capsule.rb', line 30

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.



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

def concurrency
  @concurrency
end

#modeObject (readonly)

Returns the value of attribute mode.



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

def mode
  @mode
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#queuesObject

Returns the value of attribute queues.



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

def queues
  @queues
end

#weightsObject (readonly)

Returns the value of attribute weights.



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

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)


78
79
80
81
82
# File 'lib/sidekiq/capsule.rb', line 78

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

#fetcherObject



39
40
41
42
43
44
45
# File 'lib/sidekiq/capsule.rb', line 39

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



94
95
96
97
98
# File 'lib/sidekiq/capsule.rb', line 94

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



126
127
128
# File 'lib/sidekiq/capsule.rb', line 126

def logger
  config.logger
end

#lookup(name) ⇒ Object



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

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

#redisObject

Raises:

  • (ArgumentError)


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

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



90
91
92
# File 'lib/sidekiq/capsule.rb', line 90

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

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

Yields:

  • (@server_chain)


84
85
86
87
88
# File 'lib/sidekiq/capsule.rb', line 84

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

#stopObject



47
48
49
# File 'lib/sidekiq/capsule.rb', line 47

def stop
  fetcher&.bulk_requeue([])
end