Class: Perus::Server::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/perus/server/server.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options_path = DEFAULT_SERVER_OPTIONS_PATH, environment = 'development') ⇒ Server

Returns a new instance of Server.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/perus/server/server.rb', line 29

def initialize(options_path = DEFAULT_SERVER_OPTIONS_PATH, environment = 'development')
    self.class.load_options(options_path)
    ENV['RACK_ENV'] = environment

    # initialise/migrate the db and start cleanup/caching timers
    DB.start
    DB.start_timers

    # ping data is processed in a thread pool
    Thread.new do
        while true
            ping = Server.ping_queue.pop
            begin
                ping.call
            rescue => e
                puts e.inspect
                if e.backtrace.is_a?(Array)
                    puts e.backtrace.join("\n") + "\n"
                end
            end
        end
    end
end

Class Method Details

.load_options(path = DEFAULT_SERVER_OPTIONS_PATH) ⇒ Object



69
70
71
# File 'lib/perus/server/server.rb', line 69

def self.load_options(path = DEFAULT_SERVER_OPTIONS_PATH)
    options.load(path, DEFAULT_SERVER_OPTIONS)
end

.optionsObject



65
66
67
# File 'lib/perus/server/server.rb', line 65

def self.options
    @options ||= Perus::Options.new
end

.ping_queueObject



61
62
63
# File 'lib/perus/server/server.rb', line 61

def self.ping_queue
    @ping_queue ||= Queue.new
end

Instance Method Details

#runObject



53
54
55
56
57
58
59
# File 'lib/perus/server/server.rb', line 53

def run
    Thin::Server.start(
        self.class.options.listen,
        self.class.options.port.to_i,
        App
    )
end