Sha256: 0d0885223d1da02eb65e9c6bbbef10259413f5fbb98139ab0f96e5d24ac584cf

Contents?: true

Size: 657 Bytes

Versions: 4

Compression:

Stored size: 657 Bytes

Contents

require 'fileutils'

module WithAdvisoryLock
  class Flock < Base

    def filename
      @filename ||= begin
        safe = @lock_name.to_s.gsub(/[^a-z0-9]/i, '')
        fn = ".lock-#{safe}-#{stable_hashcode(@lock_name)}"
        # 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
      0 == file_io.flock(File::LOCK_EX|File::LOCK_NB)
    end

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
with_advisory_lock-1.0.0 lib/with_advisory_lock/flock.rb
with_advisory_lock-0.0.11 lib/with_advisory_lock/flock.rb
with_advisory_lock-0.0.10 lib/with_advisory_lock/flock.rb
with_advisory_lock-0.0.9 lib/with_advisory_lock/flock.rb