Class: Gruf::Controllers::Base

Inherits:
Object
  • Object
show all
Includes:
Errors::Helpers
Defined in:
lib/gruf/controllers/base.rb

Overview

Base controller object for Gruf gRPC requests

Direct Known Subclasses

HealthController

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Errors::Helpers

#fail!

Constructor Details

#initialize(method_key:, service:, rpc_desc:, active_call:, message:) ⇒ Base

Initialize the controller within the given request context

Parameters:

  • method_key (Symbol)

    The gRPC method that this controller relates to

  • service (GRPC::GenericService)

    The gRPC service stub for this controller

  • rpc_desc (GRPC::RpcDesc)

    The RPC descriptor for this service method

  • active_call (GRPC::ActiveCall)

    The gRPC ActiveCall object

  • message (Google::Protobuf::MessageExts)

    The incoming protobuf request message



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/gruf/controllers/base.rb', line 48

def initialize(method_key:, service:, rpc_desc:, active_call:, message:)
  @request = Request.new(
    method_key: method_key,
    service: service,
    rpc_desc: rpc_desc,
    active_call: active_call,
    message: message
  )
  @error = Gruf::Error.new
  @interceptors = Gruf.interceptors.prepare(@request, @error)
end

Class Attribute Details

.bound_serviceObject (readonly)

Returns the value of attribute bound_service.



36
37
38
# File 'lib/gruf/controllers/base.rb', line 36

def bound_service
  @bound_service
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



31
32
33
# File 'lib/gruf/controllers/base.rb', line 31

def error
  @error
end

#requestObject (readonly)

Returns the value of attribute request.



28
29
30
# File 'lib/gruf/controllers/base.rb', line 28

def request
  @request
end

Class Method Details

.bind(service) ⇒ Object

Bind the controller to the given service and add it to the service registry

Parameters:

  • service (GRPC::GenericService)

    The name of the service to bind this controller to



65
66
67
68
69
70
71
72
73
# File 'lib/gruf/controllers/base.rb', line 65

def self.bind(service)
  service_class = service.name.constantize
  ::Gruf.logger.debug "[gruf] Binding #{service_class} to #{name}"
  ::Gruf.services << service_class
  # rubocop:disable ThreadSafety/InstanceVariableInClassMethod
  @bound_service = service_class
  # rubocop:enable ThreadSafety/InstanceVariableInClassMethod
  ServiceBinder.bind!(service: service_class, controller: self)
end

Instance Method Details

#call(method_key, &block) ⇒ Object

Call a method on this controller

Parameters:

  • method_key (Symbol)

    The name of the gRPC service method being called as a Symbol

  • &block (block)

    The passed block for executing the method



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/gruf/controllers/base.rb', line 92

def call(method_key, &block)
  Interceptors::Context.new(@interceptors).intercept! do
    process_action(method_key, &block)
  end
rescue GRPC::BadStatus
  raise # passthrough, to be caught by Gruf::Interceptors::Timer
rescue GRPC::Core::CallError, StandardError => e # CallError is not a StandardError
  set_debug_info(e.message, e.backtrace) if Gruf.backtrace_on_error
  error_message = Gruf.use_exception_message ? e.message : Gruf.internal_error_message
  fail!(:internal, :unknown, error_message)
end

#process_action(method_key, &block) ⇒ Object

Call a method on this controller. Override this in a subclass to modify the behavior around processing a method

Parameters:

  • method_key (Symbol)

    The name of the gRPC service method being called as a Symbol

  • &block (block)

    The passed block for executing the method



82
83
84
# File 'lib/gruf/controllers/base.rb', line 82

def process_action(method_key, &block)
  send(method_key, &block)
end