Created
January 12, 2011 22:24
-
-
Save gondoi/777024 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class File | |
def flocked? &block | |
status = flock LOCK_EX|LOCK_NB | |
case status | |
when false | |
return true | |
when 0 | |
begin | |
block ? block.call : false | |
ensure | |
flock LOCK_UN | |
end | |
else | |
raise SystemCallError, status | |
end | |
end | |
alias_method "if_not_flocked", "flocked?" | |
end | |
dpkg = File.open("/var/lib/apt/lists/lock","r") | |
#puts dpkg.flocked? | |
dpkg.flocked? do | |
puts "not locked" | |
sleep(60) | |
end |
This file contains hidden or 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
def flocked? &block | |
status = flock LOCK_EX|LOCK_NB | |
case status | |
when false | |
return true | |
when 0 | |
flock LOCK_UN | |
return false | |
else | |
raise SystemCallError, status | |
end | |
end | |
alias_method "if_not_flocked", "flocked?" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment