Merge branch '18681-install-passenger-only-when-required'
[arvados.git] / services / api / lib / tasks / delete_old_container_logs.rake
index 8c32331dde5ac167559afa06986ea6c7ecbc13d5..7a0ab3826ab1c08beee1361ff81b654b4ccff86d 100644 (file)
@@ -1,3 +1,7 @@
+# Copyright (C) The Arvados Authors. All rights reserved.
+#
+# SPDX-License-Identifier: AGPL-3.0
+
 # This task finds containers that have been finished for at least as long as
 # the duration specified in the `clean_container_log_rows_after` config setting,
 # and deletes their stdout, stderr, arv-mount, crunch-run, and  crunchstat logs
@@ -5,13 +9,10 @@
 
 namespace :db do
   desc "Remove old container log entries from the logs table"
+
   task delete_old_container_logs: :environment do
-    Log.select("logs.id").
-        joins("JOIN containers ON object_uuid = containers.uuid").
-        where("event_type in ('stdout', 'stderr', 'arv-mount', 'crunch-run', 'crunchstat') AND containers.log IS NOT NULL AND containers.finished_at < :age",
-              age: Rails.configuration.clean_container_log_rows_after.ago).
-        find_in_batches do |old_log_ids|
-      Log.where(id: old_log_ids.map(&:id)).delete_all
-    end
+    delete_sql = "DELETE FROM logs WHERE id in (SELECT logs.id FROM logs JOIN containers ON logs.object_uuid = containers.uuid WHERE event_type IN ('stdout', 'stderr', 'arv-mount', 'crunch-run', 'crunchstat') AND containers.log IS NOT NULL AND now() - containers.finished_at > interval '#{Rails.configuration.Containers.Logging.MaxAge.to_i} seconds')"
+
+    ActiveRecord::Base.connection.execute(delete_sql)
   end
 end