-
Notifications
You must be signed in to change notification settings - Fork 21.5k
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
ActiveJob: queue_adapter can be inherited #16992
Conversation
a91699a
to
3092006
Compare
b0f3a44
to
4275f40
Compare
4275f40
to
208c9b9
Compare
performed_jobs << {job: job.class, args: job.arguments, queue: job.queue_name} | ||
job.perform_now | ||
else | ||
enqueued_jobs << {job: job.class, args: job.arguments, queue: job.queue_name} |
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 have 4 lines like this. Maybe let's return it from a method like job_hash(job)
?
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.
Actually even more natural would be job.to_hash
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.
done
208c9b9
to
22118ce
Compare
The test adapter was made like this so we can run tests in parallel. @seuros you worked on this |
require 'active_support/core_ext/string/inflections' | ||
|
||
module ActiveJob | ||
module QueueAdapter | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
class_attribute :_queue_adapter, instance_accessor: false, instance_predicate: false |
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.
Please rename _queue_adapter
to something else _queue_adapter_class
or even queue_adapter_class
to be more clear
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.
It's not always a class, though. It may be a callable. I'll change the name to reflect that.
Here's the PR when we switch the test adapter to instance methods #16724 (diff) |
22118ce
to
1d5b389
Compare
@@ -7,6 +7,7 @@ | |||
|
|||
class EnqueuedJobsTest < ActiveJob::TestCase | |||
setup { queue_adapter.perform_enqueued_at_jobs = true } | |||
teardown { queue_adapter.perform_enqueued_at_jobs = false } |
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 don't need these teardowns. The test adapter sould to allow parallel test instantiated for every test
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.
Not anymore, it's a singleton now. See existing discussion in this PR.
d933fb3
to
6f851ec
Compare
Ok. So ... as discussed the ability to pass a proc to queue_adapter is still under discussion so I suggest to leave this PR with the initial scope of being able to override sub-jobs adapters and start a new PR with the proc thing. |
Also you need to rebase before we're ready to merge |
I meant to rebase so you can squash your commits |
Then you should split them into multiple PRs. |
No need to squash or split it into multiple PRs. Looks good to me 👍 |
I'd keep |
I was erroneously ccd. the mail should have gone to @tamird i think. On 08 Oct 2014, at 09:02, Cristian Bica notifications@github.com wrote:
|
6f851ec
to
c80f403
Compare
test_adapter = ActiveJob::QueueAdapters::TestAdapter.new | ||
|
||
@old_queue_adapters = (ActiveJob::Base.subclasses << ActiveJob::Base).select do |klass| | ||
klass.respond_to?(:queue_adapter) |
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.
Every ActiveJob::Base
subclass will respond to :queue_adapter
since it's a singleton method on Base
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.
derp, you're right. fixed
9515e8a
to
cbaa77d
Compare
[klass, klass.queue_adapter].tap do | ||
klass.queue_adapter = test_adapter | ||
end | ||
end |
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 class attributes no longer inherit from superclass once they're written, assigning #queue_adapter
on classes the didn't have it explicitly set will leak side effects between test cases (since changing a superclass attr won't be inherited by its subclass afterward)
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.
great catch, fixed
fabae1a
to
2a30528
Compare
# only override explicitly set adapters, a quirk of `class_attribute` | ||
klass.singleton_class.public_instance_methods(false).include?(:_queue_adapter) | ||
end.map do |klass| | ||
[klass, klass._queue_adapter].tap do |
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.
Prefer to use the public-API reader?
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.
fixed
This allows different `queue_adapters` to be used in each `ActiveJob` class heirarchy. Previously, all subclasses used a single global queue adapter.
2a30528
to
1f8558f
Compare
ActiveJob: queue_adapter can be inherited
❤️ took a few months to get this merged :) |
👍 |
I'm not seeing this feature being mentioned in the Basics guide. Or we have an Advanced guide somewhere that I don't know :)? I think this is cool and deserves a mention. Should I submit a PR or has anyone worked on this already? |
👍 |
Is code merged in such that it's possible to have different queue adapters for different jobs? (and if so beginning in what Rails version) I got here following a trail of links between issues and PRs here and in ActiveJob. And the disussion and statuses here make me think this is now possible. But I have no idea how to do it! If I'm on the right track, could anyone post an example here? If I get an example, I will try to play with it and understand it and do a doc PR demo'ing it in the right place. (If it's already in the docs but I just haven't found it, would very much appreciate if you wanted to point me there!). |
Nevermind, just found it in the guide! "You can also configure your backend on a per job basis." Okay! Great! |
does anyone have a "patch" i can use in my rails 4.2 app to get this to work? |
Rails 5+ allows to set queue adapter per job class rails/rails#16992
Rails 5+ allows to set queue adapter per job class rails/rails#16992
Rails 5+ allows to set queue adapter per job class rails/rails#16992
@cristianbica this is #16968