Sha256: 7195b1b7551bb712c1557cb755ba5f101f32e7ce5cedf423b5ad6d4a5c032315

Contents?: true

Size: 1.68 KB

Versions: 17

Compression:

Stored size: 1.68 KB

Contents

# frozen_string_literal: true

module Coverband
  class Background
    @semaphore = Mutex.new
    @thread = nil

    def self.stop
      return unless @thread

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

    def self.running?
      @thread&.alive?
    end

    def self.start
      return if running?

      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 {
          loop do
            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?
            Coverband.report_coverage
            Coverband.configuration.trackers.each { |tracker| tracker.save_report }
            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
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
coverband-6.1.2 lib/coverband/integrations/background.rb
coverband-6.1.2.rc.1 lib/coverband/integrations/background.rb
coverband-6.1.1 lib/coverband/integrations/background.rb
coverband-6.1.0 lib/coverband/integrations/background.rb
coverband-6.0.3.rc.4 lib/coverband/integrations/background.rb
coverband-6.0.3.rc.3 lib/coverband/integrations/background.rb
coverband-6.0.3.rc.2 lib/coverband/integrations/background.rb
coverband-6.0.3.rc.1 lib/coverband/integrations/background.rb
coverband-6.0.2 lib/coverband/integrations/background.rb
coverband-6.0.1 lib/coverband/integrations/background.rb
coverband-6.0.1.rc.1 lib/coverband/integrations/background.rb
coverband-6.0.0 lib/coverband/integrations/background.rb
coverband-5.2.6.rc.6 lib/coverband/integrations/background.rb
coverband-5.2.6.rc.5 lib/coverband/integrations/background.rb
coverband-5.2.6.rc.4 lib/coverband/integrations/background.rb
coverband-5.2.6.rc.3 lib/coverband/integrations/background.rb
coverband-5.2.6.rc.2 lib/coverband/integrations/background.rb