Class: Midori::Runner
- Inherits:
-
Object
- Object
- Midori::Runner
- Defined in:
- lib/midori/runner.rb
Overview
Abstract runner class to control instance of Midori Server
Instance Attribute Summary collapse
-
#bind ⇒ String
the address to bind.
-
#logger ⇒ Logger
midori logger.
-
#port ⇒ Integer
the port to bind.
Instance Method Summary collapse
-
#initialize(api) ⇒ Runner
constructor
Define status of a runner.
-
#running? ⇒ Boolean
Get Midori server whether running.
-
#start ⇒ Object
Start the Midori server.
-
#stop ⇒ Boolean
Stop the Midori server.
Constructor Details
#initialize(api) ⇒ Runner
Define status of a runner
11 12 13 14 15 16 17 18 19 |
# File 'lib/midori/runner.rb', line 11 def initialize(api) configure = Midori::Configure @logger = configure.logger Midori.logger = configure.logger @bind = configure.bind @port = configure.port @api = (api.is_a? Midori::APIEngine) ? api : Midori::APIEngine.new(api, configure.route_type) @before = configure.before end |
Instance Attribute Details
#logger ⇒ Logger
midori logger
6 7 8 |
# File 'lib/midori/runner.rb', line 6 def logger @logger end |
#port ⇒ Integer
the port to bind
6 7 8 |
# File 'lib/midori/runner.rb', line 6 def port @port end |
Instance Method Details
#running? ⇒ Boolean
Get Midori server whether running
24 25 26 |
# File 'lib/midori/runner.rb', line 24 def running? !!@server end |
#start ⇒ Object
Note:
This is an async method, but no callback
Start the Midori server
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/midori/runner.rb', line 30 def start return false if running? || EventLoop.running? @logger.info "Midori #{Midori::VERSION} is now running on #{bind}:#{port}".blue init_socket async_fiber(Fiber.new do @logger.info 'Midori is booting...'.blue @before.call @logger.info 'Midori is serving...'.blue EventLoop.register(@server, :r) do |monitor| socket = monitor.io.accept_nonblock connection = Midori::Connection.new(socket) connection.server_initialize(@api, @logger) end end) EventLoop.start nil end |
#stop ⇒ Boolean
Note:
This is an async method, but no callback
Stop the Midori server
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/midori/runner.rb', line 63 def stop if running? @logger.info 'Stopping Midori'.blue EventLoop.deregister @server @server.close @server = nil EventLoop.stop true else @logger.error 'Midori Server has NOT been started'.red false end end |