Sha256: fcde55a4c26cdad6d7feeab4ca1633a60f8d717eb0f9c18ff46354f031b580b4
Contents?: true
Size: 1.48 KB
Versions: 33
Compression:
Stored size: 1.48 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.send(comparison_field) < allowed_time_for_item(item) rescue StandardError => error App47Logger.log_warn "Unable to archive item #{item.inspect}", error false end # # Return which field to use for comparison when trimming objects # def comparison_field :updated_at 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.utc end end end
Version data entries
33 entries across 33 versions & 1 rubygems