Sha256: b9a765b1edd5d5713c1f1b20737dafb79d30340eb10d29b547a712e112b7af76

Contents?: true

Size: 796 Bytes

Versions: 3

Compression:

Stored size: 796 Bytes

Contents

require 'fileutils'

module WithAdvisoryLock
  class Flock < Base

    def filename
      @filename ||= begin
        safe = lock_str.to_s.gsub(/[^a-z0-9]/i, '')
        fn = ".lock-#{safe}-#{stable_hashcode(lock_str)}"
        # Let the user specify a directory besides CWD.
        ENV['FLOCK_DIR'] ? File.expand_path(fn, ENV['FLOCK_DIR']) : fn
      end
    end

    def file_io
      @file_io ||= begin
        FileUtils.touch(filename)
        File.open(filename, 'r+')
      end
    end

    def try_lock
      if transaction
        raise ArgumentError, 'transaction level locks are not supported on SQLite'
      end
      0 == file_io.flock((shared ? File::LOCK_SH : File::LOCK_EX) | File::LOCK_NB)
    end

    def release_lock
      0 == file_io.flock(File::LOCK_UN)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
with_advisory_lock-3.2.0 lib/with_advisory_lock/flock.rb
with_advisory_lock-3.1.1 lib/with_advisory_lock/flock.rb
with_advisory_lock-3.1.0 lib/with_advisory_lock/flock.rb