Sha256: 5df27ecd17cec422706f79169d102f8c38f4ff9ae400cffd5633527541aaabab

Contents?: true

Size: 823 Bytes

Versions: 6

Compression:

Stored size: 823 Bytes

Contents

require 'tempfile'
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

6 entries across 6 versions & 1 rubygems

Version Path
parallel_tests-2.21.0 lib/parallel_tests/pids.rb
parallel_tests-2.20.0 lib/parallel_tests/pids.rb
parallel_tests-2.19.0 lib/parallel_tests/pids.rb
parallel_tests-2.18.0 lib/parallel_tests/pids.rb
parallel_tests-2.17.1 lib/parallel_tests/pids.rb
parallel_tests-2.17.0 lib/parallel_tests/pids.rb