Sha256: 216872aca4b9d6b2e2973c7bb3168919b45e40f421276439ac50707922622549

Contents?: true

Size: 797 Bytes

Versions: 10

Compression:

Stored size: 797 Bytes

Contents

require 'json'

module ParallelTests
  class Pids
    attr_reader :file_path, :mutex

    def initialize(file_path)
      @file_path = file_path
      @mutex = Mutex.new
    end

    def add(pid)
      pids << pid.to_i
      save
    end

    def delete(pid)
      pids.delete(pid.to_i)
      save
    end

    def count
      read
      pids.count
    end

    def all
      read
      pids
    end

    private

    def pids
      @pids ||= []
    end

    def clear
      @pids = []
      save
    end

    def read
      sync do
        contents = IO.read(file_path)
        return if contents.empty?
        @pids = JSON.parse(contents)
      end
    end

    def save
      sync { IO.write(file_path, pids.to_json) }
    end

    def sync
      mutex.synchronize { yield }
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
parallel_tests-3.4.0 lib/parallel_tests/pids.rb
parallel_tests-3.3.0 lib/parallel_tests/pids.rb
parallel_tests-3.2.0 lib/parallel_tests/pids.rb
parallel_tests-3.1.0 lib/parallel_tests/pids.rb
parallel_tests-3.0.0 lib/parallel_tests/pids.rb
parallel_tests-2.32.0 lib/parallel_tests/pids.rb
parallel_tests-2.31.0 lib/parallel_tests/pids.rb
parallel_tests-2.30.1 lib/parallel_tests/pids.rb
parallel_tests-2.30.0 lib/parallel_tests/pids.rb
parallel_tests-2.29.2 lib/parallel_tests/pids.rb