Sha256: f9046216bfc3ca28ef04261ff40545e07eccda9fccffdf43d4c68897133c2be0

Contents?: true

Size: 837 Bytes

Versions: 26

Compression:

Stored size: 837 Bytes

Contents

# frozen_string_literal: true
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 = File.read(file_path)
        return if contents.empty?
        @pids = JSON.parse(contents)
      end
    end

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

    def sync(&block)
      mutex.synchronize(&block)
    end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
parallel_tests-4.7.2 lib/parallel_tests/pids.rb
parallel_tests-4.7.1 lib/parallel_tests/pids.rb
parallel_tests-4.7.0 lib/parallel_tests/pids.rb
parallel_tests-4.6.1 lib/parallel_tests/pids.rb
parallel_tests-4.6.0 lib/parallel_tests/pids.rb
parallel_tests-4.5.2 lib/parallel_tests/pids.rb
parallel_tests-4.5.1 lib/parallel_tests/pids.rb
parallel_tests-4.5.0 lib/parallel_tests/pids.rb
parallel_tests-4.4.0 lib/parallel_tests/pids.rb
parallel_tests-4.3.0 lib/parallel_tests/pids.rb
parallel_tests-4.2.2 lib/parallel_tests/pids.rb
parallel_tests-4.2.1 lib/parallel_tests/pids.rb
parallel_tests-4.2.0 lib/parallel_tests/pids.rb
parallel_tests-4.1.0 lib/parallel_tests/pids.rb
parallel_tests-4.0.0 lib/parallel_tests/pids.rb
parallel_tests-3.13.0 lib/parallel_tests/pids.rb
parallel_tests-3.12.1 lib/parallel_tests/pids.rb
parallel_tests-3.12.0 lib/parallel_tests/pids.rb
parallel_tests-3.11.1 lib/parallel_tests/pids.rb
parallel_tests-3.11.0 lib/parallel_tests/pids.rb