Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How/when the 'after_filter' works/runs?

I am using Ruby on Rails 3.1 and I would like to know (for performance reasons) whether or not the after_filter runs after that view files are rendered. That is, when an user access a my application URL, the related view file that he/she should display is rendered before that the after_filter runs or the after_filter runs before that the view file is rendered?

In other words, the application server starts to send the rendered view data to the user before to run the after_filter or it waits to run the after_filter method and only then it sends that view data?

P.S.: I opened this question because I would like to run some system updates (note: these updates doesn't affect the view output data and are not "used by"/"necessary to" the view at all) without impact on the end user experience (for example: slow loading of my application Web pages).

like image 218
Backo Avatar asked Feb 24 '12 20:02

Backo


2 Answers

You have to be aware that a request to your server is fully consistent and runs in its own thread.

If you put code in an after_filter, this will delay the whole request: to sum up badly, if the thread is alive, the page is not delivered.

That's why you find great plugins like DelayedJob and Resque, they work in a parallel processes and won't affect your app.


To answer your original question , after_filter are triggered after the view has been processed.

This is sometimes a hassle that's why people created some before_render filters.

like image 67
apneadiving Avatar answered Nov 07 '22 10:11

apneadiving


after_filter is run after the content has been generated, but before it's been sent to the client. You can verify this is so because you have access to the completely-generated response data in the response method of your controller in any after_filter.

After_filters are inappropriate places to put long running tasks as they will definitely impact the users' experience. Consider instead using the after_filter to kick off a long running delayed_job or resque task to perform your background work instead.

like image 45
Veraticus Avatar answered Nov 07 '22 10:11

Veraticus