Class: Wurk::Loader
- Inherits:
-
Object
- Object
- Wurk::Loader
- Defined in:
- lib/wurk/loader.rb
Overview
Deferred extension hooks, Sidekiq.loader (spec ยง30). An extension
registers Sidekiq.loader.on_load(:api) { ... } to patch a component once
it has loaded; registering after the component already loaded runs the
block at once. Wurk loads its whole API with require "wurk", so :api
fires as soon as the alias layer is in place (end of compat.rb).
Instance Method Summary collapse
-
#initialize ⇒ Loader
constructor
A new instance of Loader.
-
#on_load(name, &block) ⇒ Object
The block runs outside the lock so it may itself register hooks.
-
#run_load_hooks(name) ⇒ Object
Marks
nameloaded and runs its pending hooks once.
Constructor Details
#initialize ⇒ Loader
Returns a new instance of Loader.
10 11 12 13 14 |
# File 'lib/wurk/loader.rb', line 10 def initialize @load_hooks = Hash.new { |h, k| h[k] = [] } @loaded = ::Set.new @lock = ::Mutex.new end |
Instance Method Details
#on_load(name, &block) ⇒ Object
The block runs outside the lock so it may itself register hooks.
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/wurk/loader.rb', line 17 def on_load(name, &block) run_now = @lock.synchronize do next true if @loaded.include?(name) @load_hooks[name] << block false end block.call if run_now nil end |
#run_load_hooks(name) ⇒ Object
Marks name loaded and runs its pending hooks once. A failing hook is
reported and the rest still run.
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/wurk/loader.rb', line 30 def run_load_hooks(name) hooks = @lock.synchronize do @loaded << name @load_hooks.delete(name) end hooks&.each do |hook| hook.call rescue StandardError => e Wurk.configuration.handle_exception(e, { hook: name }) end nil end |