Class: GoodJob::Performer
- Inherits:
-
Object
- Object
- GoodJob::Performer
- Defined in:
- lib/good_job/performer.rb
Overview
Performer queries the database for jobs and performs them on behalf of a Scheduler. It mainly functions as glue between a Scheduler and the jobs it should be executing.
The Performer enforces a callable that does not rely on scoped/closure variables because they might not be available when executed in a different thread.
Instance Attribute Summary collapse
-
#name ⇒ String
readonly
A meaningful name to identify the performer in logs and for debugging.
Instance Method Summary collapse
-
#initialize(target, method_name, name: nil, filter: nil) ⇒ Performer
constructor
A new instance of Performer.
-
#next ⇒ Object
Find and perform any eligible jobs.
-
#next?(state = {}) ⇒ Boolean
Tests whether this performer should be used in GoodJob’s current state by calling the
filter
callable set in #initialize.
Constructor Details
#initialize(target, method_name, name: nil, filter: nil) ⇒ Performer
Returns a new instance of Performer.
31 32 33 34 35 36 |
# File 'lib/good_job/performer.rb', line 31 def initialize(target, method_name, name: nil, filter: nil) @target = target @method_name = method_name @name = name @filter = filter end |
Instance Attribute Details
#name ⇒ String (readonly)
Returns a meaningful name to identify the performer in logs and for debugging. This is usually set to the list of queues the performer will query, e.g. “-transactional_messages,batch_processing”.
17 18 19 |
# File 'lib/good_job/performer.rb', line 17 def name @name end |
Instance Method Details
#next ⇒ Object
Find and perform any eligible jobs.
39 40 41 |
# File 'lib/good_job/performer.rb', line 39 def next @target.public_send(@method_name) end |
#next?(state = {}) ⇒ Boolean
Tests whether this performer should be used in GoodJob’s current state by calling the filter
callable set in #initialize. Always returns true
if there is no filter.
For example, state will be a LISTEN/NOTIFY message that is passed down from the Notifier to the Scheduler. The Scheduler is able to ask its performer “does this message relate to you?”, and if not, ignore it to minimize thread wake-ups, database queries, and thundering herds.
54 55 56 57 58 |
# File 'lib/good_job/performer.rb', line 54 def next?(state = {}) return true unless @filter.respond_to?(:call) @filter.call(state) end |