MaintenanceJob
is a lightweight Rails engine to run testable one-off jobs at deploy time to manipulate data in the database.
Theoretically this could be done in database migrations or rake tasks, but that's a terrible idea because the changes can't be tested. In the case of rake tasks, it would also require access to the prod environment to run the tasks which is not always feasible.
This gem was heavily inspired by this RailsConf 2020 talk by @alecclarke.
Add this line to your Rails application's Gemfile:
gem 'maintenance_job'
And then execute:
$ bundle install && bin/rails maintenance_job:install
This will copy the database migration that tracks which maintenance jobs have been run and create a directory under app/jobs
for all your maintenance jobs.
Add bin/rails maintenance_job:execute_pending_jobs
to your deploy script right after you run the database migrations.
To create a new maintenance job, run:
$ bin/rails generate maintenance_job job_to_be_done
This will create two files:
app/jobs/maintenance/job_to_be_done_job.rb
test/jobs/maintenance/job_to_be_done_job_test.rb
Implement your data manipulation, write some tests for it and deploy!
If you'd like to ensure any pending maintenance jobs have been run in the development environment, add the following line to your ApplicationController
:
include MaintenanceJob::EnsureNoPendingJobs if Rails.env.development?
If there are any pending jobs in development, the developer will see an actionable error in the browser where they can run the pending jobs and continue.
- Fork it (https://github.com/ayushn21/maintenance_job/fork)
- Clone the fork using
git clone
to your local development machine. - Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
The gem is available as open source under the terms of the MIT License.