Sha256: 97eea373ed642256f256be91ec41b93a9a61fb80528c12ce0fd93617e0b90b70

Contents?: true

Size: 1.37 KB

Versions: 7

Compression:

Stored size: 1.37 KB

Contents

module Datadog
  module Utils
    # Helper methods for managing forking behavior
    module Forking
      def self.included(base)
        base.send(:prepend, ClassExtensions) if base.is_a?(Class)
      end

      def self.extended(base)
        # Explicitly update PID here because there's a case where
        # the code path that lazily updates the PID may not be exercised
        # until after a fork occurs, thus causing the event to be missed.
        # By eagerly setting this, we avoid this scenario.
        base.update_fork_pid!
      end

      def after_fork!
        if forked?
          yield
          update_fork_pid!
          true
        else
          false
        end
      end

      def forked?
        Process.pid != fork_pid
      end

      def update_fork_pid!
        @fork_pid = Process.pid
      end

      def fork_pid
        @fork_pid ||= Process.pid
      end

      # Adds additional functionality for Classes that implement Forking
      module ClassExtensions
        # Addresses an edge case where forking before invoking #update_fork_pid! on the
        # object will cause forking to not be detected in the fork when it should have.
        #
        # This wrapper prevents this by initializing the fork PID when the object is created.
        def initialize(*args, &block)
          super
          update_fork_pid!
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
ddtrace-0.49.0 lib/ddtrace/utils/forking.rb
ddtrace-0.48.0 lib/ddtrace/utils/forking.rb
ddtrace-0.47.0 lib/ddtrace/utils/forking.rb
ddtrace-0.46.0 lib/ddtrace/utils/forking.rb
ddtrace-0.45.0 lib/ddtrace/utils/forking.rb
ddtrace-0.44.0 lib/ddtrace/utils/forking.rb
ddtrace-0.43.0 lib/ddtrace/utils/forking.rb