class Gruf::Cli::Executor

Handles execution of the gruf binstub, along with command-line arguments

Public Class Methods

new( args = ARGV, server: nil, services: nil, hook_executor: nil, logger: nil ) click to toggle source

@param [Hash|ARGV]

# File lib/gruf/cli/executor.rb, line 29
def initialize(
  args = ARGV,
  server: nil,
  services: nil,
  hook_executor: nil,
  logger: nil
)
  @args = args
  setup! # ensure we set some defaults from CLI here so we can allow configuration
  @services = services || Gruf.services
  @hook_executor = hook_executor || Gruf::Hooks::Executor.new(hooks: Gruf.hooks&.prepare)
  @server = server || Gruf::Server.new(Gruf.server_options)
  @logger = logger || Gruf.logger || ::Logger.new($stderr)
end

Public Instance Methods

run() click to toggle source

Run the server

# File lib/gruf/cli/executor.rb, line 47
def run
  exception = nil
  begin
    @services.each { |s| @server.add_service(s) }
    @hook_executor.call(:before_server_start, server: @server)
    @server.start!
  rescue StandardError => e
    exception = e
    # Catch the exception here so that we always ensure the post hook runs
    # This allows systems wanting to provide external server instrumentation
    # the ability to properly handle server failures
    @logger.fatal("FATAL ERROR: #{e.message} #{e.backtrace.join("\n")}")
  end
  @hook_executor.call(:after_server_stop, server: @server)
  raise exception if exception
end

Private Instance Methods

parse_options() click to toggle source

Parse all CLI arguments into an options result

@return [Slop::Result]

# File lib/gruf/cli/executor.rb, line 85
def parse_options
  ::Slop.parse(@args) do |o|
    o.null '-h', '--help', 'Display help message' do
      puts o
      exit(0)
    end
    o.string '--host', 'Specify the binding url for the gRPC service'
    o.bool '--suppress-default-interceptors', 'Do not use the default interceptors'
    o.bool '--backtrace-on-error', 'Push backtraces on exceptions to the error serializer'
    o.null '-v', '--version', 'print gruf version' do
      puts Gruf::VERSION
      exit(0)
    end
  end
end
setup!() click to toggle source

Setup options for CLI execution and configure Gruf based on inputs

# File lib/gruf/cli/executor.rb, line 69
def setup!
  opts = parse_options

  Gruf.server_binding_url = opts[:host] if opts[:host]
  if opts.suppress_default_interceptors?
    Gruf.interceptors.remove(Gruf::Interceptors::ActiveRecord::ConnectionReset)
    Gruf.interceptors.remove(Gruf::Interceptors::Instrumentation::OutputMetadataTimer)
  end
  Gruf.backtrace_on_error = true if opts.backtrace_on_error?
end