-
-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace Performer with JobPerformer to clearly delay references to Go…
…odJob::Job Necessary because there is a circular dependency in Rails 6.1 when ActiveJob initializes GoodJob, which references ActiveRecord, which tries to initialize ActiveJob (for the destroy associations async feature)
- Loading branch information
1 parent
13a059e
commit b89f186
Showing
9 changed files
with
114 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
require 'concurrent/delay' | ||
|
||
module GoodJob | ||
# | ||
# JobPerformer 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 JobPerformer must be safe to execute across multiple threads. | ||
# | ||
class JobPerformer | ||
# @param queue_string [String] Queues to execute jobs from | ||
def initialize(queue_string) | ||
@queue_string = queue_string | ||
|
||
@job_query = Concurrent::Delay.new { GoodJob::Job.queue_string(queue_string) } | ||
@parsed_queues = Concurrent::Delay.new { GoodJob::Job.queue_parser(queue_string) } | ||
end | ||
|
||
# A meaningful name to identify the performer in logs and for debugging. | ||
# @return [String] The queues from which Jobs are worked | ||
def name | ||
@queue_string | ||
end | ||
|
||
# Perform the next eligible job | ||
# @return [nil, Object] Returns job result or +nil+ if no job was found | ||
def next | ||
job_query.perform_with_advisory_lock | ||
end | ||
|
||
# Tests whether this performer should be used in GoodJob's current state. | ||
# | ||
# 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. | ||
# | ||
# @return [Boolean] whether the performer's {#next} method should be | ||
# called in the current state. | ||
def next?(state = {}) | ||
if parsed_queues[:exclude] | ||
parsed_queues[:exclude].exclude?(state[:queue_name]) | ||
elsif parsed_queues[:include] | ||
parsed_queues[:include].include?(state[:queue_name]) | ||
else | ||
true | ||
end | ||
end | ||
|
||
private | ||
|
||
attr_reader :queue_string | ||
|
||
def job_query | ||
@job_query.value | ||
end | ||
|
||
def parsed_queues | ||
@parsed_queues.value | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe GoodJob::JobPerformer do | ||
describe '#name' do | ||
it 'displays the queues' do | ||
queue_string = 'mice, elephants' | ||
job_performer = described_class.new(queue_string) | ||
expect(job_performer.name).to eq queue_string | ||
end | ||
end | ||
|
||
describe '#next' do | ||
it 'calls GoodJob.perform_with_advisory_lock' do | ||
allow(GoodJob::Job).to receive(:perform_with_advisory_lock) | ||
|
||
job_performer = described_class.new('*') | ||
job_performer.next | ||
|
||
expect(GoodJob::Job).to have_received(:perform_with_advisory_lock) | ||
end | ||
end | ||
|
||
describe 'next?' do | ||
it 'filters on queue name' do | ||
state = { queue_name: 'elephants' } | ||
|
||
result = described_class.new('*').next?(state) | ||
expect(result).to eq true | ||
|
||
result = described_class.new('elephants').next?(state) | ||
expect(result).to eq true | ||
|
||
result = described_class.new('-elephants').next?(state) | ||
expect(result).to eq false | ||
|
||
result = described_class.new('mice').next?(state) | ||
expect(result).to eq false | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters