Sending Email Using Delayed_Job

I have what I consider a working setting for sending emails via Delayed_Job . However, I did not receive my test email, and you should not wait until even more with a delay of days. I need to find out:

  • What is wrong is that an email message is not sent.
  • How to test it without waiting for days at a time.

I am new to Delayed_Job , so I apologize for the newbie’s mistakes.

Here is a model that includes the send_reminder_emails method. They were fully functional without the .delay(run_at: self.mail_date) bit .delay(run_at: self.mail_date) , so at least I know that works a lot:

 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 ReminderMailer.delay(run_at: self.mail_date).reminder_send(self.user, self).deliver_now self.create_mail_date end end handle_asynchronously :send_reminder_emails end 

Links to schedule are linked to the Ice_Cube stone, and all the date information has been checked using my console and it works. 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 my readme for Rails 4. Any help in getting the deferred part of this handwriting is smoothed out!

+6
source share
3 answers

You can use Active Job to send emails by scheduling a task at a specific time.

An active task is the basis for the announcement of tasks and their launch on various queue servers. These tasks can be everything from regularly scheduled fees, to billing, to mailing lists. All that can be cut into small units of work and work in parallel, really.

To start and execute tasks in production, you need to configure the server queue, that is, you need to decide for any third queue of the queue library that Rails should use. The rails themselves provide only a processing system in the process that only saves jobs in RAM. If the process failed, or the machine is reset, then all outstanding tasks will be lost using the built-in default async. This may be good for small applications or non-critical tasks, but most production applications will need to choose a permanent backend.

The active job has built-in adapters for several queue servers (Sidekiq, Resque, Delayed Job, and others). For an updated list of adapters, see API Documentation for ActiveJob :: QueueAdapters.

After setting up the queue adapters, create a Job to send emails

 class RemainderMailerJob < ApplicationJob queue_as :default def perform(user, remainder) ReminderMailer.reminder_send(user, reminder).deliver end end 

To send mail at a specific time, you need to queue a task and run it at a specific time. If you need to send mail in 7 days, then name this piece of code where you need it.

 RemainderMailerJob.set(wait: 1.week).perform_later(user, remainder) 

Now this task will be completed in 1 week, which in turn calls the mail program at that time, so your letters will be sent on a certain day.

IF you know a specific time by date, you can use

 RemainderMailerJob.set(wait_untill: <Specific date>).perform_later(user, remainder) 

If you have any doubts about active work, kindly refer to Active Work Basics

+7
source

When sending letters using a delayed task, there is no need to add "deliver" or "deliver" when initializing delayed_job for the mail program. So,

 ReminderMailer.delay(run_at: self.mail_date).reminder_send(self.user, self) 

he will work.

Convert your "self.mail_date" to indicate the time at which you want to send mail. Transferring only dates will not work. Basically, run_at should indicate the date and time.

Please check the delayed_job github link under the heading "Rails 3 Mailers" https://github.com/collectiveidea/delayed_job

+4
source

I saw how this happens when attribute protection is misconfigured. (Usually on legacy applications that left some kludge when upgrading the rails version.)

See if you can set the field from the console: Delayed::Job.new(run_at: 1.year.from_now) . If not, this is a criminal, and you will need to find the reason in your application.

If this works, we still need to check if this is an application problem or a DJ bug. Create a new minimal rail application using only the delayed_job table and try to reproduce this. If you can not do something else strange in your application.

0
source

All Articles