Sha256: eca609d252eafe8f7ab772e01c28f5535a70951c2e64e7ccc3481f81c0fc2ddb

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

module Nonnative
  class Pool
    def initialize(configuration)
      @configuration = configuration
    end

    def start(&block)
      all = processes + servers
      process_all(all, :start, :open?, &block)
    end

    def stop(&block)
      all = processes + servers
      process_all(all, :stop, :closed?, &block)
    end

    private

    attr_reader :configuration

    def processes
      @processes ||= configuration.processes.map do |d|
        [Nonnative::System.new(d), Nonnative::Port.new(d)]
      end
    end

    def servers
      @servers ||= configuration.servers.map do |d|
        [d.klass.new(d.port), Nonnative::Port.new(d)]
      end
    end

    def process_all(all, pr_method, po_method, &block)
      prs = []
      ths = []

      all.each do |pr, po|
        prs << pr.send(pr_method)
        ths << Thread.new { po.send(po_method) }
      end

      ThreadsWait.all_waits(*ths)

      pos = ths.map(&:value)

      yield_results(prs, pos, &block)
    end

    def yield_results(prs, pos)
      prs.zip(pos).each do |id, result|
        yield id, result
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nonnative-1.3.0 lib/nonnative/pool.rb