Sha256: 765703907793475c9d2c616ea6807291027acdfb184b0a751a3dd674d385562e

Contents?: true

Size: 1.24 KB

Versions: 155

Compression:

Stored size: 1.24 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 = @file_locks[filepath]
              file.flock(File::LOCK_UN)
              file.close
              @file_locks.delete(filepath)
            end
          end

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

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

Version data entries

155 entries across 155 versions & 2 rubygems

Version Path
prometheus-client-mmap-1.1.2 lib/prometheus/client/helper/file_locker.rb
prometheus-client-mmap-1.1.2-x86_64-linux lib/prometheus/client/helper/file_locker.rb
prometheus-client-mmap-1.1.2-x86_64-darwin lib/prometheus/client/helper/file_locker.rb
prometheus-client-mmap-1.1.2-arm64-darwin lib/prometheus/client/helper/file_locker.rb
prometheus-client-mmap-1.1.2-aarch64-linux lib/prometheus/client/helper/file_locker.rb
prometheus-client-mmap-1.2.6 lib/prometheus/client/helper/file_locker.rb
prometheus-client-mmap-1.2.6-x86_64-linux-musl lib/prometheus/client/helper/file_locker.rb
prometheus-client-mmap-1.2.6-x86_64-linux-gnu lib/prometheus/client/helper/file_locker.rb
prometheus-client-mmap-1.2.6-x86_64-darwin lib/prometheus/client/helper/file_locker.rb
prometheus-client-mmap-1.2.6-arm64-darwin lib/prometheus/client/helper/file_locker.rb
prometheus-client-mmap-1.2.6-aarch64-linux-musl lib/prometheus/client/helper/file_locker.rb
prometheus-client-mmap-1.2.6-aarch64-linux-gnu lib/prometheus/client/helper/file_locker.rb
prometheus-client-mmap-1.2.5 lib/prometheus/client/helper/file_locker.rb
prometheus-client-mmap-1.2.5-x86_64-linux-musl lib/prometheus/client/helper/file_locker.rb
prometheus-client-mmap-1.2.5-x86_64-linux-gnu lib/prometheus/client/helper/file_locker.rb
prometheus-client-mmap-1.2.5-x86_64-darwin lib/prometheus/client/helper/file_locker.rb
prometheus-client-mmap-1.2.5-arm64-darwin lib/prometheus/client/helper/file_locker.rb
prometheus-client-mmap-1.2.5-aarch64-linux-musl lib/prometheus/client/helper/file_locker.rb
prometheus-client-mmap-1.2.5-aarch64-linux-gnu lib/prometheus/client/helper/file_locker.rb
prometheus-client-mmap-1.2.4 lib/prometheus/client/helper/file_locker.rb