Module: DaemonicThreads::HTTP::Daemon

Defined in:
lib/ruby-daemonic-threads/http/daemon.rb

Overview

TODO: error handling for web frontends

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#listenerObject

Returns the value of attribute listener.



31
32
33
# File 'lib/ruby-daemonic-threads/http/daemon.rb', line 31

def listener
  @listener
end

#request_notifyObject (readonly)

Returns the value of attribute request_notify.



30
31
32
# File 'lib/ruby-daemonic-threads/http/daemon.rb', line 30

def request_notify
  @request_notify
end

Instance Method Details

#deinitialize_httpObject



26
27
28
# File 'lib/ruby-daemonic-threads/http/daemon.rb', line 26

def deinitialize_http
  @http.unregister(uri)
end

#initialize_httpObject



21
22
23
24
# File 'lib/ruby-daemonic-threads/http/daemon.rb', line 21

def initialize_http
  @http = @process.http
  @http.register(uri, self)
end

#process(mongrel_request, mongrel_response) ⇒ Object



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
74
75
76
77
78
79
80
81
# File 'lib/ruby-daemonic-threads/http/daemon.rb', line 38

def process(mongrel_request, mongrel_response)
  request = DaemonicThreads::HTTP::HttpRequest.new(mongrel_request, mongrel_response)
  
  panic_on_exception("HTTP request -- Processing", lambda { request.error(500, "Daemon encouners an exception"); request.log!(@logger) }) do

    @creatures_mutex.synchronize do
      if @must_terminate
        return request.error(503, "Daemon is in termination sequence now and can't serve your request")
      else
        @threads.add Thread.current
      end
    end
    
    request.parse
    
    unless request.correct?
      return request.error(400, "There is a restrictions on how request can be formed")
    end


    action = determine_http_action request
    
    log(:debug) { "HTTP REQUEST -- ACTION: #{action.inspect} FORMAT: #{request.requested_format} PARAMS: #{request.params.inspect}" }
    
    unless respond_to?(action)
      return request.error(404, "There is no such action")
    end
      
    begin
      result = __send__(action, request)
      request.response(result) unless request.response_sent?
      
    rescue Exception => exception
      if ActionController::Base.rescue_responses.has_key?(exception.class.name)
        request.error ActionController::Base.rescue_responses[exception.class.name]
      else
        raise exception
      end
    end
    
  end
ensure
  panic_on_exception("HTTP request -- Release ActiveRecord connection to pool") { ActiveRecord::Base.clear_active_connections! }
end

#uriObject



33
34
35
# File 'lib/ruby-daemonic-threads/http/daemon.rb', line 33

def uri
  @uri ||= (@parent ? (@parent.uri + '/' + @name) : ('/' + @http.prefix + '/' + @name))
end