9514: add rake task to delete logs for old containers whose log is already stored...
[arvados.git] / services / api / lib / tasks / delete_old_container_logs.rake
1 # This task finds containers that have been finished for at least as long as
2 # the duration specified in the `clean_container_log_rows_after` config setting,
3 # and deletes their stdout, stderr, arv-mount, crunch-run, and  crunchstat logs
4 # from the logs table.
5
6 namespace :db do
7   desc "Remove old container log entries from the logs table"
8   task delete_old_container_logs: :environment do
9     Log.select("logs.id").
10         joins("JOIN containers ON object_uuid = containers.uuid").
11         where("event_type in ('stdout', 'stderr', 'arv-mount', 'crunch-run', 'crunchstat') AND containers.log IS NOT NULL AND containers.finished_at < :age",
12               age: Rails.configuration.clean_container_log_rows_after.ago).
13         find_in_batches do |old_log_ids|
14       Log.where(id: old_log_ids.map(&:id)).delete_all
15     end
16   end
17 end