CodeGeek123
CodeGeek123

Reputation: 4501

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

Upvotes: 5

Views: 2361

Answers (4)

Zhihao Yang
Zhihao Yang

Reputation: 1965

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')

Upvotes: 1

Amokrane Chentir
Amokrane Chentir

Reputation: 30405

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.

Upvotes: 5

Harish Shetty
Harish Shetty

Reputation: 64373

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

every :sunday, :at => '12pm' do
  runner "User.send_emails"       
end

Upvotes: 3

David Nehme
David Nehme

Reputation: 21597

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.

Upvotes: 3

Related Questions