Reputation: 19257
When our rails 3 app sends messages (email or sms) to users we need ot throttle the rate at which we do so.
suppose
the_list = Messages.find(someconditions)
the_list.each do |msg|
msg.send_me
end
returns hundreds of messages, we (a) dont really want them all in memory at once and (b) cannot fire them off all at once.
If our practical limit is to send, say, 200 per minute, what is the rails way to 'find' them in smaller batches (b) throttle the calls to .send_me to meet our target send rate
FWIW we're running on Heroku and have delayed_job running now for other purposes.
Upvotes: 2
Views: 1237
Reputation: 1960
If you can't offload this from the rails process there's no way to do it or you'll block the whole app. Start a micro instance on ec2 and poll a queue w/ messages to send. Offload it from rails.
Upvotes: 1
Reputation: 28332
To limit the amount of messages that are sent you can use this:
Messages.find(someconditions, :limit => 100)
If you want to throttle the easiest way is probably to create multiple delayed jobs where they are run_at
a time in the future.
If you wanted to change your app a little further you could add a send_at time to your messages table, and when you create your messages do something like:
current_send_at = Message.for_user(current_user).latest.send_at || Time.now
new_message.send_at = current_send_at + 1.minute
new_message.save!
Then in your delayed job you'd select messages where send_at < now()
. Does this make sense?
Upvotes: 2