Class: Aptly::Mutex
- Inherits:
-
Object
- Object
- Aptly::Mutex
- Defined in:
- lib/aptly/mutex.rb
Constant Summary collapse
- @@mutex_path =
'/tmp/aptly.lock'
Class Method Summary collapse
-
.lock ⇒ Object
Attempts to acquire the aptly mutex.
-
.locked? ⇒ Boolean
Checks if the mutex is in use.
-
.mutex_path ⇒ Object
Static attribute accessor for mutex path.
-
.mutex_path=(path) ⇒ Object
Alter the mutex path.
-
.running? ⇒ Boolean
Checks if the process which originally acquired the mutex is still running.
-
.unlock ⇒ Object
Unlocks the mutex so that other processes may acquire it.
Class Method Details
.lock ⇒ Object
Attempts to acquire the aptly mutex. This method will wait if the mutex is already locked elsewhere, and check back every 5 seconds to see if it has been freed. On each check where the mutex is determined to be in use, we check if the process which originally acquired the lock is still running so we can acquire a stale mutex should we need to.
Returns:
True, once the lock is acquired.
37 38 39 40 41 42 |
# File 'lib/aptly/mutex.rb', line 37 def self.lock while self.locked? self.running? && sleep(5) || self.unlock end File.open(@@mutex_path, 'w') {|f| f.write Process.pid} end |
.locked? ⇒ Boolean
Checks if the mutex is in use. This is done via a simple file check.
Returns:
True if locked, else false
49 50 51 |
# File 'lib/aptly/mutex.rb', line 49 def self.locked? File.exist? @@mutex_path end |
.mutex_path ⇒ Object
Static attribute accessor for mutex path
Returns:
The path to the aptly mutex file
12 13 14 |
# File 'lib/aptly/mutex.rb', line 12 def self.mutex_path @@mutex_path end |
.mutex_path=(path) ⇒ Object
Alter the mutex path. This should be set to the same path in all places where ruby-aptly will be used on the same host, since the mutex is meant to be system-wide.
Parameters:
- path
-
The desired path for the mutex
24 25 26 |
# File 'lib/aptly/mutex.rb', line 24 def self.mutex_path= path @@mutex_path = path end |
.running? ⇒ Boolean
Checks if the process which originally acquired the mutex is still running. This is useful to determine staleness of a locked mutex.
Returns:
True if running, else false
59 60 61 62 63 64 65 66 67 |
# File 'lib/aptly/mutex.rb', line 59 def self.running? begin pid = File.open(@@mutex_path).read.to_i Process.kill(0, pid) return true rescue Errno::ESRCH, Errno::ENOENT return false end end |
.unlock ⇒ Object
Unlocks the mutex so that other processes may acquire it.
Returns:
True if unlock happens, else false
74 75 76 |
# File 'lib/aptly/mutex.rb', line 74 def self.unlock File.delete @@mutex_path if self.locked? end |