Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on rails - need to send messages to emails at a particular time a week

I would like to know how i should go about with this project. I need to send emails to people once a week. However this has to be auto generated and sent at a certain time every week. How hard is this to code? I need to know how if there are any books that could be of help or if any of you could direct me. It has to be programmed using ruby on rails. Hence there is a web service and database integrated. Cheers

like image 623
CodeGeek123 Avatar asked Sep 20 '11 16:09

CodeGeek123


4 Answers

Why is this complex?

All you need is to schedule a job. You can use Delayed::Job for instance. Delayed::Job gives you the possibility to schedule a job at specific time using the run_at symbol like this:

Delayed::Job.enqueue(SendEmailJob.new(...), :run_at => scheduled_at)    

Your job is a class that has to implement the perform method. Inside this method you can call the mailer responsible for sending the email. The scheduled_at can be stored on the database and updated each time the perform method runs.

like image 187
Amokrane Chentir Avatar answered Nov 09 '22 01:11

Amokrane Chentir


For books, chapter 6 of Rails Recipes is devoted to email. The book Advanced Rails Recipes has chapters on Asynchronous Processing and email. There are also railscast devoted to sending email and writing custom deamons.

like image 20
David Nehme Avatar answered Nov 09 '22 01:11

David Nehme


You can use a gem like whenever to schedule recurring tasks.

every :sunday, :at => '12pm' do
  runner "User.send_emails"       
end
like image 36
Harish Shetty Avatar answered Nov 09 '22 01:11

Harish Shetty


Maybe you can try clockwork

require 'clockwork'
include Clockwork

handler do |job|
  puts "Running #{job}"
end

every(10.seconds, 'frequent.job')
every(3.minutes, 'less.frequent.job')
every(1.hour, 'hourly.job')

every(1.day, 'midnight.job', :at => '00:00')
like image 1
Zhihao Yang Avatar answered Nov 09 '22 01:11

Zhihao Yang