Class: Daneel::Server

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Server

Returns a new instance of Server.



8
9
10
11
12
13
14
15
16
# File 'lib/daneel/server.rb', line 8

def initialize(options = {})
  require 'daneel/web'
  @logger = options[:logger] || Daneel::Logger.new
  @options = {:app => Daneel::Web, :server => "puma"}.merge(options)
  @options[:port] = ENV["PORT"] if ENV["PORT"]
  # Rack expects the port key to be capitalized. Sad day.
  @options[:Port] = @options.delete(:port) if @options[:port]
  logger.debug "Server with options: #{@options}"
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



6
7
8
# File 'lib/daneel/server.rb', line 6

def logger
  @logger
end

Instance Method Details

#runObject



18
19
20
21
22
# File 'lib/daneel/server.rb', line 18

def run
  Thread.new { run_server }
  sleep 0.1 # boot server before allowing possible interaction
  Thread.new { run_self_ping }
end

#run_self_pingObject



28
29
30
31
32
33
34
35
36
# File 'lib/daneel/server.rb', line 28

def run_self_ping
  return unless ENV['HEROKU_URL']
  uri = URI(ENV['HEROKU_URL'])
  http = Net::HTTP::Persistent.new 'daneel'
  loop do
    http.request uri
    sleep (60 * 20) # 20m
  end
end

#run_serverObject



24
25
26
# File 'lib/daneel/server.rb', line 24

def run_server
  Rack::Server.start(@options)
end