Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

add BackgroundForMultiProcess #572

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/coverband.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
require "coverband/reporters/console_report"
require "coverband/integrations/background"
require "coverband/integrations/background_middleware"
require "coverband/integrations/background_for_multi_process"
require "coverband/integrations/rack_server_check"
require "coverband/configuration"

Expand Down
3 changes: 2 additions & 1 deletion lib/coverband/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class Configuration
:view_tracker, :defer_eager_loading_data,
:track_routes, :track_redirect_routes, :route_tracker,
:track_translations, :translations_tracker,
:trackers, :csp_policy, :hide_settings
:trackers, :csp_policy, :hide_settings,
:store_for_multi_process_background, :filepath_pattern_for_multi_process, :batch_size_for_multi_process

attr_writer :logger, :s3_region, :s3_bucket, :s3_access_key_id,
:s3_secret_access_key, :password, :api_key, :service_url, :coverband_timeout, :service_dev_mode,
Expand Down
132 changes: 132 additions & 0 deletions lib/coverband/integrations/background_for_multi_process.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
# frozen_string_literal: true

module Coverband
class BackgroundForMultiProcess
include Singleton

def self.start
instance.start
end

def self.stop
instance.stop
end

def initialize
if !Coverband.configuration.store.is_a?(Coverband::Adapters::FileStore)
raise 'Coverband::BackgroundForMultiProcess only supports children processes using the FileStore adapter'
end

@semaphore = Mutex.new
@thread = nil
@pid = nil

@store = Coverband.configuration.store_for_multi_process_background
@filepath_pattern = "#{
Coverband.configuration.filepath_pattern_for_multi_process ||
File.join(Dir.mktmpdir("coverband_#{Time.now.to_i}"), "coverage")
}.*"
@batch_size = Coverband.configuration.batch_size_for_multi_process || 1000
end

def stop
return unless @thread

@semaphore.synchronize do
if @thread
@thread.exit
@thread = nil
end
end
end

def running?
@thread&.alive?
end

def start
return if running?
return if !@pid.nil?

@pid = Process.pid

logger = Coverband.configuration.logger
@semaphore.synchronize do
return if running?

logger.debug("Coverband: Starting background reporting") if Coverband.configuration.verbose
sleep_seconds = Coverband.configuration.background_reporting_sleep_seconds.to_i
@thread = Thread.new {
Thread.current.name = "Coverband Background Reporter"

loop do
if @pid != Process.pid
logger.debug("Coverband: New process detected, stopping background reporting")
stop
break
end

if Coverband.configuration.reporting_wiggle
sleep_seconds = Coverband.configuration.background_reporting_sleep_seconds.to_i + rand(Coverband.configuration.reporting_wiggle.to_i)
end
# NOTE: Normally as processes first start we immediately report, this causes a redis spike on deploys
# if deferred is set also sleep frst to spread load
sleep(sleep_seconds.to_i) if Coverband.configuration.defer_eager_loading_data?
process_files
if Coverband.configuration.verbose
logger.debug("Coverband: background reporting coverage (#{Coverband.configuration.store.type}). Sleeping #{sleep_seconds}s")
end
sleep(sleep_seconds.to_i) unless Coverband.configuration.defer_eager_loading_data?
end
}
end
rescue ThreadError
stop
end

private

def process_files
files = Dir.glob(@filepath_pattern)
return if files.empty?

Coverband.configuration.logger.debug("Processing #{files.length} coverage files")

files.each_slice(@batch_size) do |batch|
coverage_report = {}

batch.each do |file|
begin
report = JSON.parse(File.read(file))
next if report.nil?
merge_reports(coverage_report, report)
File.delete(file)
rescue JSON::ParserError => e
Coverband.configuration.logger.error("Error parsing file #{file}: #{e.message}")
rescue => e
Coverband.configuration.logger.error("Error processing file #{file}: #{e.message}")
end
end

results = convert_report_to_results(coverage_report)

@store.save_report(results) unless results.empty?
end
end

def merge_reports(first_report, second_report, options = {})
Coverband::Adapters::Base.new.send(
:merge_reports,
first_report,
second_report,
{skip_expansion: true}
)
end

def convert_report_to_results(coverage_report)
coverage_report.each_with_object({}) do |(file, coverage), results|
results[file] = coverage["data"]
end
end
end
end
4 changes: 4 additions & 0 deletions lib/coverband/integrations/resque.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
Coverband.configuration.background_reporting_enabled = false
Coverband::Background.stop
Coverband.report_coverage

Coverband.configuration.store = Coverband::Adapters::FileStore.new(Coverband.configuration.filepath_pattern_for_multi_process)
Coverband::Collectors::Coverage.instance.reset_instance
Coverband::BackgroundForMultiProcess.start
end

module Coverband
Expand Down