forked from tobi/delayed_job
-
Notifications
You must be signed in to change notification settings - Fork 954
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
Move ActiveJob
adapter to delayed_job
#1219
Open
dixpac
wants to merge
7
commits into
collectiveidea:master
Choose a base branch
from
dixpac:dix/add_active_job_adapter
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d71a189
Add `ActiveJob` adapter for `delayed_job`
dixpac 0c582ba
Use local adapter
dixpac 4fa9bef
Start writing tests
dixpac b86b3ee
More testing
dixpac 631868d
Test scheduled jobs
dixpac 41f2b77
More testing cases
dixpac 1f3b958
Fix loading adapters with `const_defined?`
dixpac File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,61 @@ | ||
module ActiveJob | ||
module QueueAdapters | ||
# Explicitly remove the implementation existing in older rails'. | ||
remove_const(:DelayedJobAdapter) if defined?(:DelayedJobAdapter) | ||
dixpac marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# = Delayed Job adapter for Active Job | ||
# | ||
# To use Delayed Job, set the queue_adapter config to +:delayed_job+. | ||
# | ||
# Rails.application.config.active_job.queue_adapter = :delayed_job | ||
class DelayedJobAdapter < ::ActiveJob::QueueAdapters::AbstractAdapter | ||
def initialize(enqueue_after_transaction_commit: false) | ||
@enqueue_after_transaction_commit = enqueue_after_transaction_commit | ||
end | ||
|
||
def enqueue_after_transaction_commit? # :nodoc: | ||
@enqueue_after_transaction_commit | ||
end | ||
|
||
def enqueue(job) | ||
delayed_job = Delayed::Job.enqueue(JobWrapper.new(job.serialize), queue: job.queue_name, priority: job.priority) | ||
job.provider_job_id = delayed_job.id | ||
delayed_job | ||
end | ||
|
||
def enqueue_at(job, timestamp) | ||
delayed_job = Delayed::Job.enqueue(JobWrapper.new(job.serialize), queue: job.queue_name, priority: job.priority, run_at: Time.at(timestamp)) | ||
job.provider_job_id = delayed_job.id | ||
delayed_job | ||
end | ||
|
||
class JobWrapper | ||
attr_accessor :job_data | ||
|
||
def initialize(job_data) | ||
@job_data = job_data | ||
end | ||
|
||
def display_name | ||
base_name = "#{job_data["job_class"]} [#{job_data["job_id"]}] from DelayedJob(#{job_data["queue_name"]})" | ||
|
||
return base_name unless log_arguments? | ||
|
||
"#{base_name} with arguments: #{job_data["arguments"]}" | ||
end | ||
|
||
def perform | ||
Base.execute(job_data) | ||
end | ||
|
||
private | ||
def log_arguments? | ||
job_data["job_class"].constantize.log_arguments? | ||
rescue NameError | ||
false | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
require 'delayed_job' | ||
require 'rails' | ||
require 'active_job/queue_adapters/delayed_job_adapter' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should probably be in |
||
|
||
module Delayed | ||
class Railtie < Rails::Railtie | ||
|
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,121 @@ | ||
require 'helper' | ||
require 'active_job' | ||
require 'concurrent' | ||
|
||
describe 'a Rails active job backend' do | ||
module JobBuffer | ||
@values = Concurrent::Array.new | ||
|
||
class << self | ||
def clear | ||
@values.clear | ||
end | ||
|
||
def add(value) | ||
@values << value | ||
end | ||
|
||
def values | ||
@values.dup | ||
end | ||
end | ||
end | ||
|
||
class TestJob < ActiveJob::Base | ||
queue_as :integration_tests | ||
|
||
def perform(message) | ||
JobBuffer.add(message) | ||
end | ||
end | ||
|
||
let(:worker) { Delayed::Worker.new(sleep_delay: 0.5, queues: %w[integration_tests]) } | ||
|
||
before do | ||
JobBuffer.clear | ||
Delayed::Job.delete_all | ||
ActiveJob::Base.queue_adapter = :delayed_job | ||
ActiveJob::Base.logger = nil | ||
end | ||
|
||
it "should supply a wrapped class name to DelayedJob" do | ||
TestJob.perform_later | ||
job = Delayed::Job.all.last | ||
expect(job.name).to match(/TestJob \[[0-9a-f-]+\] from DelayedJob\(integration_tests\) with arguments: \[\]/) | ||
end | ||
|
||
it 'enqueus and executes the job' do | ||
start_worker do | ||
TestJob.perform_later('Rails') | ||
sleep 2 | ||
expect(JobBuffer.values).to eq(['Rails']) | ||
end | ||
end | ||
|
||
it "should not run jobs queued on a non-listening queue" do | ||
start_worker do | ||
old_queue = TestJob.queue_name | ||
|
||
begin | ||
TestJob.queue_as :some_other_queue | ||
TestJob.perform_later "Rails" | ||
sleep 2 | ||
expect(JobBuffer.values.empty?).to eq true | ||
ensure | ||
TestJob.queue_name = old_queue | ||
end | ||
end | ||
end | ||
|
||
it 'runs multiple queued jobs' do | ||
start_worker do | ||
ActiveJob.perform_all_later(TestJob.new('Rails'), TestJob.new('World')) | ||
sleep 2 | ||
expect(JobBuffer.values).to eq(['Rails', 'World']) | ||
end | ||
end | ||
|
||
it 'should not run job enqueued in the future' do | ||
start_worker do | ||
TestJob.set(wait: 5.seconds).perform_later('Rails') | ||
sleep 2 | ||
expect(JobBuffer.values.empty?).to eq true | ||
end | ||
end | ||
|
||
it 'should run job enqueued in the future at the specified time' do | ||
start_worker do | ||
TestJob.set(wait: 5.seconds).perform_later('Rails') | ||
sleep 10 | ||
expect(JobBuffer.values).to eq(['Rails']) | ||
end | ||
end | ||
|
||
it "should run job bulk enqueued in the future at the specified time" do | ||
start_worker do | ||
ActiveJob.perform_all_later([TestJob.new("Rails").set(wait: 5.seconds)]) | ||
sleep 10 | ||
expect(JobBuffer.values).to eq(['Rails']) | ||
end | ||
end | ||
|
||
it "should run job with higher priority first" do | ||
start_worker do | ||
wait_until = Time.now + 3.seconds | ||
TestJob.set(wait_until: wait_until, priority: 20).perform_later "1" | ||
TestJob.set(wait_until: wait_until, priority: 10).perform_later "2" | ||
sleep 10 | ||
|
||
expect(JobBuffer.values).to eq(['2', '1']) | ||
end | ||
end | ||
|
||
private | ||
def start_worker(&) | ||
thread = Thread.new { worker.start } | ||
yield | ||
ensure | ||
worker.stop | ||
thread.join | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the adapter signature has changed over time, I think this file needs to be the fallback for when it is not present in ActiveJob.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can load this adapter only if
ActiveJob.version > Gem::Version.new("8.0")
.Afaik plan is to "deprecate" adapter in rails for 8.1 and remove it in later Rails versions in favour of the adapter inside the
dj
. Wdyt?