Class: Reifier::Server

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

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Server

Returns a new instance of Server.



3
4
5
6
# File 'lib/reifier/server.rb', line 3

def initialize(app, options = {})
  @app     = app
  @options = options
end

Instance Method Details

#load_configurationObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/reifier/server.rb', line 43

def load_configuration
  if defined?(Rails)
    path = Rails.root.join('config/reifier.rb')
  else
    path = Dir.pwd + '/reifier.rb'
  end

  return unless File.exist?(path)

  lines = File.read(path).split("\n")

  lines.each do |line|
    option = line.split.first.capitalize
    value  = line.split.last

    @options[option.to_sym] = value
  end

  puts "======= Loaded settings from #{path} =======\n"
end

#startObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/reifier/server.rb', line 8

def start
  server = TCPServer.new(@options[:Host], @options[:Port])

  child_pids = []
  @options[:Workers].to_i.times do
    child_pids << spawn_worker(server)
  end

  Signal.trap 'SIGINT' do
    puts "\n======= Cleaning up #{child_pids.length} Workers =======\n"
    child_pids.each do |cpid|
      begin
        Process.kill(:INT, cpid)
      rescue Errno::ESRCH
      end
    end

    exit
  end

  puts "# Environment: #{@options[:environment]}"
  puts "# Listening on tcp://#{@options[:Host]}:#{@options[:Port]}"
  puts "# Master PID: #{Process.pid}"
  puts "# Number of Threads used: #{@options[:Threads]}"
  puts "# Number of Workers used: #{@options[:Workers]}"

  loop do
    pid = Process.wait
    STDERR.puts "Process #{pid} crashed"

    child_pids.delete(pid)
    child_pids << spawn_worker
  end
end