class SpiderGazelle::Reactor

Attributes

thread[R]

Public Class Methods

new() click to toggle source
# File lib/spider-gazelle/reactor.rb, line 13
def initialize
    @thread = ::Libuv::Reactor.default
    @logger = Logger.instance
    @running = false
    @shutdown_called = false
end

Public Instance Methods

log(error, context, trace = nil) click to toggle source

This is an unhandled error on the Libuv Event loop

# File lib/spider-gazelle/reactor.rb, line 54
def log(error, context, trace = nil)
    msg = String.new
    if error.respond_to?(:backtrace)
        msg << "unhandled exception: #{error.message} (#{context})"
        backtrace = error.backtrace
        msg << "\n#{backtrace.join("\n")}" if backtrace
        msg << "\n#{trace.join("\n")}" if trace
    else
        msg << "unhandled exception: #{args}"
    end
    @logger.error msg
end
run() { || ... } click to toggle source
# File lib/spider-gazelle/reactor.rb, line 20
def run
    if @running
        @thread.schedule { yield }
    else
        @running = true
        @thread.notifier { |*args| log(*args) }
        @thread.on_program_interrupt { shutdown }
        @thread.run { yield }
    end
end
shutdown() click to toggle source
# File lib/spider-gazelle/reactor.rb, line 31
def shutdown
    if @shutdown_called
        @logger.warn "Shutdown called twice! Callstack:\n#{caller.join("\n")}"
        return
    end

    @thread.schedule do
        if not @shutdown_called
            @shutdown_called = true

            # Signaller will manage the shutdown of the gazelles
            signaller = Signaller.instance.shutdown
            signaller.finally do
                @thread.stop
                # New line on exit to avoid any ctrl-c characters
                # We check for pipe as we only want the master process to print this
                puts "\nSpider-Gazelle leaps through the veldt\n"
            end
        end
    end
end