Sha256: 97335b5f76ccfd9e73df0073ced6a732655903acf53720b9327847a3c52c9b80

Contents?: true

Size: 1.12 KB

Versions: 55

Compression:

Stored size: 1.12 KB

Contents

module Prometheus
  module Client
    module Helper
      class FileLocker
        class << self
          LOCK_FILE_MUTEX = Mutex.new

          def lock_to_process(filepath)
            LOCK_FILE_MUTEX.synchronize do
              @file_locks ||= {}
              return false if @file_locks[filepath]

              file = File.open(filepath, 'ab')
              if file.flock(File::LOCK_NB | File::LOCK_EX)
                @file_locks[filepath] = file
                return true
              else
                return false
              end
            end
          end

          def unlock(filepath)
            LOCK_FILE_MUTEX.synchronize do
              @file_locks ||= {}
              return false unless @file_locks[filepath]

              @file_locks.delete(filepath).flock(File::LOCK_UN)
            end
          end

          def unlock_all
            LOCK_FILE_MUTEX.synchronize do
              @file_locks ||= {}
              @file_locks.values.each do |file|
                file.flock(File::LOCK_UN)
              end

              @file_locks = {}
            end
          end
        end
      end
    end
  end
end

Version data entries

55 entries across 55 versions & 1 rubygems

Version Path
prometheus-client-mmap-0.15.0 lib/prometheus/client/helper/file_locker.rb
prometheus-client-mmap-0.14.0 lib/prometheus/client/helper/file_locker.rb
prometheus-client-mmap-0.13.0 lib/prometheus/client/helper/file_locker.rb
prometheus-client-mmap-0.12.0 lib/prometheus/client/helper/file_locker.rb
prometheus-client-mmap-0.11.0 lib/prometheus/client/helper/file_locker.rb
prometheus-client-mmap-0.10.0 lib/prometheus/client/helper/file_locker.rb
prometheus-client-mmap-0.9.10 lib/prometheus/client/helper/file_locker.rb
prometheus-client-mmap-0.9.9 lib/prometheus/client/helper/file_locker.rb
prometheus-client-mmap-0.9.8 lib/prometheus/client/helper/file_locker.rb
prometheus-client-mmap-0.9.7 lib/prometheus/client/helper/file_locker.rb
prometheus-client-mmap-0.9.6 lib/prometheus/client/helper/file_locker.rb
prometheus-client-mmap-0.9.5 lib/prometheus/client/helper/file_locker.rb
prometheus-client-mmap-0.9.4 lib/prometheus/client/helper/file_locker.rb
prometheus-client-mmap-0.9.3 lib/prometheus/client/helper/file_locker.rb
prometheus-client-mmap-0.9.2 lib/prometheus/client/helper/file_locker.rb
prometheus-client-mmap-0.9.1 lib/prometheus/client/helper/file_locker.rb
prometheus-client-mmap-0.9.1.pre.rc.2 lib/prometheus/client/helper/file_locker.rb
prometheus-client-mmap-0.9.1.pre.rc.1 lib/prometheus/client/helper/file_locker.rb
prometheus-client-mmap-0.9.0.pre.rc.1 lib/prometheus/client/helper/file_locker.rb
prometheus-client-mmap-0.9.0 lib/prometheus/client/helper/file_locker.rb