# Rails hook for post-processing after page is rendered?

**URL:** <https://discuss.rubyonrails.org/t/rails-hook-for-post-processing-after-page-is-rendered/34396>\
**Category:** rubyonrails-talk\
**Created:** [September 16, 2008, 2:47pm UTC](https://discuss.rubyonrails.org/t/rails-hook-for-post-processing-after-page-is-rendered/34396 "2008-09-16T14:47:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [September 16, 2008, 2:47pm UTC](https://discuss.rubyonrails.org/t/rails-hook-for-post-processing-after-page-is-rendered/34396/1 "2008-09-16T14:47:41Z")

</div>

We're starting to notice a pattern in our Rails app that there are asynchronous actions we'd like to take after the response is rendered and shipped to the browser. Examples including queuing email and writing statistics to the database.

It would be cool if there were a hook in Rails that would allow us to throw this work onto a queue and have it done in the same process but just after the response is sent back to the browser.

Anyone know of a hook like this, or a plugin that creates one?

-Colin

---

<div class="post-metadata">

**Author:** ![Bill\_Walton](https://avatars.discourse-cdn.com/v4/letter/b/f07891/32.png) [@Bill\_Walton](https://discuss.rubyonrails.org/u/Bill_Walton)\
**Post date:** [September 16, 2008, 3:19pm UTC](https://discuss.rubyonrails.org/t/rails-hook-for-post-processing-after-page-is-rendered/34396/2 "2008-09-16T15:19:11Z")

</div>

Hi Colin,

Colin Kelley wrote:

> We're starting to notice a pattern in our Rails app that there are asynchronous actions we'd like to take after the response is rendered and shipped to the browser. Examples including queuing email and writing statistics to the database.
> 
> It would be cool if there were a hook in Rails that would allow us to throw this work onto a queue and have it done in the same process but just after the response is sent back to the browser.
> 
> Anyone know of a hook like this, or a plugin that creates one?

If I understand your question correctly, you're looking for a distributed Ruby library / plugin. The standard library includes dRuby. The choices re: plugins includes BackgrounDRb which was the standard choice for quite a while. There are other options available today which may suit your needs better, depending. Google 'distributed ruby plugins'.

WRT doing the work in the same process that handled the request, the Rails architecture pretty much precludes that, at least as a 'best practice.'

HTH, Bill

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [September 17, 2008, 5:18am UTC](https://discuss.rubyonrails.org/t/rails-hook-for-post-processing-after-page-is-rendered/34396/3 "2008-09-17T05:18:10Z")

</div>

Bill Walton wrote:

> If I understand your question correctly, you're looking for a distributed Ruby library / plugin. The standard library includes dRuby.

...

> WRT doing the work in the same process that handled the request, the Rails architecture pretty much precludes that, at least as a 'best practice.'

Thanks Bill for the quick response.

Distributed Ruby is pretty heavyweight for what I have in mind. It has a lot of moving parts. I was thinking lightweight that would just allow me to time shift a few operations that can take a second or two so that the page is returned first and then those operations are then performed. (Example: queue an email.) I'd like to have access to the full cached model when those post\_processing operations run.

I poked around the source a little today and it seems feasible to patch in at the RailsHandler::process(request, response) level. That method could be aliased and replaced with a new process method:

alias :old\_process :process

def process(request, response) &nbsp;&nbsp;old\_process(request, response) &nbsp;&nbsp;post\_process end

where post\_process was something like

within\_timer\_block do &nbsp;&nbsp;while task = @@background\_tasks.shift &nbsp;&nbsp;&nbsp;&nbsp;task.call &nbsp;&nbsp;end end

(The controllers would queue their slow operations onto @@background\_tasks.)

The timers supervision is to preclude them taking too much time away from serving the next pages. And it would be best to not allow more than one post\_processing mongrel at a time for the same session (or possibly IP:port), lest a runaway Ajax loop consume all the mongrels for example.

Does this sound feasible? Anyone know of some work along these lines already?

---

<div class="post-metadata">

**Author:** ![Bill\_Walton](https://avatars.discourse-cdn.com/v4/letter/b/f07891/32.png) [@Bill\_Walton](https://discuss.rubyonrails.org/u/Bill_Walton)\
**Post date:** [September 17, 2008, 1:39pm UTC](https://discuss.rubyonrails.org/t/rails-hook-for-post-processing-after-page-is-rendered/34396/4 "2008-09-17T13:39:28Z")

</div>

Colin Kelley wrote:

> Bill Walton wrote: \> If I understand your question correctly, you're looking for a \> distributed \> Ruby library / plugin. The standard library includes dRuby. ... \> WRT doing the work in the same process that handled the request, the \> Rails \> architecture pretty much precludes that, at least as a 'best practice.'
> 
> Thanks Bill for the quick response.
> 
> Distributed Ruby is pretty heavyweight for what I have in mind. It has a lot of moving parts. I was thinking lightweight that would just allow me to time shift a few operations that can take a second or two so that the page is returned first and then those operations are then performed. (Example: queue an email.) I'd like to have access to the full cached model when those post\_processing operations run.

BackgrounDRb gives you access to your app's models. I haven't used any of the available alternatives, but I'd expect them to do the same.

> I poked around the source a little today and it seems feasible to patch in at the RailsHandler::process(request, response) level. That method could be aliased and replaced with a new process method:
> 
> alias :old\_process :process
> 
> def process(request, response) &nbsp;&nbsp;old\_process(request, response) &nbsp;&nbsp;post\_process end
> 
> where post\_process was something like
> 
> within\_timer\_block do &nbsp;&nbsp;while task = @@background\_tasks.shift &nbsp;&nbsp;&nbsp;&nbsp;task.call &nbsp;&nbsp;end end
> 
> (The controllers would queue their slow operations onto @@background\_tasks.)
> 
> The timers supervision is to preclude them taking too much time away from serving the next pages. And it would be best to not allow more than one post\_processing mongrel at a time for the same session (or possibly IP:port), lest a runaway Ajax loop consume all the mongrels for example.
> 
> Does this sound feasible? Anyone know of some work along these lines already?

I would absolutely recommend against thsi approach. You're adding at least three levels of complexity when one would do. And the one's you're contemplating are dangerous. Using a distributed process like BackgrounDRb solves your problem with a tested and supported solution. I was using the 'old' one, primarily because it also ran on Windows. And I didn't find it 'heavyweight' at all. I'd strongly recommend you try out the existing solutions to this very common problem before you go hacking Rails' core.

Best regards, Bill

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [September 17, 2008, 6:42pm UTC](https://discuss.rubyonrails.org/t/rails-hook-for-post-processing-after-page-is-rendered/34396/5 "2008-09-17T18:42:31Z")

</div>

Bill Walton wrote:

> BackgrounDRb gives you access to your app's models.

Excellent--I wasn't aware of that.

> I'd strongly recommend you try out the existing solutions to this very common problem before you go hacking Rails' core.

I will follow your advice and try out BackgrounDRb and perhaps one of the alternatives as well.

Thanks again,

-Colin

---

<div class="post-metadata">

**Author:** ![ahab](https://avatars.discourse-cdn.com/v4/letter/a/c57346/32.png) [@ahab](https://discuss.rubyonrails.org/u/ahab)\
**Post date:** [September 17, 2008, 8:41pm UTC](https://discuss.rubyonrails.org/t/rails-hook-for-post-processing-after-page-is-rendered/34396/6 "2008-09-17T20:41:07Z")

</div>

The Rails Wiki lists all possibilities - [http://wiki.rubyonrails.org/rails/pages/HowToRunBackgroundJobsInRails](http://wiki.rubyonrails.org/rails/pages/HowToRunBackgroundJobsInRails)

The most simple (and low-level) solution I found was to use script/ runner (from [http://www.ahabman.com/blog/2008/05/rails-background-process-simple-fast-easy/](http://www.ahabman.com/blog/2008/05/rails-background-process-simple-fast-easy/))

My\_Controller

&nbsp;&nbsp;def in\_background &nbsp;&nbsp;&nbsp;&nbsp;# your normal code here

&nbsp;&nbsp;&nbsp;&nbsp;system " RAILS\_ENV=#{RAILS\_ENV} ruby #{RAILS\_ROOT}/script/ runner 'MyModel.some\_method\_to\_run\_in\_background' & ”

&nbsp;&nbsp;end

end
