-
Notifications
You must be signed in to change notification settings - Fork 16
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
Use after hook #10
base: master
Are you sure you want to change the base?
Use after hook #10
Conversation
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.
Thank you! This looks good. I had a few minor changes.
lib/resque_solo/unique_job.rb
Outdated
def before_enqueue_001_solo_job(*args) | ||
queue_name = self.instance_variable_get(:@queue) | ||
item = { class: self.to_s, args: args } | ||
!ResqueSolo::Queue.queued?(queue_name, item) |
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.
Can we write this all as one line?
!ResqueSolo::Queue.queued?(@queue, class: to_s, args: args)
test/resque_test.rb
Outdated
@@ -12,7 +12,8 @@ class ResqueTest < MiniTest::Spec | |||
it "enqueues normal jobs" do | |||
Resque.enqueue FakeJob, "x" | |||
Resque.enqueue FakeJob, "x" | |||
assert_equal 2, Resque.size(:normal) | |||
queue_name = FakeJob.instance_variable_get(:@queue) |
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.
In all these tests, I prefer to simply hard-code the queue name rather than accessing an internal instance variable. So don't change the tests to use instance_variable_get
. There's no reason to change any of these tests except line 47.
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.
Sure, should be good now I think... I haven't yet figured out the workflow to rebase and squash commits yet so let me know if I need to figure out how to make that happen.
lib/resque_solo/unique_job.rb
Outdated
@@ -45,6 +45,21 @@ def ttl | |||
def lock_after_execution_period | |||
@lock_after_execution_period ||= 0 | |||
end | |||
|
|||
# We want this to run first in before_enqueue_hooks (which are alpha sorted), so name appropriately | |||
def before_enqueue_001_solo_job(*args) |
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.
Really excited to find resque_solo, the unreleased changes to make it not call after_enqueue_hooks if the job was not created, and this PR which makes the implementation hook based.
Why does the order of execution of before_enqueue hooks matter?
All before_enqueue_hooks will be invoked by Resque.enqueue_to, regardless of the result of any individual hook. So, it isn't clear to me why the order the hooks are invoked would matter.
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.
Yep, you're totally right. I just removed the misleading comment and name.
One implication of this change is that a job could be marked enqueued when it wasn't actually enqueued because a different before_enqueue hook returned false. I'll try to provide a test case to demonstrate, but can express it more quickly in a written example. Given a job class that includes Resque::Plugins::UniqueJob and also implements a before_enqueue hook that always returns false, before_enqueue_solo_job will mark the job enqueued even though it wasn't actually created. |
Auggghh you're right again. It looks like the various Marking queued:
Marking unqueued:
We can use the before hook to tell if the job has already been queued though. Although there is still a race condition if we don't check again on the actual creation (ie, in the I added some monkey patching back in, and now it should be doing a better job marking unqueued after performing the job, so I think this might get rid of the need for the Since I moved the This PR is getting kind of messy and I probably should split it up, but I need to find some time for that. If I should remove the Although since last time I thought I knew what I was talking about here and @bmckeough set me straight so I'm going to be a little more careful now ;-) so definitely looking for more feedback here. Thanks guys. |
…into use_after_hook
Gets rid of most of the monkeypatching in favor of before and around hooks, as per #7
Note that resque hooks don't give the queue that the job was pulled from, so there's no way to ensure a unique job across multiple queues, but it didn't look like that was in scope as far as I could see from the existing tests.