Sha256: 27f15b07ae45e8ccf478ad3951904b9eb62c59a0cb7143e362a51d8e9915de13
Contents?: true
Size: 1.34 KB
Versions: 27
Compression:
Stored size: 1.34 KB
Contents
# frozen_string_literal: true module Cron # # Clean up the collection items that have not been updated in 30 days # class TrimCollection < Job cron_tab_entry :daily # # Fetch each item and delete it if hasn't been updated in 30 days # def execute count = 0 total = collection.count while count <= total collection.limit(250).skip(count).each { |item| trim_item(item) if archive?(item) } count += 250 end end # # Try to safely destroy the item, warn if not successful # def trim_item(item) item.destroy rescue StandardError => error App47Logger.log_error "Unable to destroy item #{item.inspect}", error end # # Test if this should be archived # def archive?(item) item.updated_at < allowed_time_for_item(item) rescue StandardError => error App47Logger.log_warn "Unable to archive item #{item.inspect}", error false end # # Try to get a TTL from System configuration, otherwise return the default # def allowed_time_for_item(item) SystemConfiguration.send("#{item.class.to_s.underscore}_ttl").days.ago rescue StandardError allowed_time end # # Allowed time the amount of time allowed to exists before deleting # def allowed_time 30.days.ago end end end
Version data entries
27 entries across 27 versions & 1 rubygems