Satchel
Satchel

Reputation: 16734

How should I arrange my models and cron rake task to send emails on intervals with different start dates?

I'm re-evaluating my model and way I am trying to solve a particular problem. I have Contacts that can belong to a Campaign, which is a series of activities such as phone calls and emails, each with a number of days assigned as an attribute determining when it should get sent.

I want the cron job to look and see emails which are due today (or maybe overdue) and fire off those emails.

Contacts all have their own separate start-dates.

Sometime a Call, which needs to take place, say, 6 days from the start, doesn't get done until 10 days. That means the email in the same campaign, set to be done 8 days from the start, needs to be delayed by 4 more days, to 12 days from the start, but only for that particular contact if the delayed Call was for the Contact.

I would like from people how they would approaching modeling this and, in particular, come up with on any given day, the right Emails to send.

Some challenges I have encountered with my approach:

1) When there is no email to send, I error out with nil. I try to catch it, wondering if there's a better way to check. How do people handle that?

2) How do you calculate the cascade delay? I use a complicated way of first checking the last date of everything that was completed (completed items have their own record in ContactEmail or ContactCalls for example). I then find the difference in the interval against the attribute 'days.' Then I add that interval to the date that last item was completed.

Upvotes: 4

Views: 261

Answers (3)

Alex Korban
Alex Korban

Reputation: 15126

I would also suggest using delayed_job to handle the sending of emails. With delayed_job, you'll have a queue of tasks with associated times. It allows you to schedule tasks for a particular time in the future. It's probably more convenient than using cron.

As far as the scheduling goes, it looks to me like the email doesn't get sent until the call is done, so the email entry in the queue should be created at the point when the app is notified of call completion. The date calculation should then be as simple as Time.now + 4.days.

If there is more than one call that has to be done before the email goes out, then you'll just need to check that ALL the calls have been completed before you put the email on the queue for sending.

Upvotes: 1

Scott Schulthess
Scott Schulthess

Reputation: 2923

1) When there is no email to send, I error out with nil. I try to catch it, wondering if there's a better way to check. How do people handle that?

Check for nil. If it's nil, do nothing.

2) How do you calculate the cascade delay? I use a complicated way of first checking the last date of everything that was completed (completed items have their own record in ContactEmail or ContactCalls for example). I then find the difference in the interval against the attribute 'days.' Then I add that interval to the date that last item was completed.

Little confusing how you described it, but it looks like "Emails" should take place 4 days after "Calls". So in your rake task to do emails, merely find all calls that were completed 4 greater than 4 days ago without any corresponding emails then email them.

Upvotes: 0

RubyRedGrapefruit
RubyRedGrapefruit

Reputation: 12224

Have you tried managing this through delayed_job instead? I've had huge success with it doing similar things, and you have all the comfort of the Rails stack instead of going to cron or rake.

Upvotes: 0

Related Questions