Module: Sidekiq::Callbacks

Extended by:
ActiveSupport::Concern
Included in:
Workarea::ApplicationDocument
Defined in:
app/workers/sidekiq/callbacks.rb

Overview

A plugin for Sidekiq that enables automatic job enqueuing via Rails callback methods. Mix Sidekiq::Callbacks into your model to enable this feature on any class that defines run_callbacks.

Defined Under Namespace

Classes: InvalidConfiguration

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_worker(klass) ⇒ Object

Add a Class to the list of workers to check when running callbacks.

Parameters:

  • worker (Class)


25
26
27
28
29
30
31
# File 'app/workers/sidekiq/callbacks.rb', line 25

def add_worker(klass)
  if caching_classes?
    workers_list << klass
  elsif !workers_list.include?(klass.name)
    workers_list << klass.name
  end
end

.assert_valid_config!Object

This method is run on boot to ensure a valid configuration of callback workers. It will raise a Sidekiq::Callbacks::InvalidConfiguration if it finds a problem.



198
199
200
201
202
203
204
205
206
207
# File 'app/workers/sidekiq/callbacks.rb', line 198

def assert_valid_config!
  Sidekiq::Callbacks.workers.each do |worker|
    if (worker.enqueue_on.values.flatten & [:find, 'find']).any?
      raise(
        InvalidConfiguration,
        "For performance reasons, Sidekiq::Callbacks do not support the `find` callback."
      )
    end
  end
end

.async(worker, ...) ⇒ Object

Permanently or temporarily inline callback workers. If no workers are given, it will inline all callback workers during the execution of the block or globally if no block is given. If a block and workers are given, workers provided will only be async during the execution of the block.

Examples:

permanently inline all workers

Sidekiq::Callbacks.inline

temporarily async all workers

Sidekiq::Callbacks.async do
  Content.create!(name: 'Foo')
end

permanently async specific workers

Sidekiq::Callbacks.async(IndexProductBrowse, IndexContent)

temporarily async specific workers

Sidekiq::Callbacks.async(IndexContent) do
  Content.create!(name: 'Foo')
end

Parameters:

  • worker (Object)

    A worker to set async

  • ... (Object)

    Any number of workers to set async

Yields:

  • code to be executed during temporarily asyncing of workers

Returns:

  • nil or result of block if provided



190
191
192
# File 'app/workers/sidekiq/callbacks.rb', line 190

def async(*workers, &block)
  set_workers(workers, :async, &block)
end

.caching_classes?Boolean

Whether Rails is caching classes, which matters when checking workers to run by comparing the worker’s configuration with the model running the callback.

When we aren’t caching classes, we need to use the fully qualified constant name to decide since the classes could have been reloaded due to code changes.

Returns:

  • (Boolean)


43
44
45
# File 'app/workers/sidekiq/callbacks.rb', line 43

def caching_classes?
  ::Rails.application.config.cache_classes
end

.disable(worker, ...) ⇒ Object

Permanently or temporarily disable callback workers. If no workers are given, it will disable all callback workers during the execution of the block or globally if no block is given. If a block and workers are given, workers provided will only be disabled during the execution of the block.

Examples:

permanently disable all workers

Sidekiq::Callbacks.disable

temporarily disable all workers

Sidekiq::Callbacks.disable do
  Content.create!(name: 'Foo')
end

permanently disable specific workers

Sidekiq::Callbacks.disable(IndexProductBrowse, IndexContent)

temporarily disable specific workers

Sidekiq::Callbacks.disable(IndexContent) do
  Content.create!(name: 'Foo')
end

Parameters:

  • worker (Object)

    A worker to disable

  • ... (Object)

    Any number of workers to disable

Yields:

  • code to be executed during temporarily disabling of workers

Returns:

  • nil or result of block if provided



122
123
124
# File 'app/workers/sidekiq/callbacks.rb', line 122

def disable(*workers, &block)
  set_workers(workers, :disable, &block)
end

.enable(worker, ...) ⇒ Object

Permanently or temporarily enable callback workers. If no workers are given, it will enable all callback workers during the execution of the block or globally if no block is given. If a block and workers are given, workers provided will only be enable during the execution of the block. Callback workers already enabled will continue to be enabled during block execution.

Examples:

permanently enable all workers

Sidekiq::Callbacks.enable

temporarily enable all workers

Sidekiq::Callbacks.enable do
  Content.create!(name: 'Foo')
end

permanently enable specific workers

Sidekiq::Callbacks.enable(IndexProductBrowse, IndexContent)

temporarily enable specific workers

Sidekiq::Callbacks.enable(IndexContent) do
  Content.create!(name: 'Foo')
end

Parameters:

  • worker (Object)

    A worker to enable

  • ... (Object)

    Any number of workers to enable

Yields:

  • code to be executed during temporarily enabling of workers

Returns:

  • nil or result of block if provided



89
90
91
# File 'app/workers/sidekiq/callbacks.rb', line 89

def enable(*workers, &block)
  set_workers(workers, :enable, &block)
end

.inline(worker, ...) ⇒ Object

Permanently or temporarily inline callback workers. If no workers are given, it will inline all callback workers during the execution of the block or globally if no block is given. If a block and workers are given, workers provided will only be inline during the execution of the block. Callback workers already inlined will continue to be inlined during block execution.

Examples:

permanently inline all workers

Sidekiq::Callbacks.inline

temporarily inline all workers

Sidekiq::Callbacks.inline do
  Content.create!(name: 'Foo')
end

permanently inline specific workers

Sidekiq::Callbacks.inline(IndexProductBrowse, IndexContent)

temporarily inline specific workers

Sidekiq::Callbacks.inline(IndexContent) do
  Content.create!(name: 'Foo')
end

Parameters:

  • worker (Object)

    A worker to inline

  • ... (Object)

    Any number of workers to inline

Yields:

  • code to be executed during temporarily enabling of workers

Returns:

  • nil or result of block if provided



157
158
159
# File 'app/workers/sidekiq/callbacks.rb', line 157

def inline(*workers, &block)
  set_workers(workers, :inline, &block)
end

.workersArray<Class>

The list of workers that perform Sidekiq callbacks. Used for checking whether one of them need to be fired off after a callback happens.

Returns:

  • (Array<Class>)


16
17
18
# File 'app/workers/sidekiq/callbacks.rb', line 16

def workers
  caching_classes? ? workers_list : workers_list.map(&:constantize)
end

.workers_listArray<String,Class>

Convenience reference to the tracked list of workers in the Rails config

Returns:



52
53
54
55
56
# File 'app/workers/sidekiq/callbacks.rb', line 52

def workers_list
  config = ::Rails.application.config
  config.sidekiq_callbacks_workers = [] unless config.respond_to?(:sidekiq_callbacks_workers)
  config.sidekiq_callbacks_workers
end

Instance Method Details

#run_callbacks(kind) ⇒ Object



239
240
241
242
243
# File 'app/workers/sidekiq/callbacks.rb', line 239

def run_callbacks(kind, *)
  result = super
  _enqueue_callback_workers(kind) if result != false && kind != :find
  result
end