Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add first cut at Performer docs #112

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions lib/good_job/performer.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,46 @@
module GoodJob
#
# Performer queries the database for jobs and performs them on behalf of a
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the applied purpose for the Perfomer. Though the performer is intended to be generic/ignorant of the nature of the task.

The practical reason for the performer is to enforce a callable that does not rely on scoped/closures variables because they might not be available when executed in a different thread. Or alternatively, it describes an interface that is compatible with the Scheduler.

# {Scheduler}. It mainly functions as glue between a {Scheduler} and the jobs
# it should be executing.
#
class Performer
# @!attribute [r] name
# @return [String]
# 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"+.
attr_reader :name

# @param target [Object]
# An object that can perform jobs. It must respond to +method_name+ by
# finding and performing jobs and is usually a {Job} query,
# e.g. +GoodJob::Job.where(queue_name: ['queue1', 'queue2'])+.
# @param method_name [Symbol]
# The name of a method on +target+ that finds and performs jobs.
# @param name [String]
# A name for the performer to be used in logs and for debugging.
# @param filter [#call]
# Used to determine whether the performer should be used in GoodJob's
# current state. GoodJob state is a +Hash+ that will be passed as the
# first argument to +filter+ and includes info like the current queue.
def initialize(target, method_name, name: nil, filter: nil)
@target = target
@method_name = method_name
@name = name
@filter = filter
end

# Find and perform any eligible jobs.
def next
@target.public_send(@method_name)
end

# Tests whether this performer should be used in GoodJob's current state by
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"State" is the generic. Practically, the state is a LISTEN/NOTIFY message that is passed down from the Notifier to the Scheduler, and the Scheduler then is able to ask the Performer "does this message relate to you?" and allow the Performer to say "no, don't bother waking up thread".

# calling the +filter+ callable set in {#initialize}. Always returns +true+
# if there is no filter.
# @return [Boolean] whether the performer's {#next} method should be
# called in the current state.
def next?(state = {})
return true unless @filter.respond_to?(:call)

Expand Down