Module: Ramaze::Adapter

Defined in:
lib/ramaze/adapter.rb,
lib/ramaze/adapter/cgi.rb,
lib/ramaze/adapter/ebb.rb,
lib/ramaze/adapter/base.rb,
lib/ramaze/adapter/fake.rb,
lib/ramaze/adapter/fcgi.rb,
lib/ramaze/adapter/lsws.rb,
lib/ramaze/adapter/scgi.rb,
lib/ramaze/adapter/thin.rb,
lib/ramaze/adapter/mongrel.rb,
lib/ramaze/adapter/webrick.rb

Overview

This module holds all classes and methods related to the adapters like webrick or mongrel. It’s responsible for starting and stopping them.

Defined Under Namespace

Classes: Base, Cgi, Ebb, Fake, Fcgi, Lsws, Mongrel, Scgi, Thin, WEBrick

Constant Summary collapse

MIDDLEWARE =

(Rack) middleware injected around Adapter::Base::call

OrderedSet[
  Rack::ShowExceptions,
  Rack::ShowStatus,
  # Rack::Deflater,
  Ramaze::Reloader,
  Ramaze::Current,
  Ramaze::Dispatcher
]

Class Method Summary collapse

Class Method Details

.before(&block) ⇒ Object

Helper to assign a new block to before_call Usage:

Ramaze::Adapter.before do |env|
  if env['PATH_INFO'] =~ /superfast/
    [200, {'Content-Type' => 'text/plain'}, ['super fast!']]
  end
end


40
41
42
43
# File 'lib/ramaze/adapter/base.rb', line 40

def self.before(&block)
  @before = block if block
  @before
end

.middleware(mws = MIDDLEWARE) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ramaze/adapter/base.rb', line 17

def self.middleware(mws = MIDDLEWARE)
  if @middleware and trait[:previous] == mws
    @middleware
  else
    trait :previous => mws.dup
    inner_app = mws.last
    cascade = mws[0...-1].reverse

    @middleware = cascade.inject(inner_app){|app, mw| mw.new(app) }
  end
end

.shutdownObject

Calls ::shutdown on all running adapters and waits up to 1 second for them to finish, then goes on to kill them and exit still.



71
72
73
74
75
76
77
78
79
# File 'lib/ramaze/adapter.rb', line 71

def shutdown
  Timeout.timeout(3) do
    if Global.server.respond_to?(:shutdown)
      Global.server.shutdown
    end
  end
rescue Timeout::Error
  Global.server.kill!
end

.start_adapterObject

Takes Global.adapter and starts if test_connections is positive that a connection can be made to the specified host and port. If you set Global.adapter to false it won’t start any but deploy a dummy which is useful for testing purposes where you just send fake requests to Dispatcher.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ramaze/adapter.rb', line 49

def start_adapter
  if adapter = Global.adapter
    host, port = Global.host, Global.port

    if Global.test_connections
      test_connection(host, port)
      Log.info("Ramaze is ready to run on: #{host}:#{port} using #{Global.adapter}")
    end

    adapter.start(host, port)
  else
    Fake.start
    Log.warn("Seems like Global.adapter is turned off", "Continue without adapter.")
  end
rescue LoadError => ex
  Log.error(ex)
  Log.warn("Continue without adapter.")
end

.startup(options = {}) ⇒ Object

Is called by Ramaze.startup and will first call start_adapter and wait up to 3 seconds for an adapter to appear. It will then wait for the adapters to finish If Global.run_loose is set or otherwise pass you on control which is useful for testing or IRB sessions.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ramaze/adapter.rb', line 27

def startup options = {}
  start_adapter

  Timeout.timeout(3) do
    sleep 0.01 until Global.server
  end

  Global.server.join unless Global.run_loose

rescue SystemExit
  Ramaze.shutdown
rescue Object => ex
  Log.error(ex)
  Ramaze.shutdown
end

.test_connection(host, port) ⇒ Object

Opens a TCPServer temporarily and returns true if a connection is possible and false if none can be made



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/ramaze/adapter.rb', line 84

def test_connection(host, port)
  Timeout.timeout(1) do
    testsock = TCPServer.new(host, port)
    testsock.close
    true
  end
rescue => ex
  Log.error(ex)
  Log.error("Cannot open connection on #{host}:#{port}")
  Ramaze.shutdown
end