Class: Gruf::Cli::Executor

Inherits:
Object
  • Object
show all
Defined in:
lib/gruf/cli/executor.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(args = ARGV, server: nil, services: nil, hook_executor: nil, logger: nil) ⇒ Executor

Returns a new instance of Executor.

Parameters:

  • (Hash|ARGV)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
# 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

Instance Method Details

#runObject

Run the server



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 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