1

Please help me out.

I need to http post messages to a particular SMS gateway. So, I have a table similar to this:

|     send_time     |  phone    |          msg          |
|2010-03-15 08:30:00|12125551234|'first message user A' |
|2010-03-15 09:30:00|12125551234|'secnd message user A' |
|2010-03-15 08:30:00|15145550000|'first message user B' |

Each one represents the parameters I need to build the http post request, and it needs to be sent at the send_time (give or take a few minutes).

I currently have a rake task every minute, that checks the db and sends the POSTs accordingly. But that seems a flimsy way to go about it. Besides, this needs to scale very well; I will have to be able send many (let's say thousands) in a given minute.

How would you tackle this problem?

AdrianoFerrari
  • 2,129
  • 1
  • 18
  • 14

1 Answers1

1

This is the kind of job that is best done by a background worker. Every text message that needs to be sent would be a job with a given time to run.

Both delayed job and resque support send later (it's not first class yet in resque, but there are examples), so both of those would be good choices for this task.

To make this scale you would add more workers to consume the messages.

jonnii
  • 28,019
  • 8
  • 80
  • 108
  • Thanks for your answer jonnii. So I can use DJ to send the messages asynchronously. How would I queue the messages at the right time? Am I still stuck with a per-minute cron rake? – AdrianoFerrari Mar 15 '11 at 17:54
  • Ok, your answer, combined with [this one](http://stackoverflow.com/questions/656553/best-rails-solution-for-a-mailer-that-runs-every-minute) should work. Calling `wget` or `curl` from `cron` (instead of a rake task). – AdrianoFerrari Mar 15 '11 at 18:30
  • You queue them by using the send later functionality of DJ. Then when DJ runs it will pick jobs that applicable for `now`. – jonnii Mar 15 '11 at 18:31
  • Ah, even better! I looked at DJ but somehow never noticed the `:run_at` option. Thanks a lot jonnii! – AdrianoFerrari Mar 15 '11 at 18:57