class RSpecBackgroundProcess::ProcessPool

Attributes

options[R]
pool[R]

Public Class Methods

new(options) click to toggle source
# File lib/rspec-background-process/process_pool.rb, line 243
def initialize(options)
        @stats = {}

        @max_running = options.delete(:max_running) || 4

        @pool = LRUPool.new(@max_running) do |key, instance|
                #puts "too many instances running, stopping: #{instance.name}[#{key}]; #{@pool}"
                stats(instance.name)[:lru_stopped] += 1
                instance.stop
        end

        # keep track of running instances
        @pool.after_store do |key, instance|
                instance.after_state_change do |new_state|
                        # we mark running before it is actually started to have a chance to stop over-limit instance first
                        if new_state == :starting
                                #puts "new instance running: #{instance.name}[#{key}]"
                                @pool.running(key)
                                stats(instance.name)[:started] += 1
                        end
                        @pool.not_running(key) if [:not_running, :dead, :jammed].include? new_state
                end

                # mark running if added while already running
                @pool.running(key) if instance.running?

                # init stats
                stats(instance.name)[:started] ||= 0
                stats(instance.name)[:lru_stopped] ||= 0
        end

        # for storing shared data
        @global_context = {}

        # for filling template strings with actual instance data
        @template_renderer = ->(variables, string) {
                out = string.dup
                variables.merge(
                        /project directory/ => -> { Dir.pwd.to_s }
                ).each do |regexp, source|
                        out.gsub!(/<#{regexp}>/) do
                                source.call(*$~.captures)
                        end
                end
                out
        }

        # this are passed down to instance
        @options = options.merge(
                global_context:  @global_context,
                template_renderer: @template_renderer
        )
end

Public Instance Methods

cleanup() click to toggle source
# File lib/rspec-background-process/process_pool.rb, line 304
def cleanup
        @pool.reset_active
end
failed_instance() click to toggle source
# File lib/rspec-background-process/process_pool.rb, line 324
def failed_instance
        @pool.instances.select do |instance|
                instance.dead? or
                instance.failed? or
                instance.jammed?
        end.sort_by do |instance|
                instance.state_change_time
        end.last
end
logging_enabled?() click to toggle source
# File lib/rspec-background-process/process_pool.rb, line 300
def logging_enabled?
        @options[:logging]
end
report_failed_instance() click to toggle source
# File lib/rspec-background-process/process_pool.rb, line 334
def report_failed_instance
        if failed_instance
                puts "Last failed process instance state log: "
                failed_instance.state_log.each do |log_line|
                        puts "\t#{log_line}"
                end
                puts "Working directory: #{failed_instance.working_directory}"
                puts "Log file: #{failed_instance.log_file}"
                puts "State: #{failed_instance.state}"
                puts "Exit code: #{failed_instance.exit_code}"
        else
                puts "No process instance in failed state"
        end
end
report_logs() click to toggle source
# File lib/rspec-background-process/process_pool.rb, line 349
def report_logs
        puts "Process instance logs:"
        @pool.instances.each do |instance|
                puts "#{instance.name}: #{instance.log_file}"
        end
end
report_stats() click to toggle source
# File lib/rspec-background-process/process_pool.rb, line 312
def report_stats
        puts
        puts "Process pool stats (max running: #{@max_running}):"
        @stats.each do |key, stats|
                puts "  #{key}: #{stats.map{|k, v| "#{k}: #{v}"}.join(' ')}"
        end
        puts "Total instances: #{@stats.length}"
        puts "Total starts: #{@stats.reduce(0){|total, stat| total += stat.last[:started]}}"
        puts "Total LRU stops: #{@stats.reduce(0){|total, stat| total += stat.last[:lru_stopped]}}"
        puts "Total extra LRU stops: #{@stats.reduce(0){|total, stat| extra = (stat.last[:lru_stopped] - 1); total += extra if extra > 0; total}}"
end
stats(name) click to toggle source
# File lib/rspec-background-process/process_pool.rb, line 308
def stats(name)
        @stats[name] ||= {}
end