Class: Ferret::Store::FSDirectory::FSLock
- Defined in:
- lib/ferret/store/fs_store.rb
Overview
See Lock for hints as to how to use locks.
Constant Summary
Constants inherited from Lock
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(lock_file) ⇒ FSLock
constructor
pass the name of the file that we are going to lock.
-
#locked? ⇒ Boolean
returns true if there is a lock on the data source.
-
#obtain(lock_timeout = 1) ⇒ Object
obtain the lock on the data source.
-
#release ⇒ Object
Release the lock on the data source.
Methods inherited from Lock
Constructor Details
#initialize(lock_file) ⇒ FSLock
pass the name of the file that we are going to lock
208 209 210 211 212 |
# File 'lib/ferret/store/fs_store.rb', line 208 def initialize(lock_file) @lock_file = lock_file #@clean = FSLock.make_finalizer(lock_file) @clean = lambda { File.delete(lock_file) rescue nil} end |
Class Method Details
.make_finalizer(lock_file) ⇒ Object
214 215 216 |
# File 'lib/ferret/store/fs_store.rb', line 214 def FSLock.make_finalizer(lock_file) lambda { File.delete(lock_file) rescue nil} end |
Instance Method Details
#locked? ⇒ Boolean
returns true if there is a lock on the data source
252 253 254 255 |
# File 'lib/ferret/store/fs_store.rb', line 252 def locked? return false if FSDirectory.locks_disabled? File.exists?(@lock_file) end |
#obtain(lock_timeout = 1) ⇒ Object
obtain the lock on the data source
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/ferret/store/fs_store.rb', line 219 def obtain(lock_timeout = 1) return true if FSDirectory.locks_disabled? MAX_ATTEMPTS.times do begin # create a file if none exists. If one already exists # then someone beat us to the lock so return false File.open(@lock_file, File::WRONLY|File::EXCL|File::CREAT) {|f|} ObjectSpace.define_finalizer(self, @clean) return true rescue SystemCallError # lock was not obtained so sleep for timeout then try again. sleep(lock_timeout) end end # lock could not be obtained so raise an exception raise "could not obtain lock: #{@lock_file}" end |
#release ⇒ Object
Release the lock on the data source. Returns true if successful.
238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/ferret/store/fs_store.rb', line 238 def release return if FSDirectory.locks_disabled? begin File.delete(@lock_file) ObjectSpace.undefine_finalizer(self) rescue SystemCallError # maybe we tried to release a lock that wasn't locked. This # isn't critical so just return false return false end return true end |