Class: Kreuzberg::APIProxy::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/kreuzberg/api_proxy.rb

Overview

API server instance

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(port: 8000, host: '0.0.0.0') ⇒ Server

Initialize server

Parameters:

  • port (Integer) (defaults to: 8000)

    Port to run on (default: 8000)

  • host (String) (defaults to: '0.0.0.0')

    Host to bind to (default: “0.0.0.0”)



22
23
24
25
26
27
# File 'lib/kreuzberg/api_proxy.rb', line 22

def initialize(port: 8000, host: '0.0.0.0')
  @port = port
  @host = host
  @pid = nil
  @process = nil
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



15
16
17
# File 'lib/kreuzberg/api_proxy.rb', line 15

def host
  @host
end

#pidObject (readonly)

Returns the value of attribute pid.



15
16
17
# File 'lib/kreuzberg/api_proxy.rb', line 15

def pid
  @pid
end

#portObject (readonly)

Returns the value of attribute port.



15
16
17
# File 'lib/kreuzberg/api_proxy.rb', line 15

def port
  @port
end

Instance Method Details

#running?Boolean

Check if server is running

Returns:

  • (Boolean)


67
68
69
70
71
72
73
74
# File 'lib/kreuzberg/api_proxy.rb', line 67

def running?
  return false unless @pid

  Process.kill(0, @pid)
  true
rescue Errno::ESRCH, Errno::EPERM
  false
end

#startInteger

Start the server in the background

Returns:

  • (Integer)

    Process ID

Raises:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/kreuzberg/api_proxy.rb', line 34

def start
  binary = APIProxy.find_api_binary
  @pid = spawn(
    binary.to_s,
    'api',
    '--host', @host,
    '--port', @port.to_s,
    out: $stdout,
    err: $stderr
  )
  Process.detach(@pid)
  sleep 1
  @pid
end

#stopvoid

This method returns an undefined value.

Stop the server



53
54
55
56
57
58
59
60
61
# File 'lib/kreuzberg/api_proxy.rb', line 53

def stop
  return unless @pid

  Process.kill('TERM', @pid)
  Process.wait(@pid)
rescue Errno::ESRCH, Errno::ECHILD # rubocop:disable Lint/SuppressedException
ensure
  @pid = nil
end