class RSpecBackgroundProcess::ProcessPool::ProcessDefinition

Attributes

group[RW]
path[R]

Public Class Methods

new(pool, group, path, type, options) click to toggle source
# File lib/rspec-background-process/process_pool.rb, line 11
def initialize(pool, group, path, type, options)
        @pool = pool
        @group = group
        @path = path
        @type = type

        @extensions = Set.new
        @options = {
                ready_timeout: 10,
                term_timeout: 10,
                kill_timeout: 10,
                ready_test: ->(p){fail 'no readiness check defined'},
                refresh_action: ->(p){p.restart},
                logging: false
        }.merge(options)
        @working_directory = nil
        @arguments = []
end

Public Instance Methods

argument(*value) click to toggle source
# File lib/rspec-background-process/process_pool.rb, line 87
def argument(*value)
        @arguments += value
end
arguments() click to toggle source
# File lib/rspec-background-process/process_pool.rb, line 83
def arguments
        @arguments
end
extend(mod, options = {}) click to toggle source
# File lib/rspec-background-process/process_pool.rb, line 46
def extend(mod, options = {})
        @extensions << mod
        @options.merge! options
end
http_port_allocated_form(base_port, port_count = 1) click to toggle source
# File lib/rspec-background-process/server.rb, line 4
def http_port_allocated_form(base_port, port_count = 1)
        extend RSpecBackgroundProcess::BackgroundProcess::Server, base_port: base_port, port_count: port_count
end
initialize_copy(old) click to toggle source
# File lib/rspec-background-process/process_pool.rb, line 33
def initialize_copy(old)
        # need own copy
        @extensions = @extensions.dup
        @options = @options.dup
        @arguments = @arguments.dup
end
instance() click to toggle source
# File lib/rspec-background-process/process_pool.rb, line 91
def instance
        # disallow changes to the definition once we have instantiated
        @options.freeze
        @arguments.freeze
        @working_directory.freeze
        @extensions.freeze

        # instance is requested
        # we calculate key based on current definition
        _key = key

        # already crated
        if instance = @pool[_key]
                # always make sure options are up to date with definition
                instance.reset_options(@options)
                return instance
        end

        # can only use parts of the key for instance name
        name = Pathname.new(@path).basename

        # need to crate new one
        instance = @type.new(
                "#{@group}-#{name}-#{_key}",
                @path,
                @arguments,
                @working_directory || [name, _key],
                @options
        )

        # ports get allocated here...
        @extensions.each do |mod|
                instance.extend(mod)
        end

        @pool[_key] = instance
end
key() click to toggle source
# File lib/rspec-background-process/process_pool.rb, line 134
def key
        hash = Digest::SHA256.new
        hash.update @group.to_s
        hash.update @path.to_s
        hash.update @type.name
        @extensions.each do |mod|
                hash.update mod.name
        end
        hash.update @working_directory.to_s
        @arguments.each do |argument|
                case argument
                when Pathname
                        begin
                                # use file content as part of the hash
                                hash.update argument.read
                        rescue Errno::ENOENT
                                # use file name if it does not exist
                                hash.update argument.to_s
                        end
                else
                        hash.update argument.to_s
                end
        end
        Digest.hexencode(hash.digest)[0..16]
end
kill_timeout(seconds) click to toggle source
# File lib/rspec-background-process/process_pool.rb, line 75
def kill_timeout(seconds)
        @options[:kill_timeout] = seconds
end
logging_enabled() click to toggle source
# File lib/rspec-background-process/process_pool.rb, line 51
def logging_enabled
        @options[:logging] = true
end
logging_enabled?() click to toggle source
# File lib/rspec-background-process/process_pool.rb, line 55
def logging_enabled?
        @options[:logging]
end
ready_test(&block) click to toggle source
# File lib/rspec-background-process/process_pool.rb, line 59
def ready_test(&block)
        @options[:ready_test] = block
end
ready_timeout(seconds) click to toggle source
# File lib/rspec-background-process/process_pool.rb, line 67
def ready_timeout(seconds)
        @options[:ready_timeout] = seconds
end
ready_when_log_includes(log_line) click to toggle source
# File lib/rspec-background-process/readiness_checks.rb, line 10
def ready_when_log_includes(log_line)
        ready_test do |instance|
                log_line = instance.render(log_line)

                # NOTE: log file my not be crated just after process is started (spawned) so we need to retry
                with_retries(
                        max_tries: 10000,
                        base_sleep_seconds: 0.01,
                        max_sleep_seconds: 0.2,
                        rescue: Errno::ENOENT
                ) do
                        File::Tail::Logfile.tail(instance.log_file, forward: 0, interval: 0.01, max_interval: 1, suspicious_interval: 4) do |line|
                                line.include?(log_line) and break true
                        end
                end
        end
end
ready_when_url_response_status(uri, status = 'OK') click to toggle source
# File lib/rspec-background-process/readiness_checks.rb, line 28
def ready_when_url_response_status(uri, status = 'OK')
        ready_test do |instance|
                _uri = instance.render(uri) # NOTE: new variable (_uri) is needed or strange things happen...

                begin
                        with_retries(
                        max_tries: 10000,
                        base_sleep_seconds: 0.06,
                        max_sleep_seconds: 0.2,
                        rescue: Errno::ECONNREFUSED
                        ) do
                                open(_uri).status.last.strip == status and break true
                        end
                end
        end
end
refresh_action(&block) click to toggle source
# File lib/rspec-background-process/process_pool.rb, line 63
def refresh_action(&block)
        @options[:refresh_action] = block
end
refresh_command(command) click to toggle source
# File lib/rspec-background-process/refresh_actions.rb, line 6
def refresh_command(command)
        refresh_action do |instance|
                _command = instance.render(command)
                system _command
        end
end
start() click to toggle source

shortcut

# File lib/rspec-background-process/process_pool.rb, line 130
def start
        instance.start
end
term_timeout(seconds) click to toggle source
# File lib/rspec-background-process/process_pool.rb, line 71
def term_timeout(seconds)
        @options[:term_timeout] = seconds
end
with() { |process| ... } click to toggle source
# File lib/rspec-background-process/process_pool.rb, line 40
def with
        process = dup
        yield process
        process
end
working_directory(dir) click to toggle source
# File lib/rspec-background-process/process_pool.rb, line 79
def working_directory(dir)
        @working_directory = dir
end