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

.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.



150
151
152
153
154
155
156
157
158
159
# File 'app/workers/sidekiq/callbacks.rb', line 150

def assert_valid_config!
  Sidekiq::CallbacksWorker.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



142
143
144
# File 'app/workers/sidekiq/callbacks.rb', line 142

def async(*workers, &block)
  set_workers(workers, :async, &block)
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



74
75
76
# File 'app/workers/sidekiq/callbacks.rb', line 74

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



41
42
43
# File 'app/workers/sidekiq/callbacks.rb', line 41

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



109
110
111
# File 'app/workers/sidekiq/callbacks.rb', line 109

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

Instance Method Details

#run_callbacks(kind) ⇒ Object



191
192
193
194
195
# File 'app/workers/sidekiq/callbacks.rb', line 191

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