Sha256: 40bfdfbbd74c1002191c7cb6f48276b5e03759f1da24775ce2a696d7bcde0d54

Contents?: true

Size: 804 Bytes

Versions: 15

Compression:

Stored size: 804 Bytes

Contents

require 'json'

module ParallelTests
  class Pids
    attr_reader :pids, :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

15 entries across 15 versions & 1 rubygems

Version Path
parallel_tests-2.29.1 lib/parallel_tests/pids.rb
parallel_tests-2.29.0 lib/parallel_tests/pids.rb
parallel_tests-2.28.0 lib/parallel_tests/pids.rb
parallel_tests-2.27.1 lib/parallel_tests/pids.rb
parallel_tests-2.27.0 lib/parallel_tests/pids.rb
parallel_tests-2.26.2 lib/parallel_tests/pids.rb
parallel_tests-2.26.0 lib/parallel_tests/pids.rb
parallel_tests-2.25.0 lib/parallel_tests/pids.rb
parallel_tests-2.24.0 lib/parallel_tests/pids.rb
parallel_tests-2.23.0 lib/parallel_tests/pids.rb
parallel_tests-2.22.1 lib/parallel_tests/pids.rb
parallel_tests-2.22.0 lib/parallel_tests/pids.rb
parallel_tests-2.21.3 lib/parallel_tests/pids.rb
parallel_tests-2.21.2 lib/parallel_tests/pids.rb
parallel_tests-2.21.1 lib/parallel_tests/pids.rb