Module: Sidekiq

Defined in:
lib/sidekiq.rb,
lib/sidekiq/api.rb,
lib/sidekiq/cli.rb,
lib/sidekiq/web.rb,
lib/sidekiq/util.rb,
lib/sidekiq/actor.rb,
lib/sidekiq/fetch.rb,
lib/sidekiq/rails.rb,
lib/sidekiq/client.rb,
lib/sidekiq/worker.rb,
lib/sidekiq/logging.rb,
lib/sidekiq/manager.rb,
lib/sidekiq/testing.rb,
lib/sidekiq/version.rb,
lib/sidekiq/launcher.rb,
lib/sidekiq/paginator.rb,
lib/sidekiq/processor.rb,
lib/sidekiq/scheduled.rb,
lib/sidekiq/web_helpers.rb,
lib/sidekiq/middleware/chain.rb,
lib/sidekiq/redis_connection.rb,
lib/sidekiq/exception_handler.rb,
lib/sidekiq/extensions/action_mailer.rb,
lib/sidekiq/extensions/active_record.rb,
lib/sidekiq/extensions/class_methods.rb,
lib/sidekiq/extensions/generic_proxy.rb,
lib/sidekiq/middleware/server/logging.rb,
lib/generators/sidekiq/worker_generator.rb,
lib/sidekiq/middleware/server/retry_jobs.rb,
lib/sidekiq/middleware/server/active_record.rb

Defined Under Namespace

Modules: Actor, ExceptionHandler, Extensions, Generators, Logging, Middleware, Paginator, Scheduled, Util, WebHelpers, Worker Classes: BasicFetch, CLI, Client, DeadSet, EmptyQueueError, Fetcher, Job, JobSet, Launcher, Manager, Process, ProcessSet, Processor, Queue, Rails, RedisConnection, RetrySet, ScheduledSet, Shutdown, SortedEntry, SortedSet, Stats, Testing, Web, Workers

Constant Summary collapse

NAME =
"Sidekiq"
LICENSE =
'See LICENSE and the LGPL-3.0 for licensing details.'
DEFAULTS =
{
  :queues => [],
  :labels => [],
  :concurrency => 25,
  :require => '.',
  :environment => nil,
  :timeout => 8,
  :error_handlers => [],
  :lifecycle_events => {
    :startup => [],
    :quiet => [],
    :shutdown => [],
  }
}
VERSION =
"3.2.3"

Class Method Summary collapse

Class Method Details

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

Yields:

  • (@client_chain)

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

def self.client_middleware
  @client_chain ||= Middleware::Chain.new
  yield @client_chain if block_given?
  @client_chain
end

.configure_client {|_self| ... } ⇒ Object

Configuration for Sidekiq client, use like:

Sidekiq.configure_client do |config|
  config.redis = { :namespace => 'myapp', :size => 1, :url => 'redis://myhost:8877/0' }
end

Yields:

  • (_self)

Yield Parameters:

  • _self (Sidekiq)

    the object that the method was called on


62
63
64
# File 'lib/sidekiq.rb', line 62

def self.configure_client
  yield self unless server?
end

.configure_server {|_self| ... } ⇒ Object

Configuration for Sidekiq server, use like:

Sidekiq.configure_server do |config|
  config.redis = { :namespace => 'myapp', :size => 25, :url => 'redis://myhost:8877/0' }
  config.server_middleware do |chain|
    chain.add MyServerHook
  end
end

Yields:

  • (_self)

Yield Parameters:

  • _self (Sidekiq)

    the object that the method was called on


52
53
54
# File 'lib/sidekiq.rb', line 52

def self.configure_server
  yield self if server?
end

.default_worker_optionsObject


103
104
105
# File 'lib/sidekiq.rb', line 103

def self.default_worker_options
  defined?(@default_worker_options) ? @default_worker_options : { 'retry' => true, 'queue' => 'default' }
end

.default_worker_options=(hash) ⇒ Object


99
100
101
# File 'lib/sidekiq.rb', line 99

def self.default_worker_options=(hash)
  @default_worker_options = default_worker_options.merge(hash)
end

.dump_json(object) ⇒ Object


111
112
113
# File 'lib/sidekiq.rb', line 111

def self.dump_json(object)
  JSON.generate(object)
end

