Class: RailsErrorDashboard::RetentionCleanupJob
- Inherits:
-
ApplicationJob
- Object
- ActiveJob::Base
- ApplicationJob
- RailsErrorDashboard::RetentionCleanupJob
- Defined in:
- app/jobs/rails_error_dashboard/retention_cleanup_job.rb
Overview
Background job to enforce the retention_days configuration. Deletes error logs (and their associated records) older than the configured threshold. Uses batch deletion (in_batches + delete_all) for performance on large tables.
Schedule this job daily via your preferred scheduler (SolidQueue, Sidekiq, cron).
Instance Method Summary collapse
-
#perform ⇒ Integer
Number of errors deleted.
Instance Method Details
#perform ⇒ Integer
Returns number of errors deleted.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'app/jobs/rails_error_dashboard/retention_cleanup_job.rb', line 18 def perform retention_days = RailsErrorDashboard.configuration.retention_days return 0 if retention_days.blank? cutoff = retention_days.days.ago expired_scope = ErrorLog.where("occurred_at < ?", cutoff) return 0 if expired_scope.none? deleted_count = 0 # Delete dependents first, then errors — all in batches to prevent table locks expired_ids_scope = expired_scope.select(:id) # Batch delete dependent records (occurrences, comments, cascade patterns) ErrorOccurrence.where(error_log_id: expired_ids_scope).in_batches(of: 1000).delete_all ErrorComment.where(error_log_id: expired_ids_scope).in_batches(of: 1000).delete_all CascadePattern.where(parent_error_id: expired_ids_scope) .or(CascadePattern.where(child_error_id: expired_ids_scope)) .in_batches(of: 1000).delete_all # Now batch delete the error logs themselves expired_scope.in_batches(of: 1000) do |batch| batch_size = batch.delete_all deleted_count += batch_size end if deleted_count > 0 RailsErrorDashboard::Logger.info( "[RailsErrorDashboard] Retention cleanup: deleted #{deleted_count} errors older than #{retention_days} days" ) end deleted_count rescue => e RailsErrorDashboard::Logger.error("[RailsErrorDashboard] Retention cleanup failed: #{e.class} - #{e.}") 0 end |