CronTable
Basic cron-like system to schedule jobs
Setup
- Add
cron-tablegem - Run
rails app:cron_table:install:migrations - Run
rails db:migrateto apply migrations
Usage
- Add
include CronTable::Scheduleto the job - Define cron schedule using
crontable(every: <interval>) - Use block if cron requires params, eg
crontable(every: 1.day) { perform_later(Time.current) }
Interval specification
Use every to specify when cron should run. Allowed values include:
- Positive
ActiveSupport::Durationlike1.hour,2.days - Predefined intervals
midnightnoonbeginning_of_hour
- Custom interval registered in
CronTable.every, eg
# config/initializers/cron_table.rb
CronTable.every[:custom] = ->(context) { rand(1.hour..1.day).from_now }
Middlewares
Cron execution can be instrumented using middleware, eg
module CronTableInstrumentation
def process(context)
ElasticAPM.with_transaction(context.cron.key, "cron") { super }
end
end
CronTable.register(CronTableInstrumentation)
License
The gem is available as open source under the terms of the MIT License.