Greg Hewett's Blog

Just a simple journal.

Using Cron to Power Delayed_job

| Comments

I would not consider myself cheap, but…

Heroku workers are really expensive for the work that I need to do. I am setting up a really simple email newsletter for a website. Sending 100+ emails in the request would not be a smart thing to do, so I am looking at an alternative to using a worker.

The first option looks to be cron. By cracking open the delayed_job source code, I discover there is a class method that will do exactly what we want to do. We want to create a command to execute that will process a few jobs then exit.

I create the cron.rake file in /lib/tasks and the contents looks like this:

ruby task :cron => :environment do Delayed::Job.work_off(200) end

The option that is passed to work_off is the maximum number of jobs to process. When configuring this, remember that Heroku limits cron execution to 30 seconds. You can do lots of small jobs, like sending email, or you can can do just a few jobs that take more time.