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

Defined Under Namespace

Classes: NoServicesBoundError

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)
  • server (::Gruf::Server|NilClass) (defaults to: nil)
  • services (Array<Class>|NilClass) (defaults to: nil)
  • hook_executor (Gruf::Hooks::Executor|NilClass) (defaults to: nil)
  • logger (Logger|NilClass) (defaults to: nil)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/gruf/cli/executor.rb', line 35

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.is_a?(Array) ? 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



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/gruf/cli/executor.rb', line 53

def run
  exception = nil

  # allow lazy registering globally as late as possible, this allows more flexible binstub injections
  register_services!

  begin
    @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