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.
Add this line to your Rails application's Gemfile:
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.
Run maintenance jobs on deploy
bin/rails maintenance_job:execute_pending_jobs to your deploy script right after you run the database migrations.
Creating a maintenance job
To create a new maintenance job, run:
$ bin/rails generate maintenance_job job_to_be_done
This will create two files:
Implement your data manipulation, write some tests for it and deploy!
Ensuring all jobs have been run in local environments
If you'd like to ensure any pending maintenance jobs have been run in the development environment, add the following line to your
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 cloneto 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.