Class: Nonnative::Server

Inherits:
Runner
  • Object
show all
Defined in:
lib/nonnative/server.rb

Overview

Runtime runner that manages an in-process Ruby server.

A server runner:

  • starts a Ruby thread that runs #perform_start,
  • waits briefly (via the runner wait), and
  • participates in readiness/shutdown via TCP port checks orchestrated by Pool.

Concrete server implementations are expected to subclass Server and implement:

  • #perform_start (to bind/listen and begin serving), and
  • #perform_stop (to gracefully shut down).

The underlying configuration is a ConfigurationServer.

Direct Known Subclasses

GRPCServer, HTTPServer

Instance Method Summary collapse

Methods inherited from Runner

#name

Constructor Details

#initialize(service) ⇒ Server

Returns a new instance of Server.

Parameters:



21
22
23
24
25
# File 'lib/nonnative/server.rb', line 21

def initialize(service)
  super

  @timeout = Nonnative::Timeout.new(service.timeout)
end

Instance Method Details

#startArray<(Integer, TrueClass)>

Starts the server thread if it is not already started.

Returns:

  • (Array<(Integer, TrueClass)>)

    a tuple of:

    • a stable identifier for this server instance (object_id)
    • true (thread creation itself is considered started; readiness is checked separately)


33
34
35
36
37
38
39
40
41
42
43
# File 'lib/nonnative/server.rb', line 33

def start
  unless thread
    @thread = Thread.new { perform_start }

    wait_start

    Nonnative.logger.info "started server '#{service.name}'"
  end

  [object_id, true]
end

#stopInteger

Stops the server if it is running.

Calls #perform_stop, terminates the server thread, and waits briefly.

Returns:

  • (Integer)

    the server identifier (object_id)



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/nonnative/server.rb', line 50

def stop
  if thread
    perform_stop
    thread.terminate

    @thread = nil
    wait_stop

    Nonnative.logger.info "stopped server '#{service.name}'"
  end

  object_id
end