Class: DaemonController::LockFile
- Inherits:
-
Object
- Object
- DaemonController::LockFile
- Defined in:
- lib/daemon_controller/lock_file.rb
Defined Under Namespace
Classes: AlreadyLocked
Instance Method Summary collapse
- #exclusive_lock ⇒ Object
-
#initialize(filename) ⇒ LockFile
constructor
A new instance of LockFile.
- #shared_lock ⇒ Object
- #try_exclusive_lock ⇒ Object
- #try_shared_lock ⇒ Object
Constructor Details
#initialize(filename) ⇒ LockFile
Returns a new instance of LockFile.
27 28 29 |
# File 'lib/daemon_controller/lock_file.rb', line 27 def initialize(filename) @filename = filename end |
Instance Method Details
#exclusive_lock ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/daemon_controller/lock_file.rb', line 31 def exclusive_lock File.open(@filename, 'w') do |f| if Fcntl.const_defined? :F_SETFD f.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC) end f.flock(File::LOCK_EX) yield end end |
#shared_lock ⇒ Object
41 42 43 44 45 46 47 48 49 |
# File 'lib/daemon_controller/lock_file.rb', line 41 def shared_lock File.open(@filename, 'w') do |f| if Fcntl.const_defined? :F_SETFD f.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC) end f.flock(File::LOCK_SH) yield end end |
#try_exclusive_lock ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/daemon_controller/lock_file.rb', line 64 def try_exclusive_lock File.open(@filename, 'w') do |f| if Fcntl.const_defined? :F_SETFD f.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC) end if f.flock(File::LOCK_EX | File::LOCK_NB) yield else raise AlreadyLocked end end end |
#try_shared_lock ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/daemon_controller/lock_file.rb', line 51 def try_shared_lock File.open(@filename, 'w') do |f| if Fcntl.const_defined? :F_SETFD f.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC) end if f.flock(File::LOCK_SH | File::LOCK_NB) yield else raise AlreadyLocked end end end |