DestroySoon encapsulates the common pattern of asynchronously removing ActiveRecord models. In complex systems where there is a God entity, the God descruction take a long time to run due to all dependent destruction. In this case it's a good ideia to place the heavy weight job to the background.
DestroySoon uses DelayedJob as queue processing system.
Add DestroySoon to your gemfile:
gem 'destroy_soon', :git => 'git://github.com/redu/destroy-soon.git'
You just need to inject some behaivor to the object you want to destroy asynchronously:
class Course < ActiveRecord::Base include DestroySoon::ModelAdditions end
The ActiveRecord model should have a new column called
destroy_soon. This column will be setted to true when a destruction task is scheduled. You should generate a migration as follows:
$ rails g migration AddDestroySoonToCourse
Edit the migration to add the column:
class AddDestroySoonToCourse < ActiveRecord::Migration def self.up add_column :courses, :destroy_soon, :boolean, :default => false add_index :courses, :destroy_soon end def self.down remove_column :courses, :destroy_soon end end
And migrate the database using
rake db:migrate. In order to destroy Course instance in the backgroud call the
class CoursesController < BaseController def destroy @course = Course.find(params[:id]) @course.async_destroy respond_with(@course) end end
You can use DelayedJob's named queues setting the queue name as follows:
# config/initializers/destroy_soon.rb DestroySoon::Queue.default_queue = "destruction"
This project is maintained and funded by Redu Educational Techologies.
Copyright (c) 2012 Redu Educational Technologies
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.