Class: Ferret::Store::FSDirectory::FSLock

Inherits:
Lock
  • Object
show all
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

Lock::MAX_ATTEMPTS

Instance Method Summary collapse

Methods inherited from Lock

#while_locked

Constructor Details

#initialize(lock_file) ⇒ FSLock

pass the name of the file that we are going to lock



207
208
209
# File 'lib/ferret/store/fs_store.rb', line 207

def initialize(lock_file)
  @lock_file = lock_file
end

Instance Method Details

#locked?Boolean

returns true if there is a lock on the data source

Returns:

  • (Boolean)


243
244
245
246
# File 'lib/ferret/store/fs_store.rb', line 243

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



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/ferret/store/fs_store.rb', line 212

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|}
      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.to_s
end

#releaseObject

Release the lock on the data source. Returns true if successful.



230
231
232
233
234
235
236
237
238
239
240
# File 'lib/ferret/store/fs_store.rb', line 230

def release 
  return if FSDirectory.locks_disabled?
  begin
    File.delete(@lock_file)
  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