Extract formula locks into a class
This commit is contained in:
parent
97f9f93f25
commit
97d3ae1775
@ -1,6 +1,7 @@
|
|||||||
require 'download_strategy'
|
require 'download_strategy'
|
||||||
require 'dependency_collector'
|
require 'dependency_collector'
|
||||||
require 'formula_support'
|
require 'formula_support'
|
||||||
|
require 'formula_lock'
|
||||||
require 'hardware'
|
require 'hardware'
|
||||||
require 'bottles'
|
require 'bottles'
|
||||||
require 'patches'
|
require 'patches'
|
||||||
@ -241,19 +242,12 @@ class Formula
|
|||||||
end
|
end
|
||||||
|
|
||||||
def lock
|
def lock
|
||||||
HOMEBREW_CACHE_FORMULA.mkpath
|
@lock = FormulaLock.new(name)
|
||||||
lockpath = HOMEBREW_CACHE_FORMULA/"#{@name}.brewing"
|
@lock.lock
|
||||||
@lockfile = lockpath.open(File::RDWR | File::CREAT)
|
|
||||||
unless @lockfile.flock(File::LOCK_EX | File::LOCK_NB)
|
|
||||||
raise OperationInProgressError, @name
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def unlock
|
def unlock
|
||||||
unless @lockfile.nil?
|
@lock.unlock unless @lock.nil?
|
||||||
@lockfile.flock(File::LOCK_UN)
|
|
||||||
@lockfile.close
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def == b
|
def == b
|
||||||
|
40
Library/Homebrew/formula_lock.rb
Normal file
40
Library/Homebrew/formula_lock.rb
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
class FormulaLock
|
||||||
|
LOCKDIR = HOMEBREW_CACHE_FORMULA
|
||||||
|
|
||||||
|
def initialize(name)
|
||||||
|
@name = name
|
||||||
|
@path = LOCKDIR.join("#{@name}.brewing")
|
||||||
|
end
|
||||||
|
|
||||||
|
def lock
|
||||||
|
LOCKDIR.mkpath
|
||||||
|
@lockfile = get_or_create_lockfile
|
||||||
|
unless @lockfile.flock(File::LOCK_EX | File::LOCK_NB)
|
||||||
|
raise OperationInProgressError, @name
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def unlock
|
||||||
|
unless @lockfile.nil? || @lockfile.closed?
|
||||||
|
@lockfile.flock(File::LOCK_UN)
|
||||||
|
@lockfile.close
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def with_lock
|
||||||
|
lock
|
||||||
|
yield
|
||||||
|
ensure
|
||||||
|
unlock
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def get_or_create_lockfile
|
||||||
|
if @lockfile.nil? || @lockfile.closed?
|
||||||
|
@path.open(File::RDWR | File::CREAT)
|
||||||
|
else
|
||||||
|
@lockfile
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -1,4 +1,5 @@
|
|||||||
require 'extend/pathname'
|
require 'extend/pathname'
|
||||||
|
require 'formula_lock'
|
||||||
|
|
||||||
class Keg < Pathname
|
class Keg < Pathname
|
||||||
def initialize path
|
def initialize path
|
||||||
@ -62,18 +63,7 @@ class Keg < Pathname
|
|||||||
end
|
end
|
||||||
|
|
||||||
def lock
|
def lock
|
||||||
HOMEBREW_CACHE_FORMULA.mkpath
|
FormulaLock.new(fname).with_lock { yield }
|
||||||
path = HOMEBREW_CACHE_FORMULA/"#{fname}.brewing"
|
|
||||||
file = path.open(File::RDWR | File::CREAT)
|
|
||||||
unless file.flock(File::LOCK_EX | File::LOCK_NB)
|
|
||||||
raise OperationInProgressError, fname
|
|
||||||
end
|
|
||||||
yield
|
|
||||||
ensure
|
|
||||||
unless file.nil?
|
|
||||||
file.flock(File::LOCK_UN)
|
|
||||||
file.close
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def linked_keg_record
|
def linked_keg_record
|
||||||
|
21
Library/Homebrew/test/test_formula_lock.rb
Normal file
21
Library/Homebrew/test/test_formula_lock.rb
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
require 'testing_env'
|
||||||
|
require 'formula_lock'
|
||||||
|
|
||||||
|
class FormulaLockTests < Test::Unit::TestCase
|
||||||
|
def setup
|
||||||
|
@lock = FormulaLock.new("foo")
|
||||||
|
@lock.lock
|
||||||
|
end
|
||||||
|
|
||||||
|
def teardown
|
||||||
|
@lock.unlock
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_locking_file_with_existing_lock_raises_error
|
||||||
|
assert_raises(OperationInProgressError) { FormulaLock.new("foo").lock }
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_locking_existing_lock_suceeds
|
||||||
|
assert_nothing_raised { @lock.lock }
|
||||||
|
end
|
||||||
|
end
|
Loading…
x
Reference in New Issue
Block a user