CronTable
Basic cron-like system to schedule jobs
Setup
- Add
cron-table
gem - Run
rails app:cron_table:install:migrations
- Run
rails db:migrate
to apply migrations
Usage
- Add
include CronTable::Schedule
to 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::Duration
like1.hour
,2.days
- Predefined intervals
midnight
noon
beginning_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.