Send a reminder mail to a user one day before

How can I send an email to user at a specific time that he has set himself?

For example, he has decided that he wants to start a particular activity on 15th feb. Now I want to send him a mail one day before 15th feb.

Currently this is what I have thought of but this checks everyday if there is any such user whose activity starts in one day. Is there a way that eliminates the checks and instead does a triggering?

scheduler.cron("0 5 * * *") do
  UserActivity.all.each do |user_activity|
    if (Time.now - user_activity.start_time )/1.day < 1
      ChallengeNotifier.send_challenge_start_notifier(user_activity.user).deliver
    end
  end
end

Hey Charlie!

If you don’t mind using a gem you could use something like Delayed Job or Sidekiq. I would start with Delayed Job since tasks are persisted to Postgres so you won’t have to set up an instance of Redis or anything in production. Delayed Job both creates a queue of tasks and also allows you to define specifically when the task should be fired off in the future. Seems pretty perfect for your use case.

But I don’t know what exactly when the email should be sent. It depends on when the user sets the date to. So if he has 15th feb then I need to send him an email on 14th. I believe even if I use Sidekiq I will tell the worker when to send the email.

So my question is to trigger an email automatically. For example, the date set on the database is 15th feb. As soon as 14th feb is reached then rails app should send an email on its own without me running a cron job every day for the check.

I’m not aware of a way to do this without some sort of task that runs daily.

So this is the only way right

scheduler.cron("0 5 * * *") do
  UserActivity.all.each do |user_activity|
    if (Time.now - user_activity.start_time )/1.day &lt; 1
      ChallengeNotifier.send_challenge_start_notifier(user_activity.user).deliver
    end
  end
end

What do you mean by “only way”?

Cron is one way to run repeated tasks. There are almost definitely others.

The code you’ve written is one way to accomplish this. There are definitely others.

I feel like a lot of your questions ask “what is the best way?” or “what is the only way?”. There is almost never an answer to these questions. There are many ways to do things; they have certain advantages and disadvantages. It would be good to get in the habit of looking for many ways and choosing the one that seems best to you when you weigh the pros and cons.

Anyone that says “this is the best way to do this” should be distrusted.

No, I did not mean to ask the only way. I wanted to know what are the other ways except this and using delayed jobs. I just wanted to know the alternatives. I try doing things my way but I am damn sure there are many other better practices to solve the same question.

Actually, I’m pretty sure @benorenstein is right here. If it’s possible for the start time of the activity to change, and you want the time the email is sent to change based on that, a daily cron job is pretty much your only option.