Class: Cron::TrimCollection
- Inherits:
-
Job
- Object
- ActiveJob::Base
- ApplicationJob
- Job
- Cron::TrimCollection
- Defined in:
- lib/app/jobs/cron/trim_collection.rb
Overview
Clean up the collection items that have not been updated in 30 days
Direct Known Subclasses
TrimAuditLogs, TrimCronServers, TrimFailedDelayedJobs, TrimNotifications
Instance Method Summary collapse
-
#allowed_time ⇒ Object
Allowed time the amount of time allowed to exists before deleting.
-
#allowed_time_for_item(item) ⇒ Object
Try to get a TTL from System configuration, otherwise return the default.
-
#archive?(item) ⇒ Boolean
Test if this should be archived.
-
#perform ⇒ Object
Fetch each item and delete it if hasn’t been updated in 30 days.
-
#trim_item(item) ⇒ Object
Try to safely destroy the item, warn if not successful.
Methods inherited from Job
cron_tab_entry, #send_support_email
Methods inherited from ApplicationJob
valid_environment?, valid_environments
Methods included from App47Logger
clean_params, #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_time ⇒ Object
Allowed time the amount of time allowed to exists before deleting
53 54 55 |
# File 'lib/app/jobs/cron/trim_collection.rb', line 53 def allowed_time 30.days.ago end |
#allowed_time_for_item(item) ⇒ Object
Try to get a TTL from System configuration, otherwise return the default
44 45 46 47 48 |
# File 'lib/app/jobs/cron/trim_collection.rb', line 44 def allowed_time_for_item(item) SystemConfiguration.send("#{item.class.to_s.underscore}_ttl").days.ago rescue StandardError allowed_time end |
#archive?(item) ⇒ Boolean
Test if this should be archived
34 35 36 37 38 39 |
# File 'lib/app/jobs/cron/trim_collection.rb', line 34 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 |
#perform ⇒ Object
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| trim_item(item) if archive?(item) } count += 250 end end |
#trim_item(item) ⇒ Object
Try to safely destroy the item, warn if not successful
25 26 27 28 29 |
# File 'lib/app/jobs/cron/trim_collection.rb', line 25 def trim_item(item) item.destroy rescue StandardError => error App47Logger.log_error "Unable to destroy item #{item.inspect}", error end |