Sha256: c9b182dd8c4be81443a942da0e9c6a28d39bf9b049c09046e356544c7fd0df63

Contents?: true

Size: 1.72 KB

Versions: 4

Compression:

Stored size: 1.72 KB

Contents

require 'open3'
require 'json'
require 'mkmf'
require 'net/http'

module TrueAutomation
  class Client
    @pid = nil

    def start(options)
      @port = options[:port]
      remote = options[:remote]
      ta_debug = options[:ta_debug]

      if options[:ta_service]
        driver_path = " --driver #{options[:ta_service]}"
      elsif options[:driver]
        driver_path = " --driver #{options[:driver]}"
        driver_path += " --driver-version #{options[:driver_version]}" if options[:driver_version]
      end

      @executable = ENV['TRUEAUTOMATION_EXEC'] || 'trueautomation'

      if find_executable(@executable).nil?
        raise "`#{@executable}` not found. Can not find TrueAutomation.IO client"
      end

      trueautomation_version = `#{@executable} --version`
      puts "TrueAutomation.IO client #{trueautomation_version.strip}"

      Dir.mkdir('log') unless File.exist?('log')
      logfile = "log/trueautomation-#{Time.now.strftime('%Y%m%dT%H%M%S')}.log"

      @pid = spawn("#{@executable} --log-file #{logfile} --port #{@port}#{driver_path}#{remote}#{ta_debug}")
      puts "Started TrueAutomation.IO client with pid #{@pid} listening to port #{@port}"

      @pid
    end

    def stop
      begin
        if @pid
          puts "Stopping TrueAutomation.IO Client with pid #{@pid}"
          uri = URI("http://localhost:#{@port}/shutdown")
          Net::HTTP.get(uri)

          @pid = nil
        end
      rescue
      end
    end

    def wait_until_start
      counter = 0
      loop do
        break if check_connection or counter >= 10
        counter += 1
        sleep 2
      end
    end

    private

    def check_connection
      Socket.tcp('localhost', @port, connect_timeout: 2) { true } rescue false
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
true_automation-0.5.9 lib/true_automation/client.rb
true_automation-0.5.6 lib/true_automation/client.rb
true_automation-0.5.5 lib/true_automation/client.rb
true_automation-0.5.4 lib/true_automation/client.rb