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

Queue priority in config #845

Merged
merged 1 commit into from
Nov 16, 2015

Conversation

ryannealmes
Copy link

Queue priority is not settable via a config file. This causes complications when you want to set queue priority and leverage ActiveJob in Rails. In order to allow setting queue priority I have created a queue_attributes attribute that falls off the worker. The attribute is then accessed and the priority is set when setting up the job prior to enqueuing.

I have extracted the enqueueing code into it's own class to try and clean things up a little into a JobPreparer for a lack of a better name.

Some side notes it may be better to rather use the queue attribute on the worker, but I thought this was a good start.

This is my first public PR to someone else's repo. I have read the guidelines ... I am happy if it's rejected, but input on where I can improve in future or how I can change things to provide similar functionality would be much appreciated.

Someone also started a thread requesting this functionality.

@sferik
Copy link
Collaborator

sferik commented Sep 22, 2015

Can you please rebase from master and fix the outstanding RuboCop style guide violations in your code?

@ryannealmes
Copy link
Author

I have rebased from master ... I hope I have done it correctly. Let me know if there is anything I need to fix. There were also a couple of rubocop issues that weren't mine which I included as well.

@sferik
Copy link
Collaborator

sferik commented Sep 25, 2015

I hope I have done it correctly.

@ryannealmes This doesn’t look quite right. Try this in your fork:

git remote add upstream git@github.com:collectiveidea/delayed_job.git
git checkout master
git pull upstream master
git checkout queue-priority-in-config
git rebase master
# after each conflict is resolved resolved:
git add .
git rebase --continue
# after all conflicts are resolved:
git push --force origin queue-priority-in-config

Also, ideally all of the changes in your branch would be squashed into a single commit. Let me know if you need instructions on how to do that.

@ryannealmes ryannealmes force-pushed the queue-priority-in-config branch 2 times, most recently from d43cbcc to c3cc9da Compare September 25, 2015 18:00
@ryannealmes
Copy link
Author

Cool, I think I am there. Let me know what you think. Thanks for the help.

@ryannealmes
Copy link
Author

Hey @sferik, is there anything I need to do to get this merged? I know guys are probably pretty busy with other stuff, but I am not really sure if I should just wait until someone says something or if I should actively be chatting to someone. Thanks.

@svoop
Copy link

svoop commented Nov 11, 2015

