Sha256: f3bbbe12c5876cedf3f502808ecebc2075543e7da0d5f1668fd4deb80e53780f

Contents?: true

Size: 557 Bytes

Versions: 19

Compression:

Stored size: 557 Bytes

Contents

require 'fileutils'

module Cerberus
  class Latch
    #Emulate File.flock
    def self.lock(lock_file, options = {})
      if File.exists?(lock_file)
        modif_time = File::Stat.new(lock_file).mtime
        ttl = options[:lock_ttl]

        if ttl and modif_time + ttl < Time.now
          File.delete(lock_file)
        else
          return
        end
      end

      begin
        FileUtils.mkpath(File.dirname(lock_file))
        File.new(lock_file, 'w').close
        yield
      ensure
        File.delete(lock_file)
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 2 rubygems

Version Path
cerberus-0.8.0 lib/cerberus/latch.rb
cerberus-0.7.9 lib/cerberus/latch.rb
edavis10-cerberus-0.7.8 lib/cerberus/latch.rb
cerberus-0.7.8 lib/cerberus/latch.rb
cerberus-0.7.7 lib/cerberus/latch.rb
cerberus-0.7.6 lib/cerberus/latch.rb
cerberus-0.7.2 lib/cerberus/latch.rb
cerberus-0.7.5 lib/cerberus/latch.rb
cerberus-0.7 lib/cerberus/latch.rb
cerberus-0.4.0 lib/cerberus/latch.rb
cerberus-0.3.6 lib/cerberus/latch.rb
cerberus-0.4.4 lib/cerberus/latch.rb
cerberus-0.4.1 lib/cerberus/latch.rb
cerberus-0.4.3 lib/cerberus/latch.rb
cerberus-0.4.2 lib/cerberus/latch.rb
cerberus-0.4.5.1 lib/cerberus/latch.rb
cerberus-0.4.5 lib/cerberus/latch.rb
cerberus-0.5 lib/cerberus/latch.rb
cerberus-0.6 lib/cerberus/latch.rb