Sha256: 394cf58cd6ef6b83976112cbc250954c6d9d9d3d796ef7e1d6e5f268e7342f22

Contents?: true

Size: 1.64 KB

Versions: 2

Compression:

Stored size: 1.64 KB

Contents

# Copyright (c) 2022 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
# frozen_string_literal: true

require 'contrast/components/logger'

module Contrast
  module Agent
    # This module adds an at_exit hook for us to send messages that may be lost at process exit
    module AtExitHook
      extend Contrast::Components::Logger::InstanceMethods

      def self.exit_hook
        @_exit_hook ||= begin
          at_exit do
            on_exit
          end
          true
        end
      end

      # Actions to take when a process exits. Typically called from our
      # exit_hook, but exposed here for other process terminations, like those
      # in Kernel#exec
      def self.on_exit
        logger.debug('at_exit invoked, host application terminating',
                     p_id: @pid,
                     pp_id: @ppid,
                     process_pid: Process.pid,
                     process_pp_id: Process.ppid)

        context = Contrast::Agent::REQUEST_TRACKER.current
        return unless context

        if Contrast::Agent::Reporter.enabled?
          [
            context.new_observed_route,
            Contrast::Agent::Reporting::DtmMessage.dtm_to_event(context.server_activity),
            Contrast::Agent::Reporting::DtmMessage.dtm_to_event(context.activity.library_usages),
            Contrast::Agent::Reporting::DtmMessage.dtm_to_event(context.activity)
          ].each do |event|
            Contrast::Agent.reporter&.send_event_immediately(event)
          end
        else
          Contrast::Agent.messaging_queue&.send_event_immediately(context.activity)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
contrast-agent-6.1.2 lib/contrast/agent/at_exit_hook.rb
contrast-agent-6.1.1 lib/contrast/agent/at_exit_hook.rb