Class: Sidekiq::Loader
- Inherits:
-
Object
- Object
- Sidekiq::Loader
- Includes:
- Component
- Defined in:
- lib/sidekiq/loader.rb
Instance Attribute Summary
Attributes included from Component
Instance Method Summary collapse
-
#initialize(cfg = Sidekiq.default_configuration) ⇒ Loader
constructor
A new instance of Loader.
-
#on_load(name, &block) ⇒ Object
Declares a block that will be executed when a Sidekiq component is fully loaded.
-
#run_load_hooks(name) ⇒ Object
Executes all blocks registered to
namevia on_load.
Methods included from Component
#default_tag, #fire_event, #handle_exception, #hostname, #identity, #inspect, #logger, #mono_ms, #process_nonce, #real_ms, #redis, #safe_thread, #tid, #watchdog
Constructor Details
#initialize(cfg = Sidekiq.default_configuration) ⇒ Loader
Returns a new instance of Loader.
7 8 9 10 11 12 |
# File 'lib/sidekiq/loader.rb', line 7 def initialize(cfg = Sidekiq.default_configuration) @config = cfg @load_hooks = Hash.new { |h, k| h[k] = [] } @loaded = Set.new @lock = Mutex.new end |
Instance Method Details
#on_load(name, &block) ⇒ Object
Declares a block that will be executed when a Sidekiq component is fully loaded. If the component has already loaded, the block is executed immediately.
Sidekiq.loader.on_load(:api) do
# extend the sidekiq API
end
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/sidekiq/loader.rb', line 22 def on_load(name, &block) # we don't want to hold the lock while calling the block to_run = nil @lock.synchronize do if @loaded.include?(name) to_run = block else @load_hooks[name] << block end end to_run&.call nil end |
#run_load_hooks(name) ⇒ Object
Executes all blocks registered to name via on_load.
Sidekiq.loader.run_load_hooks(:api)
In the case of the above example, it will execute all hooks registered for :api.
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/sidekiq/loader.rb', line 44 def run_load_hooks(name) hks = @lock.synchronize do @loaded << name @load_hooks.delete(name) end hks&.each do |blk| blk.call rescue => ex handle_exception(ex, hook: name) end end |