[Backgroundrb-devel] Using BackgrounDRb to replace nested loops?
Jacob Patton
jacob.patton at gmail.com
Thu Jul 13 20:22:27 EDT 2006
In response to my Rails Weenie post today[1], someone mentioned that
I try BackgrounDRb to help take the strain off my application.
1: http://rails.techno-weenie.net/question/2006/7/13/nested-loops-
best-way-to-send-messages-to-blog-subscribers
I've just tried to install and use BackgrounDRb with my app, but I'm
afraid that I'm not too versed in running processes like these, and I
can't get the caching to work--I get this error: private method
`cache' called for #<BackgrounDRb::MiddleMan:
0x254f9b0>:BackgrounDRb::MiddleMan
Would someone help me by looking over my Rails Weenie question,
below, and recommend to me whether BackgrounDRb will help me with my
problem?
(Thanks for the great site, as always, Ezra, and it was good meeting
you after the Workshop for Good Event, too!)
From the Rails Weenie post:
I run a members-only blog where subscribers can opt to receive
messages in given categories via email in real-time or daily/weekly
digests. What’s the best way to deliver the digest messages without
pushing my app into an ugly, processor-hungry loop?
Each message belongs to a given category, and each subscriber can
subscribe to 0-n categories to receive updates. Right now the daily
emails are sent as soon as a message is posted:
1. # within the create action...after the message is saved:
2.
3. # get subscribers to this category
4. @subscribers = @msg.category.subscribers
5.
6. # loop through the subscribers, delivering the message to each
one
7. for subscriber in @subscribers
8. MsgNotifier.deliver_update(@msg)
9. end
This causes a slight delay upon posting--the message has to be sent
to all the subscribers for this category--but it works. (I'm not
terribly concerned about the delay in this part, for now, but if
anyone has a suggestion on how to improve things, I'd love to hear
it.) The real problem arises, however, when I'm trying to compile the
messages for daily and weekly digests:
1. # get all real-time subscribers
2. @subscribers = User.find_all_by_freq('real-time')
3.
4. # loop through subscribers
5. for subscriber in @subscribers
6.
7. @text = nil
8.
9. # get categories for subscriber
10. @categories = subscriber.categories
11.
12. # loop through categories
13. for category in @categories
14.
15. # get messages posted in that category in the past day
16. @msgs = category.messages(:conditions => "[created_at > ?],
1.day.ago")
17.
18. # loop through messages
19. for msg in @msgs
20.
21. # concatenate the messages for that category
22. @text += msg.body
23. end
24. end
25. end
26.
27. # deliver subscriber's update containing all of the past days
messages
28. MsgNotifier.deliver_daily(@subscriber, @text)
29.
30. # unset @text
31. @text = nil
32. end
As you can see, this is really, really ugly. Is there a better way to
gather together the content for the digest messages? I'm open to any
suggestions--this code is very, very slow right now...
=
Best,
Jacob Patton
More information about the Backgroundrb-devel
mailing list