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
44
45
46
47
48
49
50
# File 'lib/nonnative/server.rb', line 33

def start
  unless thread
    @error = nil
    @thread = Thread.new do
      perform_start
    rescue StandardError => e
      @error = e
      raise
    end
    @thread.report_on_exception = true

    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)



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/nonnative/server.rb', line 57

def stop
  if thread
    perform_stop
    thread.terminate

    @thread = nil
    wait_stop

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

  object_id
end

#terminationString?

Describes how the server thread terminated before becoming ready, for lifecycle diagnostics.

Returns nil while the thread is still alive, so callers can distinguish a dead thread from a live server that merely missed its readiness window.

Returns:

  • (String, nil)

    termination detail (clean early return or uncaught exception), or nil



77
78
79
80
81
82
83
# File 'lib/nonnative/server.rb', line 77

def termination
  return if thread.nil? || thread.alive?

  return "server thread raised #{error.class}: #{error.message}" if error

  'server thread exited before readiness'
end