1

In my app, when a user signs up, they are sent a confirmation email. I use delayed_job, to make the process of sending email go in background.

But disadvantage of using delayed_job is having a worker all the time. And having a worker for this is expensive.

Is there something other than delayed_job, that will make email sending go in background.

Here is my controller code snippet.

def create
    @user = User.new(params[:user])

    respond_to do |format|
      if @user.save
        UserMailer.delay.registration_confirmation(@user)
        format.html { redirect_to @user, notice: 'User was successfully created.' }
        format.js

The point is I am having 20-40 signups in a day. That means at most the the queue is busy for about 60 seconds and I will have to pay for the the entire day, which is very impractical. Some other nice approach.

Nikhil
  • 1,268
  • 2
  • 13
  • 29
  • Actually, I found this similar question, with some much better answers: http://stackoverflow.com/questions/7589059/whats-the-most-elegant-way-to-implement-a-digest-email-without-reinventing-a-que – bricker Oct 07 '11 at 10:44

2 Answers2

0

I recommend setting up a cron that will run once every hour and send out e-mails. You'll have to create a new model, QueuedEmail or something similar, and then instead of using ActionMailer right away, save it as a QueuedEmail. This is basically the same thing that delayed_job does, but you'll have more control over when it gets run, and crons don't take up much memory.

The cron should be to script/rails runner, and you should have a method in your QueuedEmail model that will send out all pending e-mails. I recommend whenever to generate crontabs (quick to setup and very easy to use). The cron will look something like this (this example is set to run once a day at 2am, you can look up how to adjust the intervals, I don't know off the top of my head):

0 2 * * * /bin/bash -l -c 'cd /RAILS_ROOT/ && script/rails runner -e production '\''QueuedEmail.send_pending'\'' >> log/cron.log 2>&1'

Or in Whenever:

every :day, :at => '2 am' do
    runner "QueuedEmail.send_pending"
end

QueuedEmail#send_pending

class QueuedEmail < ActiveRecord::Base
    def send_pending
        all.each do |email|
            params = your_parsing_method(email.params)
            record = your_parsing_method(email.record)
            email.destroy if UserMailer.registration_confirmation(record, params).deliver
        end
    end
end

UserController#create

if @user = User.create(params[:user])
    User.delay_email(@user, params[:user])
    redirect_to user_path(@user), :notice => "Created User. E-mail will be sent within the hour."
end

User#delay_email

def delay_email(record, params)
    QueuedEmail.create(:record => your_db_formatting_method(record), :params => your_db_formatting_method(params.to_s)) # or use something built in, like to_s.
end

None of this code is tested and is probably quite broken, but it's the general idea that matters. You could also go one step further and extend the Rails ActionMailer, but that is far more advanced.

bricker
  • 8,911
  • 2
  • 44
  • 54
  • going for a cron, it again requires on heroku to have a worker. I don't want a separate worker, but other than that, I really like your approach – Nikhil Oct 07 '11 at 17:00
  • Heroku, offers a free cron option and this might be the cheapest solution for now. – Nikhil Oct 12 '11 at 20:49
0

If you are using a job only to send "signup mails" you probably be better not using a separated job for that (considering you have very low amount of emails to send).

This will simplify your configuration and will reduce your costs.

A separated job is useful if you have a task that takes a lot of time to be concluded or you have a very busy site, as it's not the case of either, it's a good idea to just send regular emails.

Gabriel Mazetto
  • 1,090
  • 1
  • 12
  • 23