Class: Yarn::Server
Instance Attribute Summary collapse
-
#host ⇒ Object
Returns the value of attribute host.
-
#port ⇒ Object
Returns the value of attribute port.
-
#socket ⇒ Object
Returns the value of attribute socket.
-
#workers ⇒ Object
Returns the value of attribute workers.
Instance Method Summary collapse
- #init_workers ⇒ Object
-
#initialize(options = {}) ⇒ Server
constructor
A new instance of Server.
- #load_rack_app(app_path) ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
Methods included from Logging
#debug, #log, #output, #timestamp
Constructor Details
#initialize(options = {}) ⇒ Server
Returns a new instance of Server.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/yarn/server.rb', line 10 def initialize(={}) # merge given options with default values opts = { output: $stdout, host: '127.0.0.1', port: 3000, workers: 4, rack: "off" }.merge() @app = nil @app = load_rack_app(opts[:rack]) unless opts[:rack] == "off" @host, @port, @num_workers = opts[:host], opts[:port], opts[:workers] @workers = [] $output, $debug = opts[:output], opts[:debug] end |
Instance Attribute Details
#host ⇒ Object
Returns the value of attribute host.
8 9 10 |
# File 'lib/yarn/server.rb', line 8 def host @host end |
#port ⇒ Object
Returns the value of attribute port.
8 9 10 |
# File 'lib/yarn/server.rb', line 8 def port @port end |
#socket ⇒ Object
Returns the value of attribute socket.
8 9 10 |
# File 'lib/yarn/server.rb', line 8 def socket @socket end |
#workers ⇒ Object
Returns the value of attribute workers.
8 9 10 |
# File 'lib/yarn/server.rb', line 8 def workers @workers end |
Instance Method Details
#init_workers ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/yarn/server.rb', line 49 def init_workers @num_workers.times do @workers << fork do trap("INT") { exit } loop do handler ||= @app ? RackHandler.new(@app) : RequestHandler.new session = @socket.accept handler.run session end end end end |
#load_rack_app(app_path) ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'lib/yarn/server.rb', line 28 def load_rack_app(app_path) if File.exists?(app_path) config_file = File.read(app_path) rack_application = eval("Rack::Builder.new { #{config_file} }") else log "#{app_path} does not exist. Exiting." Kernel::exit end end |
#start ⇒ Object
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/yarn/server.rb', line 38 def start trap("INT") { stop } @socket = TCPServer.new(@host, @port) log "Yarn started #{@num_workers} workers and is listening on #{@host}:#{@port}" init_workers # Waits here for all processes to exit Process.waitall end |
#stop ⇒ Object
62 63 64 65 66 |
# File 'lib/yarn/server.rb', line 62 def stop @socket.close if (@socket && !@socket.closed?) log "Server stopped. Have a nice day!" end |