# Using after\_filter for speed improvement?

**URL:** <https://www.ruby-forum.com/t/using-after-filter-for-speed-improvement/85455>\
**Category:** Rails\
**Created:** [February 13, 2007, 7:35pm UTC](https://www.ruby-forum.com/t/using-after-filter-for-speed-improvement/85455 "2007-02-13T19:35:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![YJYJYJ](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b5a626/32.png) [@YJYJYJ](https://www.ruby-forum.com/u/YJYJYJ)\
**Post date:** [February 13, 2007, 7:35pm UTC](https://www.ruby-forum.com/t/using-after-filter-for-speed-improvement/85455/1 "2007-02-13T19:35:19Z")

</div>

Whenever a user sends another user a message, my app delivers an email  
notification to the recipient. Unfortunately, the email delivery process  
creates a short delay. Any thoughts on how to remove this delay? Would  
calling the delivery method with a after\_filter be the right way to go?  
Thanks.

Jesse

---

<div class="post-metadata">

**Author:** ![YJYJYJ](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b5a626/32.png) [@YJYJYJ](https://www.ruby-forum.com/u/YJYJYJ)\
**Post date:** [February 13, 2007, 8:44pm UTC](https://www.ruby-forum.com/t/using-after-filter-for-speed-improvement/85455/2 "2007-02-13T20:44:45Z")

</div>

> Whenever a user sends another user a message, my app delivers an email  
> notification to the recipient. Unfortunately, the email delivery process  
> creates a short delay. Any thoughts on how to remove this delay? Would  
> calling the delivery method with a after\_filter be the right way to go?  
> Thanks.

The after\_filter might mask the delay… I’ve never used it, but another  
option would be to store the message info in a table and process it with  
a  
background process out of cron every minute or so (or shoot it off to  
backgroundrb for processing)…

-p

---

<div class="post-metadata">

**Author:** ![YJYJYJ](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b5a626/32.png) [@YJYJYJ](https://www.ruby-forum.com/u/YJYJYJ)\
**Post date:** [February 13, 2007, 9:12pm UTC](https://www.ruby-forum.com/t/using-after-filter-for-speed-improvement/85455/3 "2007-02-13T21:12:48Z")

</div>

On 2/13/07, YJ [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Whenever a user sends another user a message, my app delivers an email  
> notification to the recipient. Unfortunately, the email delivery process  
> creates a short delay. Any thoughts on how to remove this delay? Would  
> calling the delivery method with a after\_filter be the right way to go?  
> Thanks.

You can try this approach

> **[DATANOISE.COM](https://www.datanoise.com/)**
>
> DATANOISE.COM - Contact us for any business inquiries

## – Kent

> **[DATANOISE.COM](https://www.datanoise.com)**
>
> DATANOISE.COM - Contact us for any business inquiries

---

<div class="post-metadata">

**Author:** ![YJYJYJ](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b5a626/32.png) [@YJYJYJ](https://www.ruby-forum.com/u/YJYJYJ)\
**Post date:** [February 13, 2007, 9:19pm UTC](https://www.ruby-forum.com/t/using-after-filter-for-speed-improvement/85455/4 "2007-02-13T21:19:49Z")

</div>

The method of running a cron that often isn’t really a good idea,  
especially if you’re not running on top of the line hardware. It will  
load up the whole environment, execute that one tiny task and exit. What  
I’ve done in cases like this is write a long-running(read: infinite)  
class method that checks for the condition, executes its task and then  
waits 30 seconds or so. Yes, you still have an extra copy of the app in  
memory, but you’re not using up cpu and disk resources like you would  
loading everything up every minute.

Jason

---

<div class="post-metadata">

**Author:** ![YJYJYJ](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b5a626/32.png) [@YJYJYJ](https://www.ruby-forum.com/u/YJYJYJ)\
**Post date:** [February 13, 2007, 10:55pm UTC](https://www.ruby-forum.com/t/using-after-filter-for-speed-improvement/85455/5 "2007-02-13T22:55:16Z")

</div>

Check out the observer class. It is tailor made for what you want to do.

[http://api.rubyonrails.org/classes/ActiveRecord/Observer.html](http://api.rubyonrails.org/classes/ActiveRecord/Observer.html)

---

<div class="post-metadata">

**Author:** ![YJYJYJ](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b5a626/32.png) [@YJYJYJ](https://www.ruby-forum.com/u/YJYJYJ)\
**Post date:** [February 14, 2007, 1:49pm UTC](https://www.ruby-forum.com/t/using-after-filter-for-speed-improvement/85455/6 "2007-02-14T13:49:21Z")

</div>

YJ wrote:

> Whenever a user sends another user a message, my app delivers an email  
> notification to the recipient. Unfortunately, the email delivery process  
> creates a short delay. Any thoughts on how to remove this delay? Would  
> calling the delivery method with a after\_filter be the right way to go?  
> Thanks.
> 
> Jesse

Hey YJ

I wrote a short article on exactly this issue here:

[http://rails.co.za/articles/2006/10/30/sending-emails-in-the-background-with-actionmailer-and-backgroundrb](http://rails.co.za/articles/2006/10/30/sending-emails-in-the-background-with-actionmailer-and-backgroundrb)

It does as Philip stated right at the end of his reply…

Cheers,  
Gustav P.

---

<div class="post-metadata">

**Author:** ![YJYJYJ](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b5a626/32.png) [@YJYJYJ](https://www.ruby-forum.com/u/YJYJYJ)\
**Post date:** [February 14, 2007, 7:29pm UTC](https://www.ruby-forum.com/t/using-after-filter-for-speed-improvement/85455/7 "2007-02-14T19:29:15Z")

</div>

Chet,

The observer class looks interesting, and I may use it in a project I’m  
working on, but I don’t think the situation I had could use Observer.  
Basically, the app is an Ebay-style auction site. I have lots which have  
an expiration time and a status. My daemon goes and queries the database  
for lots that are “Open” but whose end time has passed. If it finds one,  
it closes the lot, logs a purchase and sends emails to the buyer and  
seller.  
It is really clunky having an external process like that running, and a  
bit unreliable - the app could be running fine, but without the daemon,  
lots will never get properly closed and no one will get emails about  
them.  
If you know of any other way to implement something like this, I’d be  
happy to hear about it.

Jason
