Sha256: 148ee6beaa451511c0bd9c99947bc7719917ef0a20b7565a18cb8d5312dcbf08

Contents?: true

Size: 1.79 KB

Versions: 1

Compression:

Stored size: 1.79 KB

Contents

# frozen_string_literal: true

require_relative "base"

module Sbmt
  module Strangler
    module WorkModes
      class Replace < Base
        include Dry::Monads::Result::Mixin

        def call
          mirror_call_result = mirror_call
          track_mirror_call(mirror_call_result.success?)

          unless mirror_call_result.success?
            render(
              json: {error: "Mirror lambda call failed!"},
              status: :internal_server_error
            ) # TODO: Возможно стоит сделать фолбэк на проксирование?
            return
          end

          render_call_result = render_call(mirror_call_result.value!)
          track_render_call(render_call_result.success?)

          unless render_call_result.success?
            render(
              json: {error: "Render lambda call failed!"},
              status: :internal_server_error
            ) # TODO: Возможно стоит сделать фолбэк на проксирование?
            return
          end

          render render_call_result.value!
        end

        private

        delegate :render, to: :rails_controller
        delegate :track_mirror_call, :track_render_call, to: :metric_tracker

        def mirror_call
          value = strangler_action.mirror.call(rails_controller)
          Success(value)
        rescue => err
          handle_error(err)
          Failure(nil)
        end

        def render_call(mirror_result)
          value = strangler_action.render.call(mirror_result)
          Success(value)
        rescue => err
          handle_error(err)
          Failure(nil)
        end

        def handle_error(err)
          Sbmt::Strangler.error_tracker.error(err)
          Sbmt::Strangler.logger.error(err)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sbmt-strangler-0.9.1 lib/sbmt/strangler/work_modes/replace.rb