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 find_each + destroy to respect dependent: :destroy on associations.
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 |
# 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 deleted_count = 0 # Use find_each to process in batches (default 1000) # destroy triggers dependent: :destroy on associations (occurrences, comments, cascades) ErrorLog.where("occurred_at < ?", cutoff).find_each do |error_log| error_log.destroy deleted_count += 1 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 |