Permalink
Browse files

rough cut of evented http request

  • Loading branch information...
1 parent 41b3d00 commit 12b106144a2240e53a500834c00cc2d6d9533fc8 @adamwiggins committed Jul 27, 2009
Showing with 49 additions and 5 deletions.
  1. +8 −0 lib/probe.rb
  2. +10 −0 spec/probe_spec.rb
  3. +1 −1 views/results/it_works.erb
  4. +30 −4 worker.rb
View
8 lib/probe.rb
@@ -132,6 +132,14 @@ def probe_http
return :app_exception
end
+ def self.http_result(response)
+ if response[:status].to_s.match(/^2\d\d$/)
+ return :it_works
+ else
+ return :app_exception
+ end
+ end
+
def result_type
return result if %w(it_works heroku_error).include? result.to_s
'user_error'
View
10 spec/probe_spec.rb
@@ -40,4 +40,14 @@
probe.state.should == 'done'
end
end
+
+ describe "http result" do
+ it ":it_works when status code is 200" do
+ Probe.http_result(:status => 200).should == :it_works
+ end
+
+ it ":app_exception when status code is 500" do
+ Probe.http_result(:status => 500).should == :app_exception
+ end
+ end
end
View
2 views/results/it_works.erb
@@ -6,7 +6,7 @@
<tr>
<th>Details</th>
<td>
- <% details = @probe.result_details %>
+ <% details = @probe.result_details || {} %>
<ul>
<li>HTTP status <%= details['http_code'] %></li>
<li><%= details['response_time'] %>ms response time</li>
View
34 worker.rb
@@ -7,8 +7,18 @@ def log(msg)
log "=== Worker starting up"
+$httpreqs = []
+
trap 'INT' do
log "Shutting down"
+
+ if $httpreqs.size > 0
+ log "(putting #{$httpreqs.size} httpreq probes back into the queue)"
+ $httpreqs.each do |probe|
+ probe.enqueue
+ end
+ end
+
EM.stop
exit
end
@@ -18,10 +28,26 @@ def log(msg)
probe = Probe.pop_queue
if probe
log "Working #{probe.id} #{probe.state}"
- probe.perform
- probe.save
- probe.enqueue if probe.state != 'done'
- log "#{probe.id} next state is #{probe.state} #{probe.result ? "and result is #{probe.result}" : ''}"
+
+ if probe.state == 'httpreq'
+ log "Sending http request to #{probe.url}"
+ $httpreqs << probe
+
+ http = EM::Protocols::HttpClient.request(:host => probe.uri.host, :port => probe.uri.port, :request => probe.uri.path)
+
+ http.callback do |r|
+ probe.result, probe.result_details = Probe.http_result(r)
+ probe.state = 'done'
+ probe.save
+ log "#{probe.id} next state is #{probe.state} #{probe.result ? "and result is #{probe.result}" : ''}"
+ $httpreqs.delete probe
+ end
+ else
+ probe.perform
+ probe.save
+ probe.enqueue if probe.state != 'done'
+ log "#{probe.id} next state is #{probe.state} #{probe.result ? "and result is #{probe.result}" : ''}"
+ end
end
end
end

0 comments on commit 12b1061

Please sign in to comment.