Sha256: 300bd08dd3167c841e16abaf83e15140b89d98c034ac3c20d56a6444c0867e29

Contents?: true

Size: 1.85 KB

Versions: 50

Compression:

Stored size: 1.85 KB

Contents

module Actions
  module Middleware
    class KeepCurrentRequestID < Dynflow::Middleware
      def delay(*args)
        pass(*args).tap { store_current_request_id }
      end

      def plan(*args)
        with_current_request_id do
          pass(*args).tap { store_current_request_id }
        end
      end

      def run(*args)
        restore_current_request_id { pass(*args) }
      end

      def finalize
        restore_current_request_id { pass }
      end

      # Run all execution plan lifecycle hooks as the original request_id
      def hook(*args)
        store_current_request_id if !action.input.key?(:current_request_id) && ::Logging.mdc['request']
        restore_current_request_id do
          pass(*args)
        end
      end

      private

      def with_current_request_id
        if action.input[:current_request_id].nil?
          yield
        else
          restore_current_request_id { yield }
        end
      end

      def store_current_request_id
        action.input[:current_request_id] = ::Logging.mdc['request']
      end

      def restore_current_request_id
        if (restored_id = action.input[:current_request_id]).present?
          old_id = ::Logging.mdc['request']
          if old_id.present? && old_id != restored_id
            action.action_logger.warn(_('Changing request id %{request_id} to saved id %{saved_id}') % { :saved_id => restored_id, :request_id => old_id })
          end
          ::Logging.mdc['request'] = restored_id
        end
        yield
      ensure
        # Reset to original request id only when not nil
        # Otherwise, keep the id until it's cleaned in  Dynflow's run_user_code block
        # so that it will stay valid for the rest of the processing of the current step
        # (even outside of the middleware lifecycle)
        ::Logging.mdc['request'] = old_id if old_id.present?
      end
    end
  end
end

Version data entries

50 entries across 50 versions & 1 rubygems

Version Path
foreman-tasks-9.2.3 app/lib/actions/middleware/keep_current_request_id.rb
foreman-tasks-9.2.2 app/lib/actions/middleware/keep_current_request_id.rb
foreman-tasks-9.2.1 app/lib/actions/middleware/keep_current_request_id.rb
foreman-tasks-9.2.0 app/lib/actions/middleware/keep_current_request_id.rb
foreman-tasks-9.1.1 app/lib/actions/middleware/keep_current_request_id.rb
foreman-tasks-9.0.4 app/lib/actions/middleware/keep_current_request_id.rb
foreman-tasks-9.1.0 app/lib/actions/middleware/keep_current_request_id.rb
foreman-tasks-9.0.2 app/lib/actions/middleware/keep_current_request_id.rb
foreman-tasks-9.0.1 app/lib/actions/middleware/keep_current_request_id.rb
foreman-tasks-8.3.3 app/lib/actions/middleware/keep_current_request_id.rb
foreman-tasks-9.0.0 app/lib/actions/middleware/keep_current_request_id.rb
foreman-tasks-8.2.1 app/lib/actions/middleware/keep_current_request_id.rb
foreman-tasks-8.1.4 app/lib/actions/middleware/keep_current_request_id.rb
foreman-tasks-8.3.2 app/lib/actions/middleware/keep_current_request_id.rb
foreman-tasks-8.3.1 app/lib/actions/middleware/keep_current_request_id.rb
foreman-tasks-8.1.3 app/lib/actions/middleware/keep_current_request_id.rb
foreman-tasks-8.3.0 app/lib/actions/middleware/keep_current_request_id.rb
foreman-tasks-8.2.0 app/lib/actions/middleware/keep_current_request_id.rb
foreman-tasks-8.1.2 app/lib/actions/middleware/keep_current_request_id.rb
foreman-tasks-8.1.1 app/lib/actions/middleware/keep_current_request_id.rb