Class: Ferret::Store::Lock
- Inherits:
-
Object
- Object
- Ferret::Store::Lock
- Defined in:
- lib/ferret/store/directory.rb
Overview
A Lock is used to lock a data source so that not more than one output stream can access a data source at one time. It is possible that locks could be disabled. For example a read only index stored on a CDROM would have no need for a lock.
You can use a lock in two ways. Firstly:
write_lock = @directory.make_lock(LOCK_NAME)
write_lock.obtain(WRITE_LOCK_TIME_OUT)
... # Do your file modifications # ...
write_lock.release()
Alternatively you could use the while locked method. This ensures that the lock will be released once processing has finished.
write_lock = @directory.make_lock(LOCK_NAME)
write_lock.while_locked(WRITE_LOCK_TIME_OUT) do
... # Do your file modifications # ...
end
Direct Known Subclasses
Constant Summary collapse
- MAX_ATTEMPTS =
Attempts made to obtain the lock before the application gives up. If you want the process to wait longer to get the lock then just increase the lock timeout
5
Instance Method Summary collapse
-
#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.
-
#while_locked(lock_timeout = 1) ⇒ Object
Obtains the lock, processes the block and ensures that the lock is released when the block terminates.
Instance Method Details
#locked? ⇒ Boolean
Returns true if there is a lock on the data source
124 125 126 |
# File 'lib/ferret/store/directory.rb', line 124 def locked? raise NotImplementedError end |
#obtain(lock_timeout = 1) ⇒ Object
Obtain the lock on the data source. If you expect to have to wait for a while on a lock then you should set the lock_timeout to a large number. This may be necessary if you are doing multiple large batch updates on an index but the default 1 second should be fine in most cases.
114 115 116 |
# File 'lib/ferret/store/directory.rb', line 114 def obtain(lock_timeout = 1) raise NotImplementedError end |
#release ⇒ Object
Release the lock on the data source
119 120 121 |
# File 'lib/ferret/store/directory.rb', line 119 def release raise NotImplementedError end |
#while_locked(lock_timeout = 1) ⇒ Object
Obtains the lock, processes the block and ensures that the lock is released when the block terminates. The lock timeout is in seconds.
130 131 132 133 134 135 136 137 |
# File 'lib/ferret/store/directory.rb', line 130 def while_locked(lock_timeout=1) obtain(lock_timeout) begin yield ensure release() end end |