Skip to content

Instantly share code, notes, and snippets.

@gondoi
Created January 12, 2011 22:24
Show Gist options
  • Save gondoi/777024 to your computer and use it in GitHub Desktop.
Save gondoi/777024 to your computer and use it in GitHub Desktop.
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
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