Class: File

Inherits:
Object
  • Object
show all
Defined in:
lib/logging/utils.rb

Overview


Instance Method Summary collapse

Instance Method Details

#flock?Boolean

Returns true if another process holds an exclusive lock on the file. Returns false if this is not the case.

If a block of code is passed to this method, it will be run iff this process can obtain an exclusive lock on the file. The block will be run while this lock is held, and the exclusive lock will be released when the method returns.

The exclusive lock is requested in a non-blocking mode. This method will return immediately (and the block will not be executed) if an exclusive lock cannot be obtained.

Returns:

  • (Boolean)


84
85
86
87
88
89
90
91
92
93
94
# File 'lib/logging/utils.rb', line 84

def flock?
  status = flock(LOCK_EX|LOCK_NB)
  case status
  when false; true
  when 0; block_given? ? yield : false
  else
    raise SystemCallError, "flock failed with status: #{status}"
  end
ensure
  flock LOCK_UN
end

#flock_shObject

Execute a block in the context of a shared lock on this file. A shared lock will be obtained on the file, the block executed, and the lock released.



100
101
102
103
104
105
# File 'lib/logging/utils.rb', line 100

def flock_sh
  flock LOCK_SH
  yield
ensure
  flock LOCK_UN
end