Using delayed job in low demand system

Delayed Job is one of the most popular background processing libraries in rails. Common practices are to run several Delayed Job workers and each worker will check the database periodically and executes the pending jobs.

The advantage is the pending jobs will get execute very quickly, typically within 5 seconds. This is good for critical jobs. However, running the workers all the time requires resources and periodically checking the database will increase the load. In reality, if you are just using delayed job for sending out welcome emails, you can actually collect and execute in batches. This is especially helpful when you started with a low spec server and when you don't actually have so many sign-ups.

Delayed Job has a method to run all available jobs and exit. To do this, you can stop running the workers and create a Cron job via whenever gem:

every 2.minutes do
  rake 'jobs:workoff'
end

Then deploy it:

whenever -w

Delayed Job will now start every 2 minutes, clear the jobs and kill itself.


AI Summary
Chrome On-device AI 2024-07-27 11:45:39

Share Article