Class: Mutagem::Lockfile
- Inherits:
-
Object
- Object
- Mutagem::Lockfile
- Defined in:
- lib/mutagem/lockfile.rb
Overview
File locking wrapper for Flock
Direct Known Subclasses
Instance Attribute Summary collapse
-
#lockfile ⇒ Object
filename for the lockfile, can include path.
Instance Method Summary collapse
-
#initialize(lockfile = nil) ⇒ Lockfile
constructor
Create a new LockFile.
-
#lock(file, mode) ⇒ Boolean
Get a file lock with flock and ensure the lock is removed (but not the lockfile).
-
#locked? ⇒ Boolean
Does another process have a lock? True if we can’t get an exclusive lock.
Constructor Details
#initialize(lockfile = nil) ⇒ Lockfile
Create a new LockFile
11 12 13 14 |
# File 'lib/mutagem/lockfile.rb', line 11 def initialize(lockfile=nil) raise ArgumentError, "lockfile not specified" unless lockfile @lockfile = lockfile end |
Instance Attribute Details
#lockfile ⇒ Object
filename for the lockfile, can include path
6 7 8 |
# File 'lib/mutagem/lockfile.rb', line 6 def lockfile @lockfile end |
Instance Method Details
#lock(file, mode) ⇒ Boolean
Get a file lock with flock and ensure the lock is removed (but not the lockfile). Accepts an optional code block.
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/mutagem/lockfile.rb', line 44 def lock(file, mode) result = file.flock(mode) if result begin yield file if block_given? # may not have block if called by locked? ensure # unlock but leave the file on the filesystem file.flock(File::LOCK_UN) end end return result end |
#locked? ⇒ Boolean
Does another process have a lock? True if we can’t get an exclusive lock
18 19 20 21 22 23 24 25 26 |
# File 'lib/mutagem/lockfile.rb', line 18 def locked? return false unless File.exists?(lockfile) result = false open(lockfile, 'w') do |f| # exclusive non-blocking lock result = !lock(f, File::LOCK_EX | File::LOCK_NB) end result end |