Class: TP2::Server

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

Constant Summary collapse

PENDING_REQUESTS_GRACE_PERIOD =
0.1
PENDING_REQUESTS_TIMEOUT_PERIOD =
5

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(machine, opts, &app) ⇒ Server

Returns a new instance of Server.



32
33
34
35
36
37
38
# File 'lib/tp2/server.rb', line 32

def initialize(machine, opts, &app)
  @machine = machine
  @opts = opts
  @app = app || app_from_opts
  @server_fds = []
  @accept_fibers = []
end

Class Method Details

.rack_app(opts) ⇒ Object



12
13
14
15
16
# File 'lib/tp2/server.rb', line 12

def self.rack_app(opts)
  raise "Missing app location" if !opts[:app_location]

  TP2::RackAdapter.load(opts[:app_location])
end

.static_app(opts) ⇒ Object



28
29
# File 'lib/tp2/server.rb', line 28

def self.static_app(opts)
end

.tp2_app(machine, opts) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/tp2/server.rb', line 18

def self.tp2_app(machine, opts)
  if opts[:app_location]
    opts[:log]&.log("Loading app at #{opts[:app_location]}")
    require opts[:app_location]

    opts.merge!(TP2.config)
  end
  opts[:app]
end

Instance Method Details

#app_from_optsObject



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/tp2/server.rb', line 40

def app_from_opts
  case @opts[:app_type]
  when nil, :tp2
    Server.tp2_app(@machine, @opts)
  when :rack
    Server.rack_app(@opts)
  when :static
    Server.static_app(@opts)
  else
    raise "Invalid app type #{@opts[:app_type].inspect}"
  end
end

#runObject



53
54
55
56
57
58
# File 'lib/tp2/server.rb', line 53

def run
  setup
  @machine.join(*@accept_fibers)
rescue UM::Terminate
  graceful_shutdown
end