Bijan
Bijan

Reputation: 26479

Rails 2.3.X - Execute code after request was rendered and returned?

is it possible in rails 2.3.X to start a new chain of commands after a request has been rendered and returned to the requestor?

I need that feature in order to work with an asynchronous API on the other side: They expect a response to their request and after that response is done my rails app should send a new http-request to them (post something to their API)...

What are the possibilities here? Is there something like a after_render hook? Should I make use of threads or background tasks and how could this be done?

I would be very glad for some solutions :-)

Kind regards

UPDATE: The Return-Code (eg. 200) should be sent to the requestor before the other calls are executed

Upvotes: 11

Views: 4516

Answers (5)

Aaron Scruggs
Aaron Scruggs

Reputation: 712

The easiest thing to do is spawn a new thread. This is assuming that it is a lightweight call and you don't need advanced error logging or retry logic.

Thread.new do
  puts  "call the api"
end

Upvotes: 12

John Bachir
John Bachir

Reputation: 22731

The two most popular solutions for this are Delayed Job (that Lars mentioned), and Resque:

Upvotes: 3

Lars Haugseth
Lars Haugseth

Reputation: 14881

How about using something like Delayed Job?

Upvotes: 2

CharlieMezak
CharlieMezak

Reputation: 5999

Are you rendering html? If so, maybe you can insert some javascript into the rendered page to make a new request to your controller and initiate the further action that you need to take.

Upvotes: 0

NotDan
NotDan

Reputation: 32223

I could be wrong, but I think code execution continues after a render, unless you put a return. This is why you get an error if you try to render twice..

Upvotes: 1

Related Questions