Class: Wurk::Middleware::Chain::Entry

Inherits:
Object
  • Object
show all
Defined in:
lib/wurk/middleware/chain.rb

Overview

Holds the klass + ctor args for a registered middleware. Defers instantiation until retrieve/invoke runs so each job gets a fresh object.

One opt-in exception, for Wurk's own built-ins: a class that defines self.shared_instance? as true on itself is built once per entry, when the entry is bound to its config, and invoke hands every job that one instance. Only for a class whose #call writes no instance state — it runs on every processor thread of the capsule at once. Asked of the class itself, never inherited: a host's subclass of a built-in may keep state, and gets Sidekiq's fresh instance per job like any other middleware. make_new, and so retrieve, always builds a fresh one.

Whether the class declares config= is resolved once, at registration, so the common case (the setter comes from include Wurk::Middleware::ServerMiddleware) skips a respond_to? on every job × every entry. It is a fast path, not the rule: the contract is "set config if the instance is respondable" (spec §10.1), which also covers a setter defined on the singleton in initialize, one reached through method_missing, or one added to the class after it was registered — so a class that does not declare it still gets asked.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, *args) ⇒ Entry

Returns a new instance of Entry.



211
212
213
214
215
216
217
# File 'lib/wurk/middleware/chain.rb', line 211

def initialize(klass, *args)
  @klass = klass
  @args = args
  @takes_config = klass.public_method_defined?(:config=)
  @shared = klass.singleton_class.method_defined?(:shared_instance?, false) && klass.shared_instance?
  @instance = nil
end

Instance Attribute Details

#config ⇒ Object

Returns the value of attribute config.



209
210
211
# File 'lib/wurk/middleware/chain.rb', line 209

def config
  @config
end

#klass ⇒ Object (readonly)

Returns the value of attribute klass.



209
210
211
# File 'lib/wurk/middleware/chain.rb', line 209

def klass
  @klass
end

Instance Method Details

#instance ⇒ Object

What invoke runs: the shared instance, or a fresh one.



227
228
229
# File 'lib/wurk/middleware/chain.rb', line 227

def instance
  @instance || make_new
end

#make_new ⇒ Object



231
232
233
234
235
# File 'lib/wurk/middleware/chain.rb', line 231

def make_new
  instance = @klass.new(*@args)
  instance.config = config if @takes_config || instance.respond_to?(:config=)
  instance
end