3
4
5
6
7
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/coupler/runner.rb', line 3
def initialize(argv = ARGV, options = {}, &block)
@msg_proc = block
irb = false
OptionParser.new do |opts|
opts.on("-p", "--port PORT", "Web server port") do |port|
Base.set(:port, port.to_i)
end
opts.on("--dir DIR", "Directory to use for Coupler's data") do |dir|
Base.set(:data_path, dir)
end
opts.on("-e", "--environment ENVIRONMENT", "Set the environment") do |env|
case env
when "production", "development", "test"
Base.set(:environment, env.to_sym)
else
raise "Invalid environment (must be production, development, or test)"
end
end
opts.on('-i', '--interactive', "Run an IRB session") do
irb = true
end
end.parse!(argv)
say "Starting up Coupler..."
say "Migrating database..."
Coupler::Database.migrate!
say "Starting scheduler..."
Coupler::Scheduler.instance.start
say "Starting web server..."
handler = Rack::Handler.get('mongrel')
settings = Coupler::Base.settings
@web_server = Mongrel::HttpServer.new(settings.bind, settings.port, 950, 0, 60)
@web_server.register('/', handler.new(Coupler::Base))
success = false
begin
@web_thread = @web_server.run
success = true
rescue Errno::EADDRINUSE => e
Scheduler.instance.shutdown
say "Can't start web server, port already in use. Aborting..."
end
if success
Coupler::Base.set(:running, true)
puts "Web server is up and running on http://#{settings.bind}:#{settings.port}"
if !options.has_key?(:trap) || options[:trap]
trap("INT") do
shutdown
end
end
end
end
|