-
Notifications
You must be signed in to change notification settings - Fork 201
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
Closed
Lock Improvements #129
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
require "fileutils" | ||
|
||
module Rufus | ||
module Lock | ||
|
||
# Returns true if the scheduler has acquired the [exclusive] lock and | ||
# thus may run. | ||
# | ||
# Most of the time, a scheduler is run alone and this method should | ||
# return true. It is useful in cases where among a group of applications | ||
# only one of them should run the scheduler. For schedulers that should | ||
# not run, the method should return false. | ||
# | ||
# Out of the box, rufus-scheduler proposes the | ||
# :lockfile => 'path/to/lock/file' scheduler start option. It makes | ||
# it easy for schedulers on the same machine to determine which should | ||
# run (to first to write the lockfile and lock it). It uses "man 2 flock" | ||
# so it probably won't work reliably on distributed file systems. | ||
# | ||
# If one needs to use a special/different locking mechanism, providing | ||
# overriding implementation for this #lock and the #unlock complement is | ||
# easy. | ||
class Flock | ||
attr_reader :path | ||
|
||
def initialize(path) | ||
|
||
@path = path.to_s | ||
end | ||
|
||
def lock | ||
return true if locked? | ||
|
||
@lockfile = nil | ||
|
||
FileUtils.mkdir_p(::File.dirname(@path)) | ||
|
||
file = File.new(@path, File::RDWR | File::CREAT) | ||
locked = file.flock(File::LOCK_NB | File::LOCK_EX) | ||
|
||
return false unless locked | ||
|
||
now = Time.now | ||
|
||
file.print("pid: #{$$}, ") | ||
file.print("scheduler.object_id: #{self.object_id}, ") | ||
file.print("time: #{now}, ") | ||
file.print("timestamp: #{now.to_f}") | ||
file.flush | ||
|
||
@lockfile = file | ||
|
||
true | ||
end | ||
|
||
def unlock | ||
!!(@lockfile.flock(File::LOCK_UN) if @lockfile) | ||
end | ||
|
||
def locked? | ||
!!(@lockfile.flock(File::LOCK_NB | File::LOCK_EX) if @lockfile) | ||
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module Rufus | ||
module Lock | ||
# A lock that can always be acquired | ||
class Null | ||
def lock; true; end | ||
def locked?; true; end | ||
def unlock; true; 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
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,43 @@ | ||
|
||
# | ||
# Specifying rufus-scheduler | ||
# | ||
# Tue Aug 13 05:58:48 JST 2013 | ||
# | ||
|
||
require 'spec_helper' | ||
|
||
|
||
describe Rufus::Lock::Flock do | ||
|
||
before :each do | ||
@lock_path = '.rufus-scheduler.lock' | ||
@lock = Rufus::Lock::Flock.new(@lock_path) | ||
end | ||
|
||
after :each do | ||
|
||
FileUtils.rm_f(@lock_path) | ||
FileUtils.rm_f('lock.txt') | ||
end | ||
|
||
context ':lock => Rufus::Lock::File.new(path)' do | ||
|
||
it 'writes down a .rufus-scheduler.lock file' do | ||
@lock.lock | ||
|
||
line = File.read(@lock_path) | ||
|
||
expect(line).to match(/pid: #{$$}/) | ||
end | ||
|
||
it '"flocks" the lock file' do | ||
@lock.lock | ||
|
||
f = File.new(@lock_path, 'a') | ||
|
||
expect(f.flock(File::LOCK_NB | File::LOCK_EX)).to eq(false) | ||
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
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.
@jmettraux am I right to think this is a bug?
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 just code, not a bug.
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.
So if we have:
Once the job is triggered, its
@next_time
value will befalse
(since it'll only run once). Shouldn't it get deleted from the jobs array at that point?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.
https://gist.github.com/jmettraux/3245c45f49693a2f5e57
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.
Ah, that makes sense.
My example is what happens when
confirm_lock
returnstrue
, so the job triggers but it doesn't run its block:So my question: what does it mean when
@next_time
isfalse
? Doesn't seem like the job is ever run.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.
@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!