开发者

how do i use delayed_job on heroku to send out emails that are batched?

开发者 https://www.devze.com 2023-01-13 01:55 出处:网络
I read the documentation on workers and delayed_job and couldn\'t follow exactly, so wanted to get head-start with some strategy and sample code.

I read the documentation on workers and delayed_job and couldn't follow exactly, so wanted to get head-start with some strategy and sample code.

I have a controller which I use to send emails one by one. Now each day I want to check which emails need to be sent for the day, and then send them through heroku as a delayed_job.

How do I begin to approach this? thanks.

This is what I'm coming up with based on the answers:

Using the 'whenever' gem, I created the following schedule.rb

  every 1.day, :at => '4:30 am' do

    heroku 开发者_StackOverflow= Heroku::Client.new(ENV['HEROKU_USER'], ENV['HEROKU_PASS'])
    heroku.set_workers(ENV['HEROKU_APP'], 1)

    Contact.all.each do |contact|
      contact_email = contact.email_today

      unless contact.email_today == "none"
        puts contact.first_name
        puts contact_email.days
        puts contact.date_entered

        Delayed::Job.enqueue OutboundMailer.deliver_campaign_email(contact,contact_email)
      end

    end

    heroku.set_workers(ENV['HEROKU_APP'], 0)
  end

To determine whether I should send an email today or not, I created the method for contact.rb:

  def email_today

    next_event_info = self.next_event_info # invokes method for contact

    next_event = next_event_info[:event]
    delay = next_event_info[:delay]

    if next_event.class.name == "Email" && from_today(self, next_event.days) + delay < 0 #helper from_today
      return next_event      
    else
      return "none"
    end

  end

Does this look right? I am developing on windows and deploying to heroku so don't know how to test it...thanks!


If you're sending emails out once a day, you probably want to start by add the cron addon to your application, which will fire a rake task once-per day.

Obviously, you'll also need to add the delayed_job plugin (http://docs.heroku.com/delayed-job). In addition, your app will need to be running at least one worker.

Then it's just a matter of doing your mail work from within your cron rake task. For example, if you had a mailer called 'UserMailer', your cron could look something like this:

#lib/cron.rb
task :cron => :environment do
  User.all.each do |user|
    Delayed::Job.enqueue UserMailer.deliver_notification(user)
  end
end

If you're only using background tasks to send these emails, you could probably add also some logic in your cron task, and your mailer methods to add and remove workers as required, which will save you having to pay for the workers while they're not in use.

0

精彩评论

暂无评论...
验证码 换一张
取 消