Merge branch '16387-batch-update-deactivated-user'
[arvados.git] / services / api / lib / sweep_trashed_objects.rb
index 1dc45a0ca14a6f8ff6205ae5dd21f6aff42734ac..8613c749cf247c6c11f309c4d43cddc544e99b4f 100644 (file)
@@ -7,7 +7,7 @@ require 'current_api_client'
 module SweepTrashedObjects
   extend CurrentApiClient
 
-  def delete_project_and_contents(p_uuid)
+  def self.delete_project_and_contents(p_uuid)
     p = Group.find_by_uuid(p_uuid)
     if !p || p.group_class != 'project'
       raise "can't sweep group '#{p_uuid}', it may not exist or not be a project"
@@ -20,7 +20,7 @@ module SweepTrashedObjects
     # exceptions, and delete records owned by this project
     skipped_classes = ['Group', 'User']
     ActiveRecord::Base.descendants.reject(&:abstract_class?).each do |klass|
-      if !skipped_classes.include?(klass.name) && klass.columns.collect{|c| c.name}.include?('owner_uuid')
+      if !skipped_classes.include?(klass.name) && klass.columns.collect(&:name).include?('owner_uuid')
         klass.where({owner_uuid: p_uuid}).destroy_all
       end
     end
@@ -48,12 +48,15 @@ module SweepTrashedObjects
         where({group_class: 'project'}).
         where('is_trashed = false and trash_at < statement_timestamp()').
         update_all('is_trashed = true')
+
+      # Sweep expired tokens
+      ActiveRecord::Base.connection.execute("DELETE from api_client_authorizations where expires_at <= statement_timestamp()")
     end
   end
 
   def self.sweep_if_stale
-    return if Rails.configuration.trash_sweep_interval <= 0
-    exp = Rails.configuration.trash_sweep_interval.seconds
+    return if Rails.configuration.Collections.TrashSweepInterval <= 0
+    exp = Rails.configuration.Collections.TrashSweepInterval.seconds
     need = false
     Rails.cache.fetch('SweepTrashedObjects', expires_in: exp) do
       need = true