Class: Midori::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/midori/runner.rb

Overview

Abstract runner class to control instance of Midori Server

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api) ⇒ Runner

Define status of a runner

Parameters:

  • api (Class)

    inherited from [Midori::API]



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

#bindString

the address to bind

Returns:

  • (String)

    the current value of bind



6
7
8
# File 'lib/midori/runner.rb', line 6

def bind
  @bind
end

#loggerLogger

midori logger

Returns:

  • (Logger)

    the current value of logger



6
7
8
# File 'lib/midori/runner.rb', line 6

def logger
  @logger
end

#portInteger

the port to bind

Returns:

  • (Integer)

    the current value of port



6
7
8
# File 'lib/midori/runner.rb', line 6

def port
  @port
end

Instance Method Details

#running?Boolean

Get Midori server whether running

Returns:

  • (Boolean)
    true

    running

  • (Boolean)
    false

    not running



24
25
26
# File 'lib/midori/runner.rb', line 24

def running?
  !!@server
end

#startObject

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

#stopBoolean

Note:

This is an async method, but no callback

Stop the Midori server

Returns:

  • (Boolean)
    true

    stop successfully

  • (Boolean)
    false

    nothing to stop



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