Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I would use <a href="http://github.com/tobi/delayed_job" rel="noreferrer">DelayedJob</a> for this ( assuming you are not sending large number of emails emails a day, i.e. 100's of thousands per day etc.)</p> <pre><code>class Email &lt; ActiveRecord::Base belongs_to :campaign after_create :schedule_email_dispatch def schedule_email_dispatch send_at(campaign.created_at + self.days.days, :send_email) end def send_email end end </code></pre> <p>Run the workers using the rake task:</p> <pre><code>rake jobs:work </code></pre> <p>Every time a new Email object is created a delayed job item is added to the queue. At the correct interval the email will be sent by the worker.</p> <pre><code>@campaign = Compaign.new(...) @campaign.emails.build(:days =&gt; 1) @campaign.emails.build(:days =&gt; 2) @campaign.save # now the delay </code></pre> <p>In the example above, two delayed job entries will be created after saving the campaign. They are executed 1 and 2 days after the creation date of the campaign.</p> <p>This solution ensures emails are sent approximately around the expected schedule times. In a cron job based solution, disptaching happens at the cron intervals. There can be several hours delay between the intended dispatch time and the actual dispatch time.</p> <p>If you want to use the cron approach do the following:</p> <pre><code>class Email &lt; ActiveRecord::Base def self.dispatch_emails # find the emails due for dispatch Email.all(:conditions =&gt; ["created_at &lt;= DATE_SUB(?, INTERVAL days DAY)", Time.now]).each do |email| email.send_email end end end </code></pre> <p>In this solution, most of the processing is done by the DB. </p> <p>Add <code>email.rake</code> file in <code>lib/tasks</code> directory:</p> <pre><code>task :dispatch_emails =&gt; :environment do Email.dispatch_emails end </code></pre> <p>Configure the cron to execute <code>rake dispatch_emails</code> at regular intervals( in your case &lt; 24 hours)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload