2

I'm trying to send emails out of a Padrino app asynchronously - I've tried using Ruby fork command but that doesn't scale very well I think, I tried delayed_job also but have now settled on Resque (mainly because the web interface it comes with is super nice).

I have it working except I had to resort to using Pony instead of padrino-mailer to send my emails from /lib. I'd appreciate any help removing Pony from the picture. The main problem I have is that I don't know how to call padrino-mailer from outside of a controller or helper. I'm sure it's possible, and would appreciate any help. If we can get this sorted out I would think that this might be very useful to other Padrino developers - sending emails async is probably a very common core scalability requirement for web apps.

See here for info on how to install and configure Resque : https://github.com/defunkt/resque

Then look here for how to send emails async from Padrino via Resque : https://gist.github.com/1384630


An update - I switched to using the ruby mail gem for sending the emails from /lib - see the updated gist for the updated module for sending async emails via resque.

I'm still interested to know if padrino-mailer can be coerced into working from /lib - help on that is still appreciated.

1 Answers1

1

I like so much resque and I use it for some more complicated daemons where I need to prioritize queue.

For simple but very flexible daemons in cron style you can take a look at: https://github.com/daddye/foreverb

The code is quite simple:

#!/usr/bin/env ruby
require 'yaml' # not really necessary but some envs need it...
require 'rubygems' unless defined?(Gem)
require 'forever'
boot = File.expand_path('../../config/boot.rb', __FILE__)

Forever.run :fork => true do
  before :each do
    require boot
    # Here we setup app projects, if you need only once you can do
    # MyApp.setup_application!
    # Which load their dependencies
    Padrino.mounted_apps.each do |app|
      app.app_obj.setup_application!
    end
  end

  every 1.minutes do
    MyQueue.each do |q|
      MyApp.deliver(:notification, q)
      q.destroy
    end
  end
end

Place this simple file under your project_root/lib, apply chmod +x file_name run it with:

./lib/file_name start|stop|restart

Finally if you need to build it in a enqueue from your controller you can build a nubble simple queue table like:

MyQueue.create(:from => 'info@godaddy.com', :to => 'buyer@gmail.com', :subject => 'Order changed')
DAddYE
  • 1,719
  • 11
  • 16