pg_cron jobs scheduling for Rails applications
By default, no configuration is needed to use this gem.
PgCronRails schedules your pg_cron jobs in the default PSQL database using your current database connection user.
You can also customize PgCronRails database connection configuration by creating an initializer:
PgCronRails.configure do |config|
config.connection_config = {
username: username
password: password
database: database
}
end
Create pg_cron job
- Generate a job:
rails generate pg_cron_job daily_vacuum
create db/pg_cron_jobs/daily_vacuum.yml
create db/migrate/[TIMESTAMP]_add_daily_vacuum_pg_cron_job.rb
- Define
db/pg_cron_jobs/daily_vacuum.yml
job params.
daily_vacuum:
cron: "3 5 * * *"
statement: "VACUUM VERBOSE ANALYZE"
database: <%= ActiveRecord::Base.connection.current_database %> # This parameter is optional
- Run the migration
rails db:migrate
The methods added to ActiveRecord::Migration
are defined in PgCronRails::Statements
The gem is available as open source under the terms of the MIT License.