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
-
.add_worker(klass) ⇒ Object
Add a Class to the list of workers to check when running callbacks.
-
.assert_valid_config! ⇒ Object
This method is run on boot to ensure a valid configuration of callback workers.
-
.async(worker, ...) ⇒ Object
Permanently or temporarily inline callback workers.
-
.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.
-
.disable(worker, ...) ⇒ Object
Permanently or temporarily disable callback workers.
-
.enable(worker, ...) ⇒ Object
Permanently or temporarily enable callback workers.
-
.inline(worker, ...) ⇒ Object
Permanently or temporarily inline callback workers.
-
.workers ⇒ Array<Class>
The list of workers that perform Sidekiq callbacks.
-
.workers_list ⇒ Array<String,Class>
Convenience reference to the tracked list of workers in the Rails config.
Instance Method Summary collapse
Class Method Details
.add_worker(klass) ⇒ Object
Add a Class to the list of workers to check when running callbacks.
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.
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.
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.
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.
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.
157 158 159 |
# File 'app/workers/sidekiq/callbacks.rb', line 157 def inline(*workers, &block) set_workers(workers, :inline, &block) end |
.workers ⇒ Array<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.
16 17 18 |
# File 'app/workers/sidekiq/callbacks.rb', line 16 def workers caching_classes? ? workers_list : workers_list.map(&:constantize) end |
.workers_list ⇒ Array<String,Class>
Convenience reference to the tracked list of workers in the Rails config
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 |