Ruby on Rails Friday, August 19, 2016


On Thursday, August 18, 2016 at 7:41:23 PM UTC-4, Phil wrote:

Have a few cases where it is critically important to flush the output of 'render' immediately.  For example:


 def place_order
  if validate_and_record_order?
   render :text => "OK"
  else
   render :text => "ERROR"
  end
  # DESPERATELY WANT TO FLUSH OUTPUT HERE!
  # Do lots of talking with banks, send out mailers, etc.
 end


Another wrinkle here is we can't store the customer's banking information to run as a deferred task.  Ideally we just want to say 'flush the output' after the 'if' block.  Even if there's an exception after the 'if' we still don't want them to get a 500, we want them to get the OK/ERROR.

The problem we have is the people connecting to our app are sometimes timing out (they have a short timeout) and think the order failed when in fact it was recorded, customer was charged, etc.  I'm a little hesitant about using Thread.new, but perhaps that's the only reasonable way to flush the output early?

I have used the spawnling gem to fork a new long-running process. It handles closing/re-opening database connections, etc.

https://github.com/tra/spawnling

Another alternative could be Server-Sent Events (SSE), which works with Passenger > 4.0.5. It will require changes in how you set up the front end as well, but is good if you want to pass status back to the user during/after this long-running task.

Jim Crate

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/5afcec87-d8d2-4b0c-bbff-f644deb9ac77%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment