Sha256: 5759601fd8c78bf9f0430ebe7454713e376e1421bd25cb7eec4844bd00e0e7c1

Contents?: true

Size: 1.42 KB

Versions: 6

Compression:

Stored size: 1.42 KB

Contents

require_relative '../path'
require_relative '../log'
require_relative '../exceptions'
require 'lockfile'

module Open
  def self.lock(file, unlock = true, options = {})
    unlock, options = true, unlock if Hash === unlock
    return yield if file.nil? and not Lockfile === options[:lock]

    if Lockfile === file
      lockfile = file
    else
      file = file.find if Path === file
      FileUtils.mkdir_p File.dirname(File.expand_path(file)) unless File.exist? File.dirname(File.expand_path(file))

      case options[:lock]
      when Lockfile
        lockfile = options[:lock]
      when FalseClass
        lockfile = nil
        unlock = false
      when Path, String
        lock_path = options[:lock].find
        lockfile = Lockfile.new(lock_path, options)
      else
        lock_path = File.expand_path(file + '.lock')
        lockfile = Lockfile.new(lock_path, options)
      end
    end

    begin
      lockfile.lock unless lockfile.nil? || lockfile.locked?
    rescue Aborted, Interrupt
      raise LockInterrupted
    end

    res = nil

    begin
      res = yield lockfile
    rescue KeepLocked
      unlock = false
      res = $!.payload
    ensure
      if unlock 
        begin
          if lockfile.locked?
            lockfile.unlock 
          else
          end
        rescue Exception
          Log.warn "Exception unlocking: #{lockfile.path}"
          Log.exception $!
        end
      end
    end

    res
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
scout-gear-7.3.0 lib/scout/open/lock.rb
scout-gear-7.2.0 lib/scout/open/lock.rb
scout-gear-7.1.0 lib/scout/open/lock.rb
scout-gear-6.0.0 lib/scout/open/lock.rb
scout-gear-5.2.0 lib/scout/open/lock.rb
scout-gear-5.1.1 lib/scout/open/lock.rb