0

As part of my app I am sending out an email to many users daily. Depending on their status they will be sent one of five possible types of emails.

The logic that determines which email the user receives is fairly long.

Should I:

1) Should I create a delayed_job for each email

or

2) Send the entire logic (50 lines of Ruby) with the send commands into a single job

What are the pros/cons of either approach?

Jason
  • 1,129
  • 1
  • 9
  • 20

3 Answers3

1

If you have a huge logic i recommed not to put in the delayed job if you need to send a bunch of email. better use resque(https://github.com/resque/resque) or sidekiq(http://sidekiq.org/). as in the time of sending email it delayed job will lock your database so your performance will be low. If you have small logic and less number of email just go for delayed job for each email. as it is easy to setup and implement.

Sabyasachi Ghosh
  • 2,765
  • 22
  • 33
  • Does delayed_job really lock the db? What if I do not call the db from the delayed job, would it still lock it then? – Jason Feb 12 '14 at 07:19
  • If you have installed the delayed job gem it will create a table for storing and processing data in future. check the documentation. – Sabyasachi Ghosh Feb 12 '14 at 07:39
1

Further to Sabyasachi Ghosh's answer, here are the differences between DelayedJob and Resque:

DelayedJob relies on the DB

  • Requires ActiveRecord
  • Uses Ruby Objects (not just references)
  • Has much deeper queuing functionality (queue depth etc)
  • Runs much heavier than resque

Resque relies on Redis

  • Lightweight
  • Runs independently of ActiveRecord
  • Is meant to process references (not entire objects)


Modularity

In answer to your question, I would look at modularity

Rails' is based on the principle of DRY code -- which essentially means you need to be as modular as possible (reusing code wherever you can). This leads to efficiency & simpler development cycles

In light of this, you have to observe your queueing functionality from the perspective of modularity. What does the queuing system actually do?


It queues things

Therefore, you want to include as little code as possible in the queuing system

I would create a redis instance (you can get them on Heroku), and use resque to queue specific information (such as id or email)

This will allow you to use resque to run through the Redis list, sending as many emails as you need

Community
  • 1
  • 1
Richard Peck
  • 76,116
  • 9
  • 93
  • 147
  • The issue is that the data sent in the custom emails must be fetched at a later date (time triggered) right before the emails are sent. It there any way to keep the queues light-weight in this case? I can't think of an approach where the data collection (db access) and sending the emails would not all occur in one job as it is not possible to perform communication between various jobs. – Jason Feb 14 '14 at 02:04
0

I think so you need to send each email via delayed job because if anything happens with your delayed job like if your delayed job get crashed or stopped in that case when system will re execute your job it can cause problems so i suggest you to add each email in delayed job.

Ahmad Hussain
  • 2,443
  • 20
  • 27
  • Indeed, I had code that would flag the db each time an email was sent so that if the job failed it would not resend to users. However, if only the emails are put into the queue the only recovery option is to rerun the jobs... it would be out of rails's hands. – Jason Feb 14 '14 at 02:09
  • Delayed job make sure that all of your jobs processed successfully so in both cases rerun is the solution so if you want to put this in one rails code you can do it my point is that always try to avoid rails ActiveRecord executions from delayed job – Ahmad Hussain Feb 14 '14 at 04:37
  • The main purpose of delayed job is to process time taking processes. – Ahmad Hussain Feb 14 '14 at 04:39
  • Ahmad, why are AR calls from Delayed_job not a good idea? Too many calls to the db from potentially too many workers? – Jason Feb 15 '14 at 00:18
  • In any case you have to execute apply db calls even from delayed job. My point is delayed job has a basic purposes to execute time taking tasks in background delayed job don't do anything special so just try to give it time taking processes only. – Ahmad Hussain Feb 15 '14 at 04:07