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

Version Path
web47core-1.0.10 lib/app/jobs/cron/trim_collection.rb
web47core-1.0.8 lib/app/jobs/cron/trim_collection.rb
web47core-1.0.7 lib/app/jobs/cron/trim_collection.rb
web47core-1.0.6 lib/app/jobs/cron/trim_collection.rb
web47core-1.0.5 lib/app/jobs/cron/trim_collection.rb
web47core-1.0.4 lib/app/jobs/cron/trim_collection.rb
web47core-1.0.3 lib/app/jobs/cron/trim_collection.rb
web47core-1.0.2 lib/app/jobs/cron/trim_collection.rb
web47core-1.0.1 lib/app/jobs/cron/trim_collection.rb
web47core-1.0.0 lib/app/jobs/cron/trim_collection.rb
web47core-0.9.9 lib/app/jobs/cron/trim_collection.rb
web47core-0.9.8 lib/app/jobs/cron/trim_collection.rb
web47core-0.9.7 lib/app/jobs/cron/trim_collection.rb
web47core-0.9.6 lib/app/jobs/cron/trim_collection.rb
web47core-0.9.0 lib/app/jobs/cron/trim_collection.rb
web47core-0.8.5 lib/app/jobs/cron/trim_collection.rb
web47core-0.8.3 lib/app/jobs/cron/trim_collection.rb
web47core-0.8.2 lib/app/jobs/cron/trim_collection.rb
web47core-0.8.1 lib/app/jobs/cron/trim_collection.rb
web47core-0.8.0 lib/app/jobs/cron/trim_collection.rb