Module: Contrast::Api::Communication::ServiceLifecycle

Includes:
Components::Interface
Included in:
Speedracer
Defined in:
lib/contrast/api/communication/service_lifecycle.rb

Overview

Handles local service startup

Instance Method Summary collapse

Methods included from Components::Interface

included

Instance Method Details

#attempt_local_service_startupObject



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/contrast/api/communication/service_lifecycle.rb', line 12

def attempt_local_service_startup
  zombie_check
  service_starter_thread.join(5)
  is_service_started = Contrast::Utils::OS.running?
  if is_service_started
    logger.info('The bundled service was successfully started.')
  else
    logger.error('The bundled service could not be started. The agent will not function properly.')
  end
  is_service_started
end

#determine_startup_optionsObject



33
34
35
36
37
38
# File 'lib/contrast/api/communication/service_lifecycle.rb', line 33

def determine_startup_options
  return { out: :out, err: :out } if CONTRAST_SERVICE.logger_path == 'STDOUT'
  return { out: :err, err: :err } if CONTRAST_SERVICE.logger_path == 'STDERR'

  { out: File::NULL, err: File::NULL }
end

#service_starter_threadObject



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/contrast/api/communication/service_lifecycle.rb', line 47

def service_starter_thread
  Contrast::Agent::Thread.new do
    # Always check to see if it already started
    unless Contrast::Utils::OS.running?
      # Spawn the service process
      spawn_service
      # Block until service is running
      sleep(0.1) until Contrast::Utils::OS.running?
    end
  end
end

#spawn_serviceObject

This is a separate method so we can overwrite it globally in specs



41
42
43
44
45
# File 'lib/contrast/api/communication/service_lifecycle.rb', line 41

def spawn_service
  options = determine_startup_options
  logger.debug('Spawning service')
  spawn 'contrast_service', options
end

#zombie_checkObject

check if there’s a zombie service that exists, and wait on it if so. currently, this only happens when trying to initialize speedracer



26
27
28
29
30
31
# File 'lib/contrast/api/communication/service_lifecycle.rb', line 26

def zombie_check
  zombie_pid_list = Contrast::Utils::OS.zombie_pids
  zombie_pid_list.each do |pid|
    Process.wait(pid.to_i)
  end
end