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

Version Path
web47core-3.2.20 lib/app/jobs/cron/trim_collection.rb
web47core-3.2.19 lib/app/jobs/cron/trim_collection.rb
web47core-3.2.18 lib/app/jobs/cron/trim_collection.rb
web47core-3.2.17 lib/app/jobs/cron/trim_collection.rb
web47core-3.2.16 lib/app/jobs/cron/trim_collection.rb
web47core-3.2.15 lib/app/jobs/cron/trim_collection.rb
web47core-3.2.14 lib/app/jobs/cron/trim_collection.rb
web47core-3.2.13 lib/app/jobs/cron/trim_collection.rb
web47core-3.2.12 lib/app/jobs/cron/trim_collection.rb
web47core-3.2.9 lib/app/jobs/cron/trim_collection.rb
web47core-3.2.8 lib/app/jobs/cron/trim_collection.rb
web47core-3.2.7 lib/app/jobs/cron/trim_collection.rb
web47core-3.2.6 lib/app/jobs/cron/trim_collection.rb
web47core-3.2.5 lib/app/jobs/cron/trim_collection.rb
web47core-3.2.4 lib/app/jobs/cron/trim_collection.rb
web47core-2.2.20 lib/app/jobs/cron/trim_collection.rb
web47core-2.2.19 lib/app/jobs/cron/trim_collection.rb
web47core-3.2.3 lib/app/jobs/cron/trim_collection.rb
web47core-3.2.2 lib/app/jobs/cron/trim_collection.rb
web47core-2.2.15 lib/app/jobs/cron/trim_collection.rb