Class: Protein::Server
- Inherits:
-
Object
- Object
- Protein::Server
- Defined in:
- lib/protein/server.rb
Class Method Summary collapse
- .config(config = nil) ⇒ Object
- .route(router) ⇒ Object
- .router ⇒ Object
- .service(service) ⇒ Object
- .start ⇒ Object
- .transport(transport, opts = {}) ⇒ Object
- .transport_class ⇒ Object
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 |
.router ⇒ Object
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 |
.start ⇒ Object
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 |