Class: Cron::TrimCollection

Inherits:
Job show all
Defined in:
lib/app/jobs/cron/trim_collection.rb

Overview

Clean up the collection items that have not been updated in 30 days

Instance Method Summary collapse

Methods inherited from Job

cron_tab_entry, #send_support_email

Methods inherited from ApplicationJob

valid_environment?, valid_environments

Methods included from App47Logger

#clean_params, #delete_parameter_keys, #log_controller_error, #log_debug, log_debug, log_error, #log_error, log_exception, #log_message, log_message, #log_warn, log_warn, #mask_parameter_keys, #update_flash_messages

Instance Method Details

#allowed_timeObject

Allowed time the amount of time allowed to exists before deleting



32
33
34
# File 'lib/app/jobs/cron/trim_collection.rb', line 32

def allowed_time
  30.days.ago
end

#archive?(item) ⇒ Boolean

Test if this should be archived

Returns:

  • (Boolean)


25
26
27
# File 'lib/app/jobs/cron/trim_collection.rb', line 25

def archive?(item)
  item.updated_at < allowed_time
end

#performObject

Fetch each item and delete it if hasn’t been updated in 30 days



12
13
14
15
16
17
18
19
20
# File 'lib/app/jobs/cron/trim_collection.rb', line 12

def perform
  # Rails.cache.reconnect
  count = 0
  total = collection.count
  while count <= total
    collection.limit(250).skip(count).each { |item| item.destroy if archive?(item) }
    count += 250
  end
end