Mark Locklear
Mark Locklear

Reputation: 5325

Getting started: Sending an email in Rails with delayed job send email

I am new to delayed job. I am trying to configure delayed job to send an email. In my model I have...

  def send_reminder_emails
    NsoMailer.send_reminder_emails(self)
  end

I have the appropriate send_reminder_emails action in app/mailers/nso_mailer.rb, and a test email in registrations/reminder_email.html.erb.

I have followed directions in https://github.com/collectiveidea/delayed_job to install the delayed job and daemons gems. Now my question is how to configure the job? In this case I think the job should be Delayed::Job.enqueue Registrations.send_email_reminders??? Documentation seems a little lacking on the github wiki. I know there is the script/delayed_job file. Should I be modifying this file?

If anyone can suggest a getting started page or blog that would be nice. I'm running this on my own server (no heroku), also we have our own SMTP server (so no sendgrid or 3rd party mail services). Mail is already configured on the server and I am successfully sending emails. My goal with delayed_job is to send some reminder emails once per day.

Any help or nudge in the right direction greatly appreciated.

Upvotes: 0

Views: 2115

Answers (1)

Xaid
Xaid

Reputation: 359

If you want to call the method on your model asynchronously then you can do the following:

model_instance.delay.send_reminder_email

If you want to call a method on the mailer directly then you'll do something like (I'm assuming that the method name defined in nso_mailer.rb is reminder_emails):

NsoMailer.delay.reminder_emails(model_instance)

In my projects, I usually have either a service object take care of all the notifications or create a concern which handles the sending of emails.

A good resource to look at is Ryan Bates's Railcast titled 'Service Objects' or read DHH's blog post on using concerns Put chubby models on a diet with concerns

As for your setup questions, if you are deploying via capistrano then you can call bundle exec bin/delayed_job restart after the deployment is successful to restart the DJ daemon.

Upvotes: 0

Related Questions