# Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details. # frozen_string_literal: true cs__scoped_require 'contrast/components/interface' cs__scoped_require 'contrast/agent/service_heartbeat' module Contrast module Agent # This class used to ensure that our worker threads are running in multi-process environments class ThreadWatcher include Contrast::Components::Interface access_component :logging attr_reader :heartbeat def initialize @pids = {} @heartbeat = Contrast::Agent::ServiceHeartbeat.new end def startup! unless heartbeat.running? logger.debug('Attempting to start heartbeat thread') heartbeat.start_thread! end heartbeat_result = heartbeat.running? logger.debug('Heartbeat thread status', alive: heartbeat_result) unless Contrast::Agent.messaging_queue.running? logger.debug('Attempting to start messaging queue thread') Contrast::Agent.messaging_queue.start_thread! end messaging_result = Contrast::Agent.messaging_queue.running? logger.debug('Messaging thread status', alive: messaging_result) logger.debug('ThreadWatcher started threads') @pids[Process.pid] = messaging_result && heartbeat_result end def ensure_running? return if @pids[Process.pid] == true logger.debug('ThreadWatcher - threads not running') startup! end end end end