Class: Protein::Server

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

Class Method Summary collapse

Class Method Details

.config(config = nil) ⇒ Object



17
18
19
20
# File 'lib/protein/server.rb', line 17

def config(config = nil)
  @config = (@config || {}).merge(config) if config
  @config || {}
end

.route(router) ⇒ Object



4
5
6
# File 'lib/protein/server.rb', line 4

def route(router)
  @router = Router.define(router)
end

.routerObject



13
14
15
# File 'lib/protein/server.rb', line 13

def router
  GetConst.call(@router)
end

.service(service) ⇒ Object



8
9
10
11
# File 'lib/protein/server.rb', line 8

def service(service)
  @router ||= Class.new(Router)
  @router.service(service)
end

.startObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/protein/server.rb', line 31

def start
  worker_count = config.fetch(:concurrency, 5)
  on_worker_boot = config[:on_worker_boot]

  pids = (1..worker_count).map do |i|
    fork do
      Protein.logger.info "Starting server #{i}/#{worker_count} with PID #{Process.pid}"
      on_worker_boot.call if on_worker_boot.respond_to?(:call)
      transport_class.serve(router)
    end
  end

  Signal.trap('TERM') do
    pids.each { |pid| Process.kill(:TERM, pid) }
    pids.each { |pid| Process.wait(pid) }
  end

  Signal.trap('INT') do
    pids.each { |pid| Process.kill(:INT, pid) }
    pids.each { |pid| Process.wait(pid) }
  end

  Process.wait
end

.transport(transport, opts = {}) ⇒ Object



22
23
24
# File 'lib/protein/server.rb', line 22

def transport(transport, opts = {})
  @transport_class = Transport.define(transport, opts)
end

.transport_classObject



26
27
28
# File 'lib/protein/server.rb', line 26

def transport_class
  GetConst.call(@transport_class)
end