@ryannealmes Thanks for this PR, this is really a crucial bit! (I have to up an app to Rails 4.2 now and without this feature, I'll be forced to switch to another backend.)

@sferik Any chance this will be merged soonish?

@@ -4,7 +4,7 @@ Gem::Specification.new do |spec|
spec.description = 'Delayed_job (or DJ) encapsulates the common pattern of asynchronously executing longer tasks in the background. It is a direct extraction from Shopify where the job table is responsible for a multitude of core tasks.'
spec.email = ['brian@collectiveidea.com']
spec.files = %w[CHANGELOG.md CONTRIBUTING.md LICENSE.md README.md Rakefile delayed_job.gemspec]
spec.files += Dir.glob('{contrib,lib,recipes,spec}/**/*')
spec.files += Dir.glob('{contrib,lib,recipes,spec}/**/*') # rubocop:disable SpaceAroundOperators</code>
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should this have </code> at the end?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Also, just one space after rubocop:disable.

@sferik
Copy link
Collaborator

sferik commented Nov 11, 2015

Sorry for the long delay on merging this pull request. Other than the few minor questions/suggestions I just made related to RuboCop, this looks good to me.

I’d like to merge this and release it at the same time as Rails 5 compatibility. Of course, Rails 5 is not yet released but I’m hoping it will come later this year or early 2016.

In the mean time, after this is merged, you’ll be able to use it by specifying a Git source in your Gemfile:

gem 'delayed_job', :git => 'https://github.com/collectiveidea/delayed_job.git'

@svoop
Copy link

svoop commented Nov 14, 2015

@sferik @ryannealmes

There might still be a problem lurking. With the version from the queue-priority-in-config branch of ryannealmes/delayed_job, I experience the following behaviour:

# initializers/delayed_job.rb
Delayed::Worker.queue_attributes = [
  { name: :low_priority, priority: 10 }
]

"test".delay(queue: :low_priority).to_s
job = Delayed::Job.last
job.queue      # => "low_priority"   <-- OKAY
job.priority   # => 10               <-- OKAY

UserMailer.password_reset(user).deliver_later(queue: :low_priority)
job = Delayed::Job.last
job.queue      # => "low_priority"   <-- OKAY
job.priority   # => 0                <-- FAIL

@svoop
Copy link

svoop commented Nov 14, 2015

@sferik @ryannealmes

I found the cause for the problem mentioned in my previous comment. While the ActiveJob documentation uses symbols for queue names, the actual queue name is converted to a string internally. Therefore, when setting default priorities per queue, you must currently use a string to reference the queue. Now, I won't be the only one tripping over this 😄

I suggest adding a to_s to the priority discovery method in this PR:

Delayed::Backend::JobPreparer#set_priority:

  def set_priority
    options[:priority] ||= Delayed::Worker.default_priority
-   queue_attributes = Delayed::Worker.queue_attributes.select { |queue| queue[:name] == options[:queue] }
+   queue_attributes = Delayed::Worker.queue_attributes.select { |queue| queue[:name].to_s == options[:queue] }
    options[:priority] = queue_attributes.first[:priority] if queue_attributes.any?
  end

@svoop
Copy link

svoop commented Nov 14, 2015

@ryannealmes I've added a PR to your fork with this fix and a little addition to the README explaining your feature. The rubocop directive cleanup suggested by @sferik is also included: ryannealmes#2

…r to pull from worker prior to setting job priority to default priority.
@ryannealmes
Copy link
Author

Thanks for the help @svoop and @sferik . I have merged and rebased @svoop 's changes. Let me know if there is anything else.

@sferik
Copy link
Collaborator

sferik commented Nov 16, 2015

Looks good to me! Thanks for the patch!

sferik added a commit that referenced this pull request Nov 16, 2015
@sferik sferik merged commit 5f91410 into collectiveidea:master Nov 16, 2015
@ryannealmes
Copy link
Author

@sferik , @svoop do you guys know why this PR wouldn't show up in my public contributions? Thanks.

@svoop
Copy link

svoop commented Nov 16, 2015

@ryannealmes No idea, after all, collectiveidea/delayed_job is listed as a repo you've contributed to. Maybe a Github glitch.

@ryannealmes
Copy link
Author

@svoop I was looking forward to being a contributor :/ At least the functionality is there now :)

@maxjacobson
Copy link

Maybe it uses the timestamp of the commit, and the public activity thing doesn't scroll back to September

@sferik
Copy link
Collaborator

sferik commented Nov 17, 2015

@ryannealmes It’s because this is a fork and GitHub only counts contributions to the original repo.

@swrobel
Copy link

swrobel commented Feb 1, 2016

Any chance of getting a release with this change before all of the rails 5 stuff @sferik?

@albus522
Copy link
Member

albus522 commented Feb 1, 2016

We already have the rails 5 stuff in master. I don't like releasing something against in flux dependencies. Have been bitten by that too much.

@ConfusedVorlon
Copy link

fair enough.

I wonder if I'm missing a simple way to get what I want anyway without risking the bleeding edge.

Is there a way to set the priority with a rails4 mailer?

something like:

#doesn't work
MyMailer.signup(message,recipient).deliver_later(priority: 5)

I know that activeMailer supports queues - so default queue priorities will allow

MyMailer.signup(message,recipient).deliver_later(queue: "high_priority")

@albus522
Copy link
Member

Be aware I have simplified the configuration structure on this in master and the about to be released gem
https://github.com/collectiveidea/delayed_job#named-queues

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants