Class: Coupler::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/coupler/runner.rb

Instance Method Summary collapse

Constructor Details

#initialize(argv = ARGV, options = {}, &block) ⇒ Runner

Returns a new instance of Runner.



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

  # See the Rack::Handler::Mongrel.run! method
  # NOTE: I don't want to join the server immediately, which is why I'm
  #       doing this by hand.
  @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

#        say <<'EOF'
#                             ___
#                            /\_ \
#  ___    ___   __  __  _____\//\ \      __   _ __
# /'___\ / __`\/\ \/\ \/\ '__`\\ \ \   /'__`\/\`'__\
#/\ \__//\ \L\ \ \ \_\ \ \ \L\ \\_\ \_/\  __/\ \ \/
#\ \____\ \____/\ \____/\ \ ,__//\____\ \____\\ \_\
# \/____/\/___/  \/___/  \ \ \/ \/____/\/____/ \/_/
#                         \ \_\
#                          \/_/
#EOF
  end
end

Instance Method Details

#joinObject



81
82
83
# File 'lib/coupler/runner.rb', line 81

def join
  @web_thread.join
end

#shutdownObject



75
76
77
78
79
# File 'lib/coupler/runner.rb', line 75

def shutdown
  say "Shutting down..."
  Scheduler.instance.shutdown
  @web_server.stop
end