Ask A Question

Notifications

You’re not receiving notifications from this thread.

Send an Email On a Date Specified in a Database Column

Liz Bayardelle asked in Rails

I have what I think is a working setup to send emails via Delayed_Job. However, I haven't received my test email and it isn't practical to wait for more to happen with a delay of days. I need to figure out:

  1. What's wrong that's causing the email not to send.
  2. How to test it without waiting days at a time.

I'm new to Delayed_Job, so pardon the newbie mistakes.

Here's the model that includes the send_reminder_emails method. They were fully functional without the delay bit, so at least I know that much works:

class Reminder < ActiveRecord::Base
  belongs_to :user
  before_save :create_mail_date
  after_save :send_reminder_emails
  extend FriendlyId
  friendly_id :name, use: [:slugged, :finders]

  def create_mail_date
    @schedule = IceCube::Schedule.new(self.date)
    case self.repeating
    when "Weekly"
        @schedule.add_recurrence_rule(
            IceCube::Rule.weekly
        )
    when "Monthly"
        @schedule.add_recurrence_rule(
            IceCube::Rule.monthly.day_of_month(self.date.mon)
        )
    when "Yearly"
        @schedule.add_recurrence_rule(
            IceCube::Rule.yearly.day_of_year(self.date.yday)
        )
    end
    if self.repeating
        self.date = @schedule.next_occurrence(Time.now)
    end
    self.mail_date = self.date - 7.days
  end

  private

  def send_reminder_emails
      if self.reminder
            RemainderMailerJob.set(wait_until: self.mail_date).perform_later(user, remainder)  ##this line is the issue
            #ReminderMailer.delay(run_at: self.mail_date).reminder_send(self.user, self)ow
         self.create_mail_date   ##this is another solution I've tried
     end
  end
  handle_asynchronously :send_reminder_emails

end

The references to schedule are via the Ice_Cube gem and all of the date stuff has been tested via my console and is working. Here is my reminder_mailer.rb:

class ReminderMailer < ApplicationMailer
  default from: "man@manlyartofbbq.com"

  def reminder_send(user, reminder)
    @user = user
    @reminder = reminder

    mail(to: user.email, subject: "Reminder! #{reminder.name} is fast approaching!")
  end
end

I installed Delayed_Job step by step from their readme for Rails 4. Any help getting the delayed part of this mailer ironed out is appreciated!

Reply

Hi Liz,

Scheduling can be kind of difficult to get a handle on.

Are you running the daemon on your development environment? (The script that manages/runs delayed_jobs in the background.) This will essentially create a worker process to queue up your jobs and process them, if I'm not mistaken. I haven't worked directly with delayed_job, but it's similar to Resque and Sidekiq.

You should also be able to queue a job for a few seconds in the future from the command line for testing purposes. :)

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 87,677+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.