Module: WifiWand::NetworkConnectivityProbeHelper

Defined in:
lib/wifi_wand/services/network_connectivity_probe_helper.rb

Constant Summary collapse

HELPER_PROCESS_BOUNDARY_ERROR =

StandardError excludes process-control and VM-level exceptions like Interrupt, SystemExit, and NoMemoryError.

StandardError

Class Method Summary collapse

Class Method Details

.parallel_probe_result(tester, mode, items, overall_timeout) ⇒ Object



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
# File 'lib/wifi_wand/services/network_connectivity_probe_helper.rb', line 43

def self.parallel_probe_result(tester, mode, items, overall_timeout)
  return batch_result(false, false, []) if items.empty?

  result_queue = Queue.new
  threads = items.map { |item| start_probe_thread(tester, mode, item, result_queue) }
  deadline = current_time + overall_timeout
  pending_results = threads.length
  probe_results = []

  while pending_results.positive?
    remaining_time = deadline - current_time
    return batch_result(false, true, probe_results) if remaining_time <= 0

    result = result_queue.pop(timeout: remaining_time)
    return batch_result(false, true, probe_results) if result.nil?

    pending_results -= 1
    probe_results << result
    return batch_result(true, false, probe_results) if result[:success]
  end

  batch_result(false, false, probe_results)
ensure
  cleanup_threads(threads || [])
end

.parse_argv(argv) ⇒ Object

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
# File 'lib/wifi_wand/services/network_connectivity_probe_helper.rb', line 11

def self.parse_argv(argv)
  mode_arg, items_json, timeout_arg = argv
  mode = parse_mode(mode_arg)
  items = JSON.parse(items_json, symbolize_names: true)
  raise ArgumentError, 'probe items must be an array' unless items.is_a?(Array)

  { mode: mode, items: items, timeout: Float(timeout_arg) }
end

.run(argv, output: $stdout, tester: nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/wifi_wand/services/network_connectivity_probe_helper.rb', line 20

def self.run(argv, output: $stdout, tester: nil)
  probe = parse_argv(argv)
  tester ||= NetworkConnectivityTester.new(verbose: false, output: $stderr)
  result = parallel_probe_result(
    tester,
    probe[:mode],
    probe[:items],
    probe[:timeout]
  )
  output.print(JSON.generate(result))
  output.flush
rescue HELPER_PROCESS_BOUNDARY_ERROR => e
  # Subprocess boundary: always return JSON so the parent can treat helper
  # failures as indeterminate connectivity instead of hanging on bad output.
  output.print(JSON.generate(
    success:       false,
    timed_out:     false,
    error_class:   e.class.to_s,
    error_message: e.message
  ))
  output.flush
end