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, configure = Midori::Configure) ⇒ Runner

Define status of a runner

Parameters:

  • api (Class)

    inherited from [Midori::API]

  • configure (Class) (defaults to: Midori::Configure)

    inherited from [Midori::Configure]



12
13
14
15
16
17
18
19
# File 'lib/midori/runner.rb', line 12

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
# 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
  @server = TCPServer.new(@bind, @port)
  EventLoop.register(@server, :r) do |monitor|
    socket = monitor.io.accept_nonblock
    connection = Midori::Connection.new(socket)
    connection.server_initialize(@api, @logger)
  end
  async_fiber(Fiber.new do
    @before.call
  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



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/midori/runner.rb', line 50

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