Class: Stf::StartDebugSessionInteractor

Inherits:
Object
  • Object
show all
Includes:
ADB, Log
Defined in:
lib/stf/interactor/start_debug_session_interactor.rb

Instance Method Summary collapse

Methods included from Log

#logger, verbose

Instance Method Details

#connect_loop(all_flag: false, wanted: 1, filter: nil, force_filter: false, healthcheck: nil, daemon_mode: false, delay: 5, timeout: 120) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/stf/interactor/start_debug_session_interactor.rb', line 72

def connect_loop(all_flag: false,
                 wanted: 1,

                 filter: nil,
                 force_filter: false,
                 healthcheck: nil,

                 daemon_mode: false,
                 delay: 5,
                 timeout: 120)
  finish_time = Time.now + timeout
  one_time_mode = !daemon_mode

  while true do
    DI[:stop_all_debug_sessions_interactor].disconnect_unwanted_devices(filter, force_filter, healthcheck)

    if one_time_mode && Time.now > finish_time
      raise "Connect loop timeout reached"
    end

    all_devices = DeviceList.new(DI[:stf].get_devices)
    stf_devices = all_devices.select_ready_to_connect
    stf_devices = stf_devices.by_filter(filter) if filter
    stf_devices = stf_devices.select_healthy_for_connect(healthcheck) if healthcheck

    if all_flag
      to_connect = stf_devices.size
    else
      connected = (DI[:android_util].get_present_adb_devices_ids + DI[:connect_ios].ios_serials_to_urls.keys)
      to_connect = wanted - connected.size
    end

    return if one_time_mode && to_connect <= 0

    if to_connect > 0
      if stf_devices.empty?
        logger.error 'There is no available devices with criteria ' + filter
      else
        random_device = stf_devices.asArray.sample
        DI[:start_one_debug_session_interactor].execute(random_device)
        next
      end
    end

    sleep delay
  end
end

#count_connected_devices(filter, force_filter) ⇒ Object



120
121
122
123
124
125
126
# File 'lib/stf/interactor/start_debug_session_interactor.rb', line 120

def count_connected_devices(filter, force_filter)
  stf_devices = DeviceList.new(DI[:stf].get_user_devices)
  stf_devices = stf_devices.by_filter(filter) if filter && force_filter
  connected = (DI[:android_util].map_serials_to_adb_urls.keys + DI[:connect_ios].ios_serials_to_urls.keys)
  connected_filtered = stf_devices.asArray.map {|d| d.serial} & connected
  connected_filtered.size
end

#execute(opts = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/stf/interactor/start_debug_session_interactor.rb', line 16

def execute(opts = {})
  all_flag = opts[:all]
  nodaemon_flag = opts[:nodaemon]
  filter = opts[:filter]
  max_n = opts[:n].to_i > 0 ? opts[:n].to_i : 1
  start_timeout = opts[:starttime].to_i > 0 ? opts[:starttime].to_i : 120
  session = opts[:worktime].to_i > 0 ? opts[:session].to_i : 10800
  min_n = opts[:min].to_s.empty? ? (max_n + 1) / 2 : [opts[:min].to_i, max_n].min
  healthcheck = opts[:health]
  force_filter = opts[:forcefilter]

  DI[:demonizer].kill unless opts[:nokill]

  wanted = nodaemon_flag ? max_n : min_n

  begin
    connect_loop(all_flag: all_flag,
                 wanted: wanted,
                 filter: filter,
                 force_filter: force_filter,
                 healthcheck: healthcheck,
                 delay: 5,
                 timeout: start_timeout)

  rescue SignalException => e
    logger.info "Caught signal \"#{e.message}\""
    DI[:stop_all_debug_sessions_interactor].execute
    return false
  rescue Exception => e
    logger.info "Exception \"#{e.message}\" during initial connect loop"
    DI[:stop_all_debug_sessions_interactor].execute
    return false
  end

  connected_count = count_connected_devices(filter, force_filter)
  logger.info "Number of connected devices: #{connected_count}"

  return true if nodaemon_flag

  # will be daemon here
  DI[:demonizer].run do
    connect_loop(all_flag: all_flag,
                 wanted: max_n,
                 filter: filter,
                 force_filter: force_filter,
                 healthcheck: healthcheck,
                 daemon_mode: true,
                 delay: 30,
                 timeout: session)

    DI[:stop_all_debug_sessions_interactor].execute(byFilter: filter, nokill: true)
  end

  return true
end