Class: Ap4r::Mongrel::Ap4rSendMessageHandler

Inherits:
Mongrel::HttpHandler
  • Object
show all
Defined in:
lib/ap4r/mongrel.rb

Overview

This class is an experimental implementation of RESTified message API. Send to queue:

using HTTP POST
a message is sent as HTTP body which format is depending on MIME type
(now supported only text/plain)

options are sent as HTTP header which header name is "X-AP4R"
url consists of prefix ("/queues") and queue name

=== Request example ===
POST /queues/queue.test HTTP/1.1
Content-Type: text/plain
X-AP4R: dispatch_mode=HTTP, target_method=POST, target_url=http://localhost:3000/async_shop/execute_via_http/

hoge

=== Response example ===
HTTP/1.1 200 Ok
Date: The, 11 Dec 2007 17:17:11 GMT

7bb181f0-7ee0-012a-300a-001560abd426

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Ap4rSendMessageHandler

Returns a new instance of Ap4rSendMessageHandler.



129
130
131
132
# File 'lib/ap4r/mongrel.rb', line 129

def initialize(options)
  @tick = Time.now
  @queues = {}
end

Instance Method Details

#log_threads_waiting_for(event) ⇒ Object



181
182
183
184
185
# File 'lib/ap4r/mongrel.rb', line 181

def log_threads_waiting_for(event)
  if Time.now - @tick > 10
    @tick = Time.now
  end
end

#make_header(x_ap4r_header) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/ap4r/mongrel.rb', line 166

def make_header(x_ap4r_header)
  header = {}
  if x_ap4r_header
    x_ap4r_header.split(',').map do |e|
      key, value = e.strip.split('=')
      if %w(dispatch_mode target_method delivery).include?(key)
        header[key.to_sym] = value.to_sym
      else
        header[key.to_sym] = value
      end
    end
  end
  header
end

#process(request, response) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/ap4r/mongrel.rb', line 134

def process(request, response)
  if response.socket.closed?
    return
  end

  queue_name = request.params[::Mongrel::Const::PATH_INFO][1..-1]
  header = make_header(request.params["HTTP_X_AP4R"]) # Todo: assign as constant 2007/11/27 by kiwamu

  if "POST".include? request.params[::Mongrel::Const::REQUEST_METHOD]
    begin
      q = if @queues.key? queue_name
            @queues[queue_name]
          else
            @queues[queue_name] = ::ReliableMsg::Queue.new(queue_name)
          end
      mid = q.put(request.body.string, header)

      response.start(200) do |head, out|
        head['Content-Type'] = 'text/plain'
        out.write mid
      end
    rescue Exception
      response.start(500) do |head, out|
        head['Content-Type'] = 'text/plain'
        out.write "Failed to send message. #{request.body.string}"
      end
    end
  else
    raise "HTTP method is not POST..." # Todo
  end
end

#reload!Object

Does the internal reload for Rails. It might work for most cases, but sometimes you get exceptions. In that case just do a real restart.



189
190
191
192
193
194
# File 'lib/ap4r/mongrel.rb', line 189

def reload!
  begin
    #TODO not implemented 2007/04/09 by shino
    raise "not yet implemented!"
  end
end