Background process for mail delivery

here’s my problem:
I have a table with lots of emails to be sent at specified dates, and
I want a process that checks, let’s say every hour or so, that table
for mails to be sent, sends them and delete the records…

A very simple background process that call’s a method. If there was a
way of doing this not in background it’d be ok, I don’t care if it
gets stuck every hour for a few seconds…

I know about remoterb, but really… isn’t there an easier way? I don’t
want to spend too much time on this stuff!

thanks everyone!
Luca

http://seattlerb.rubyforge.org/ar_mailer/

On Fri, Sep 5, 2008 at 3:59 PM, Snaggy [email protected] wrote:

I know about remoterb, but really… isn’t there an easier way? I don’t
want to spend too much time on this stuff!

thanks everyone!
Luca


Maurício Linhares
http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/
(en)
João Pessoa, PB, +55 83 8867-7208

That thing is too big! And it’s meant for delivering mails like a
queue. I want to deliver them only when it’s time, they might stay
there for months, years…
Is there a way that doesn’t involve plugins, or just small ones?
like a thread.do stuff… I don’t know…

bye

On Sep 5, 9:01 pm, “Maurício Linhares” [email protected]

Is there some reason a cron job that POSTs an HTTP request to the Rails
app
wouldn’t do the trick?

where did my reply go ?!?

Anyway Bill, your idea sounds great, but how can I do it?
thanks!

Look at this… I know it’s a stupid try but I’d like to know why it
doesn’t work:
put in some method…

Thread.new do
while true do
Notifier.deliver_message
sleep 30
end
end

Notifier is an action mailer class.
Shouldn’t this send an email every 30 seconds? Can it access the
Notifies class?

confused…

you do this by using the SQL SSIS jobs also !!!

I have something that does exactly that.

It uses the Daemons gem. I start it by doing

ruby scripts/outgoing_email_thread.rb start/stop/restart

Basically this thread just loops forever. It wakes up and checks for
stuff to do by asking the database
“Notification.find(:all, :conditions => ‘b_distribute = true’).each do
|n|”

So for me I have a table called Notifications that things get inserted
into by my application. If they want batch mode distribution, they
set b_distribute = true. This thing gets them, does some mojo and
then sends out notifications. Don’t get caught up in my crazy code as
much as the idea of a Daemon thread you start that loops and does your
stuff. My app has about 4 of this bad boys running checking incoming
email, sending email, gathering SMS messages, etc.

#!/usr/bin/env ruby

require ‘rubygems’ # if you use RubyGems
require ‘daemons’

Daemons.run_proc(“outgoing_email_thread”, {:dir_mode => :normal, :dir
=> ‘tmp/pids’} ) do
require “/var/www/yoursite/current/config/environment.rb” # This
has to be a full path to your environment.rb file. scratched my head
here.
loop {
logger.info('outgoing_email_thread: Checking for outgoing email
messages at ’ + Time.now.to_s)

  # Find any notifications that need to be distributed
  Notification.find(:all, :conditions => 'b_distribute =

true’).each do |n|
begin
n.attach_to_subscribers
rescue Exception => exc
logger.error(“outgoing_email_thread: sending email…” +
exc.message)
logger.error(exc.backtrace.join("\n"))
end
n.update_attributes(:b_distribute => false)
end

  # Now notify any users based on Readings
  Reading.find(:all, :conditions => 'is_sent = false').each do |r|
    begin
      r.update_attribute(:is_sent, true)
      if r.connector.source.bsd.setting.mail_event &&

r.connector.source.bsd.email_tested
email = NotificationMailer.create_notify(r.connector,
r.notification)
email.set_content_type(“text/html” )
NotificationMailer.deliver(email)
end
rescue Exception => exc
logger.error(“outgoing_email_thread: sending email…” +
exc.message)
logger.error(exc.backtrace.join("\n"))
end
end
EmailMessage.find(:all, :conditions => [‘is_sent = ?’,
false]).each do |email|
email.send_me
end
sleep 13 # My lucky number
}
end

Hope this is helpful.

Mike

way of doing this not in background it’d be ok, I don’t care if it
gets stuck every hour for a few seconds…

I know about remoterb, but really… isn’t there an easier way? I
don’t
want to spend too much time on this stuff!

You want ar_mailer…

http://blog.segment7.net/articles/2006/08/15/ar_mailer

“Even deliviring email to the local machine may take too long when you
have to send hundreds of messages. ar_mailer allows you to store
messages into the database for later delivery by a separate process,
ar_sendmail.”

A cron task would also work…