Created
August 16, 2016 15:05
-
-
Save zenbro/577652efaf340f3f33aaa5c28d1894d2 to your computer and use it in GitHub Desktop.
Rake Task for Errbit: clear problems older than 7 days
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 4 * * * /bin/bash -l -c 'RAILS_ENV=production cd /home/errbit/current && rvm 2.2.1 do bundle exec rake custom:delete_old_problems >> log/clear_old_problems.log 2>> log/clear_old_problems.log' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace :custom do | |
desc "Clear problems older than 7 days" | |
task delete_old_problems: :environment do | |
total_count = count = Problem.where(:created_at.lt => 7.days.ago).count | |
total_removed_count = 0 | |
puts "Problems to remove: #{total_count}" | |
while count > 0 do | |
problems = Problem.where(:created_at.lt => 7.days.ago).take(10) | |
start = Time.now | |
removed_count = ProblemDestroy.execute(problems) | |
total_removed_count += removed_count | |
puts "Removed #{total_removed_count} / #{total_count} problems (#{ (Time.now - start).to_i } seconds)" | |
count -= removed_count | |
end | |
puts 'All done!' | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment