Sha256: 054b8b8c87c22f227d38bb20ce21f60eb18cc5f4a43bb98ccd201291b67356d9

Contents?: true

Size: 1.37 KB

Versions: 8

Compression:

Stored size: 1.37 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 perform
      # Rails.cache.reconnect
      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

8 entries across 8 versions & 1 rubygems

Version Path
web47core-0.6.3 lib/app/jobs/cron/trim_collection.rb
web47core-0.6.2 lib/app/jobs/cron/trim_collection.rb
web47core-0.6.1 lib/app/jobs/cron/trim_collection.rb
web47core-0.6.0 lib/app/jobs/cron/trim_collection.rb
web47core-0.5.5 lib/app/jobs/cron/trim_collection.rb
web47core-0.5.4 lib/app/jobs/cron/trim_collection.rb
web47core-0.5.3 lib/app/jobs/cron/trim_collection.rb
web47core-0.5.2 lib/app/jobs/cron/trim_collection.rb