Sha256: 314cd267a9eb96da77f9044f13cb8d4fe4b28de99245f0147d5175d674c3813f

Contents?: true

Size: 770 Bytes

Versions: 15

Compression:

Stored size: 770 Bytes

Contents

module Ro
  class Lock
    def initialize(path)
      @path = path.to_s
      @fd = false
    end

    def lock(&block)
      open!

      if block
        begin
          lock!
          block.call
        ensure
          unlock!
        end
      else
        self
      end
    end

    def open!
      @fd ||= (
        fd =
          begin
            open(@path, 'ab+')
          rescue
            unless test(?e, @path)
              FileUtils.mkdir_p(@path)
              FileUtils.touch(@path)
            end

            open(@path, 'ab+')
          end

        fd.close_on_exec = true

        fd
      )
    end

    def lock!
      open!
      @fd.flock File::LOCK_EX
    end

    def unlock!
      open!
      @fd.flock File::LOCK_UN
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
ro-1.4.6 lib/ro/lock.rb
ro-1.4.4 lib/ro/lock.rb
ro-1.4.3 lib/ro/lock.rb
ro-1.4.2 lib/ro/lock.rb
ro-1.4.1 lib/ro/lock.rb
ro-1.4.0 lib/ro/lock.rb
ro-1.3.8 lib/ro/lock.rb
ro-1.3.7 lib/ro/lock.rb
ro-1.3.6 lib/ro/lock.rb
ro-1.3.5 lib/ro/lock.rb
ro-1.3.4 lib/ro/lock.rb
ro-1.3.3 lib/ro/lock.rb
ro-1.3.2 lib/ro/lock.rb
ro-1.3.1 lib/ro/lock.rb
ro-1.2.0 lib/ro/lock.rb