Introducing ScheduledJob
TL;DR We’re open-sourcing ScheduledJob, a thin flexible interface that enables you to define recurring jobs using DelayedJob, e.g:
1
2
3
4
5
6
7
8
9
10
class WeeklyMetricJob
include ::ScheduledJob
def perform
ScheduledJob.logger.info('I need to do something over and over')
end
def self.time_to_recur(last_run_at)
last_run_at.end_of_week + 3.hours
end
During the development of RightScale Cloud Analytics, we needed a lightweight background job framework for various tasks. We run our background jobs on a ServerArray that can auto-scale.
There are a whole host of options in the background job space including:
DelayedJob made a lot of sense for us as it integrates nicely with ActiveRecord and we have used it on a couple of other projects successfully in the past. However, the problem we faced was how to make jobs run on a scheduled basis. An example of this could be a cleanup task that deletes old records from a table. Whilst this is not something that you want to run all the time, it is something that you may want to run say once a week.
There are some options for doing this, for example:
We developed ScheduledJob as none of these options were quite what we were looking because:
- In some cases we needed to be able to dynamically define when the job was to run again. This offers considerable flexibility over a static “run every X hours” type of approach. For example, we recently developed a “fast mode” for our jobs, which will exercise a callback and if true, will schedule the job to run immediately rather than the standard time-to-recur code. This wasn’t available in other frameworks.
- By building on top of DelayedJob, we can save a job including its state, and restart the app and retain that state for when the job is executed. The interface we have designed ties the logic for when the job is to recur directly into the job’s class itself rather than in a central scheduler file like Whenever for example.
Checkout the repo for details, and create issues or send pull-requests!
If you like this kind of thing, you might be interested in joining our team.