Sha256: f32d1924861f9ae607befbf8f4e0598bfc34c86c387308a7096980b5e2e9a7a2

Contents?: true

Size: 1.77 KB

Versions: 1

Compression:

Stored size: 1.77 KB

Contents

require 'ADB'

require 'stf/client'
require 'stf/log/log'
require 'stf/errors'
require 'stf/model/session'

class StartDebugSessionInteractor

  include Log
  include ADB

  def initialize(stf)
    @stf = stf
  end

  def execute
    randomized_device = nil

    1..10.times do
      begin
        devices = @stf.get_devices
        if devices.nil? || (devices.is_a?(Array) && devices.empty?)
          logger.info 'No devices connected to STF. Retrying'
          sleep 5
          next
        end
        usable_devices = devices.select{ |d| d.ready == true && d.present == true && d.using == false }
        if usable_devices.empty?
          logger.error 'All devices are being used. Retrying'
          sleep 5
          next
        end

        randomized_device = usable_devices.sample
        raise new DeviceNotAvailableError if randomized_device.nil?

        serial  = randomized_device.serial
        success = @stf.add_device serial
        if success
          logger.info "Device #{serial} added"
        elsif logger.error "Can't add device #{serial}. Retrying"
          next
        end

        result = @stf.start_debug serial
        if !result.success
          logger.error "Can't start debugging session for device #{serial}. Retrying"
          @stf.remove_device serial
          next
        end

        execute_adb_with 30, "connect #{result.remoteConnectUrl}"

        return Session.new(serial, result.remoteConnectUrl)
        break

        raise new DeviceNotAvailableError
      rescue DeviceNotAvailableError
        logger.error 'Failed to start debug session. Retrying...'
        next
      end
    end
  end
end

StartDebugSessionInteractor.new(Stf::Client.new('https://stf.default.svc.agoda.mobi', 'c8c62e3503d246b68d6683c47bc1414a80b9527185494c8ea811f874e9fdb7d2')).execute

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
stf-client-0.1.3 lib/stf/interactor/start_debug_session_interactor.rb