title: HTTP request during ActiveRecord creation
body: <p>I am trying to subscribe to a pubsubhubbub (pshb) feed on superfeedr. The way the pshb protocol works is</p>
  
  <ol>
  <li>you Send POST to the hub requesting to subscribe to a feed and provide a callback</li>
  <li>hub sends a GET to your callback to verifying your intent to subscribe</li>
  <li>you respond saying yes I do want to subscribe</li>
  <li>hub responds with subscription verification </li>
  </ol>
  
  <p>I am running the server locally on my dev machine. I have code that can successfully subscribe to a feed, I test it by executing it in the rails console directly. I have now created a Feed ActiveRecord model and want automactially subscribe every time a new Feed record is created. I have added a ActiveRecord callback in the Feed model as such</p>
  
  <pre><code>after_create :subscribe_feed
  </code></pre>
  
  <p>Now when I create an active record I see the correct HTTP request go out, then there is a long hang (about 5 seconds where nothing happens in the logs), then the response from the pshb server comes in saying my callback could not be reached.</p>
  
  <p>Here are the <strong>logs</strong> (numbers added by me for reference points)</p>
  
  <pre><code>(1)   (0.1ms)  begin transaction
  (2)  SQL (4.4ms)  INSERT INTO "feeds" ("created_at", "updated_at", "url") VALUES (?, ?, ?)  [["created_at", Fri, 15 Nov 2013 23:08:39 UTC +00:00], ["updated_at", Fri, 15 Nov 2013 23:08:39 UTC +00:00], ["url", "http://push-pub.appspot.com/feed"]]
  
  (3) pshb subscribe parameters: {:headers=&gt;{"Accept"=&gt;"application/json"}, :body=&gt;{"hub.mode"=&gt;"subscribe", "hub.verify"=&gt;"sync", "hub.callback"=&gt;"http://...myserver.../pub_sub/callback", "hub.topic"=&gt;"http://push-pub.appspot.com/feed", "hub.verify_token"=&gt;"superfeedtest", "format"=&gt;"json"}}
  
  (4) # ABOUT 5 SECOND WAIT
  
  (5) Subscribe Response: #&lt;HTTParty::Response:0x7fd2180cd978 parsed_response="Your callback couldn't be reached.\n", @response=#&lt;Net::HTTPUnprocessableEntity 422 Unprocessable Entity readbody=true&gt;, @headers={"server"=&gt;["nginx/0.8.52"], "date"=&gt;["Fri, 15 Nov 2013 23:08:44 GMT"], "content-type"=&gt;["text/plain; charset=utf-8"], "connection"=&gt;["close"], "status"=&gt;["422 Unprocessable Entity"], "x-runtime"=&gt;["10057"], "content-length"=&gt;["35"], "set-cookie"=&gt;["_superfeedr_session=BAh7BzoMdXNlcl9pZGkC%2BIY6D3Nlc3Npb25faWQiJTBiMDExZGYzNzU4Mjk0MTMxNjc4NmE0OTg3MDhlMjJk--85d29756ae4b5ae9464630741372af7656f3894b; path=/; HttpOnly"], "cache-control"=&gt;["no-cache"], "pubsubhubbub-version"=&gt;["0.3"]}&gt;
  
  (6)   (7.2ms)  commit transaction
  Redirected to http://67.180.177.165/feeds/28
  Completed 302 Found in 10695ms (ActiveRecord: 11.6ms)
  
  (7) Started GET "/pub_sub/callback?hub.challenge=437c4b3b47aa1dbf4072a2d8abb5c39a&amp;hub.lease_seconds=315360000&amp;hub.mode=subscribe&amp;hub.topic=http%3A%2F%2Fpush-pub.appspot.com%2Ffeed&amp;hub.verify_token=superfeedtest" for 173.255.193.75 at 2013-11-15 15:08:50 -0800
  </code></pre>
  
  <p>You can see that after the response the commit transaction happens (6), and then the GET from the phsb server asking to verify comes in (7). So the hub can reach my callback, but for some reason the I don't receive the GET from the hub until after it has timed out?</p>
  
  <p>I am new to rails so not sure how things work, but I'm guessing something is going on with the concurrency of requests in the middle of ActiveRecord creation. The subscription works on its own, but not during an ActiveRecord creation as I have set it up in after_create. </p>
  
  <p>What is the correct way to handle HTTP request to 3rd party servers if you want to do it when an ActiveRecord entry is created?</p>
  
  <p>NOTE** in the superfeedr api you can specify the verification intent to happen asynchronously. When I specify it to be async I do get a success response instead, but the subscription still does not seem to be crated on superfeedr's hub. I will contact superfeedr directly about this issue, but in general I want to know how to handle this situation for API's that do not have an async option.</p>