.error_handlersObject

Register a proc to handle any error which occurs within the Sidekiq process.

Sidekiq.configure_server do |config|
  config.error_handlers << Proc.new {|ex,ctx_hash| MyErrorService.notify(ex, ctx_hash) }
end

The default error handler logs errors to Sidekiq.logger.


135
136
137
# File 'lib/sidekiq.rb', line 135

def self.error_handlers
  self.options[:error_handlers]
end

.hook_rails!Object


2
3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/sidekiq/rails.rb', line 2

def self.hook_rails!
  return if defined?(@delay_removed)

  ActiveSupport.on_load(:active_record) do
    include Sidekiq::Extensions::ActiveRecord
  end

  ActiveSupport.on_load(:action_mailer) do
    extend Sidekiq::Extensions::ActionMailer
  end

  Module.__send__(:include, Sidekiq::Extensions::Klass)
end

.load_json(string) ⇒ Object


107
108
109
# File 'lib/sidekiq.rb', line 107

def self.load_json(string)
  JSON.parse(string)
end

.loggerObject


115
116
117
# File 'lib/sidekiq.rb', line 115

def self.logger
  Sidekiq::Logging.logger
end

.logger=(log) ⇒ Object


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

def self.logger=(log)
  Sidekiq::Logging.logger = log
end

.on(event, &block) ⇒ Object

Register a block to run at a point in the Sidekiq lifecycle. :startup, :quiet or :shutdown are valid events.

Sidekiq.configure_server do |config|
  config.on(:shutdown) do
    puts "Goodbye cruel world!"
  end
end

Raises:

  • (ArgumentError)

147
148
149
150
151
# File 'lib/sidekiq.rb', line 147

def self.on(event, &block)
  raise ArgumentError, "Symbols only please: #{event}" unless event.is_a?(Symbol)
  raise ArgumentError, "Invalid event name: #{event}" unless options[:lifecycle_events].keys.include?(event)
  options[:lifecycle_events][event] << block
end

.optionsObject


35
36
37
# File 'lib/sidekiq.rb', line 35

def self.options
  @options ||= DEFAULTS.dup
end

.options=(opts) ⇒ Object


39
40
41
# File 'lib/sidekiq.rb', line 39

def self.options=(opts)
  @options = opts
end

.poll_interval=(interval) ⇒ Object

See sidekiq/scheduled.rb for an in-depth explanation of this value


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

def self.poll_interval=(interval)
  self.options[:poll_interval] = interval
end

.redis(&block) ⇒ Object

Raises:

  • (ArgumentError)

70
71
72
73
# File 'lib/sidekiq.rb', line 70

def self.redis(&block)
  raise ArgumentError, "requires a block" unless block
  redis_pool.with(&block)
end

.redis=(hash) ⇒ Object


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

def self.redis=(hash)
  @redis = if hash.is_a?(ConnectionPool)
    hash
  else
    Sidekiq::RedisConnection.create(hash)
  end
end

.redis_poolObject


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

def self.redis_pool
  @redis ||= Sidekiq::RedisConnection.create
end

.remove_delay!Object

Removes the generic aliases which MAY clash with names of already

created methods by other applications. The methods `sidekiq_delay`,
`sidekiq_delay_for` and `sidekiq_delay_until` can be used instead.

19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/sidekiq/rails.rb', line 19

def self.remove_delay!
  @delay_removed = true

  [Extensions::ActiveRecord,
   Extensions::ActionMailer,
   Extensions::Klass].each do |mod|
    mod.module_eval do
      remove_method :delay if respond_to?(:delay)
      remove_method :delay_for if respond_to?(:delay_for)
      remove_method :delay_until if respond_to?(:delay_until)
    end
  end
end

.server?Boolean

Returns:

  • (Boolean)

66
67
68
# File 'lib/sidekiq.rb', line 66

def self.server?
  defined?(Sidekiq::CLI)
end

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

Yields:

  • (@server_chain)

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

def self.server_middleware
  @server_chain ||= Processor.default_middleware
  yield @server_chain if block_given?
  @server_chain
end

.❨╯°□°❩╯︵┻━┻Object


31
32
33
# File 'lib/sidekiq.rb', line 31

def self.❨╯°□°❩╯︵┻━┻
  puts "Calm down, bro"
end