views:

364

answers:

2

I've got an app that could benefit from delayed_job and some background processing. The thing is, I don't really need/want delayed_job workers running all the time.

The app runs in a shared hosting environment and in multiple locations (for different users). Plus, the app doesn't get a large amount of usage.

Is there a way to start and stop processing jobs (either with the script or rake task) from my app only after certain actions/events?

+1  A: 

You could call out to system:

system "cd #{Rails.root} && rake delayed_job:start RAILS_ENV=production"

You could just change delayed_job to check less often too. Instead of the 5 second default, set it to 15 minutes or something.

wesgarrison
A: 

Yes, you can, but I'm not sure what the benefit will be. You say you don't want workers running all the time - what are your concerns? Memory usage? Database connections?

To keep the impact of delayed_job low on your system, I'd run only one worker, and configure it to sleep most of the time.

Delayed::Worker::sleep_delay = 60 * 5  # in your initializer.rb

A single worker will only wake up and check the db for new jobs every 5 minutes. Running this way keeps you from 'customizing' too much.

But if you really want to start a Delayed::Worker programatically, look in that class for work_off, and implement your own script/run_jobs_and_exit script. It should probably look much like script/delayed_job does - 3 lines.

Jonathan Julian
Is that 60 seconds or 60 mins?...I'm using Heroku, so I can't manually start/stop server like I would normally, so I'm trying to configure it so that the jobs (beta invitations email) are only sent once every day or so.
Senthil
`sleep_delay` is in seconds.
Jonathan Julian