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

Lock Improvements #129

Closed
wants to merge 2 commits into from
Closed

Conversation

ecin
Copy link
Contributor

@ecin ecin commented Aug 14, 2014

This code goes along with #128

It refactors how Rufus::Scheduler handles locks (no more need for subclasses!), but more importantly it allows multiple schedulers to run concurrently, with only one of them running jobs at any given time.

Additionally, this fixes a seeming bug with JobArray#delete_unscheduled. A job's @next_time value can be nil or false if it never intends to run again, but we only checked for a nil value. With the fix, jobs that are scheduled using scheduled_in, for example, will get pruned.

Having the lock be an argument makes it easier to implement different
types of locks, since we won't need to subclass from Scheduler and
instead can implement smaller classes.
This allows for multiple scheduling processes to run concurrently, but
only one of them will run jobs at any given time.
@@ -65,7 +65,7 @@ def delete_unscheduled

@mutex.synchronize {

@array.delete_if { |j| j.next_time.nil? || j.unscheduled_at }
@array.delete_if { |j| !j.next_time || j.unscheduled_at }
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jmettraux am I right to think this is a bug?

Copy link
Owner

Choose a reason for hiding this comment

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

It's just code, not a bug.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So if we have:

job = proc { }
scheduler = Rufus::Scheduler.new
scheduler.schedule_in(0, job)

Once the job is triggered, its @next_time value will be false (since it'll only run once). Shouldn't it get deleted from the jobs array at that point?

Copy link
Owner

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, that makes sense.

My example is what happens when confirm_lock returns true, so the job triggers but it doesn't run its block:

callable = proc { }
scheduler = Rufus::Scheduler.new
scheduler.pause # let's do some setup first

# jobs will still be scheduled and triggered, but the trigger method will return early
def scheduler.confirm_lock
  false
end

job = scheduler.schedule_in(0, callable)

job.next_time # => Time object

# let's trigger jobs again
scheduler.resume 

sleep 1

job.next_time # => false

def scheduler.confirm_lock
  true
end

# job is still not run, but stays in scheduler.jobs

So my question: what does it mean when @next_time is false? Doesn't seem like the job is ever run.

Copy link
Owner

Choose a reason for hiding this comment

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

@next_time == false, means the job was about to be triggered (pre trigger) but didn't. @next_time == nil is when post trigger.

I'll investigate what you found in gh-130

Thanks a lot!

@jmettraux
Copy link
Owner

Hello,

so this pull request comprises two things, a refactor and a bug fix.

The refactor work is elegant.

I don't see a spec for

it allows multiple schedulers to run concurrently

or for

this fixes a seeming bug with JobArray#delete_unscheduled

I don't see code that prevents scheduled jobs from occurring in one of those "locked out" scheduler instances.

Thanks for your time.

@jmettraux jmettraux closed this Aug 14, 2014
jmettraux added a commit that referenced this pull request Aug 14, 2014
@ecin ecin mentioned this pull request Aug 14, 2014
@ecin
Copy link
Contributor Author

ecin commented Aug 14, 2014

#131 continues this work.

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

2 participants