Class: Sbmt::Pact::Provider::GrufServer

Inherits:
Object
  • Object
show all
Defined in:
lib/sbmt/pact/provider/gruf_server.rb

Overview

inspired by Gruf::Cli::Executor

Constant Summary collapse

SERVER_STOP_TIMEOUT_SEC =
15

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ GrufServer

Returns a new instance of GrufServer.



10
11
12
13
14
15
16
17
18
19
# File 'lib/sbmt/pact/provider/gruf_server.rb', line 10

def initialize(options = {})
  @options = options

  setup!

  @server_pid = nil

  @services = @options[:services].is_a?(Array) ? @options[:services] : []
  @logger = @options[:logger] || ::Logger.new($stdout)
end

Instance Method Details

#runObject

Run the server



50
51
52
53
54
55
56
57
58
59
# File 'lib/sbmt/pact/provider/gruf_server.rb', line 50

def run
  start

  yield
rescue => e
  @logger.fatal("FATAL ERROR: #{e.message} #{e.backtrace.join("\n")}")
  raise
ensure
  stop
end

#startObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sbmt/pact/provider/gruf_server.rb', line 21

def start
  raise "server already running, stop server before starting new one" if @thread

  @logger.info("[gruf] starting standalone server with options: #{@options}")

  @server = Gruf::Server.new(Gruf.server_options)
  @services.each { |s| @server.add_service(s) } if @services.any?
  @thread = Thread.new do
    @logger.debug "[gruf] starting grpc server"
    @server.start!
  end
  @server.server.wait_till_running(10)

  @logger.info("[gruf] standalone server started")
end

#stopObject



37
38
39
40
41
42
43
44
45
# File 'lib/sbmt/pact/provider/gruf_server.rb', line 37

def stop
  @logger.info("[gruf] stopping standalone server")

  @server&.server&.stop
  @thread&.join(SERVER_STOP_TIMEOUT_SEC)
  @thread&.kill

  @logger.info("[gruf] standalone server stopped")
end