Class: Maintenance::PurgeOldTrackingEvents
- Inherits:
-
BaseService
- Object
- BaseService
- Maintenance::PurgeOldTrackingEvents
- Defined in:
- app/services/maintenance/purge_old_tracking_events.rb
Instance Method Summary collapse
Methods inherited from BaseService
#initialize, #log_debug, #log_error, #log_info, #log_warning, #logger, #options, #tagged_logger
Constructor Details
This class inherits a constructor from BaseService
Instance Method Details
#process ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 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/services/maintenance/purge_old_tracking_events.rb', line 3 def process PaperTrail.request(whodunnit: 'Maintenance::PurgeOldTrackingEvents') do # Purge old webhook events (legacy) webhook_events = WebhookEvent.where(WebhookEvent[:created_at].lteq(3.months.ago)) log_info "Deleting #{webhook_events.size} web hook events older than 3 months" webhook_events.delete_all # Purge old edi communication logs edi_logs = EdiCommunicationLog.where(EdiCommunicationLog[:created_at].lteq(6.months.ago)) log_info "Deleting #{edi_logs.size} edi communication logs" edi_logs.delete_all # Archive ECLs in `exception` state older than 1 week via the state machine. edi_exception_to_archive = EdiCommunicationLog.where(state: 'exception').where(EdiCommunicationLog[:created_at].lteq(1.week.ago)) log_info "Archiving #{edi_exception_to_archive.size} exception edi communication logs older than 1 week" edi_exception_to_archive.find_each(&:archive!) # Complete ECLs stuck in `processing` older than 1 day. These are orphaned records # left behind when Sidekiq workers are killed mid-flight (deploys, OOM kills). # The `complete` event (any → processed) handles this transition; the normal # 6-month delete above will clean them up eventually. edi_stuck_processing = EdiCommunicationLog.where(state: 'processing').where(EdiCommunicationLog[:updated_at].lteq(1.day.ago)) log_info "Completing #{edi_stuck_processing.size} edi communication logs stuck in processing for more than 1 day" edi_stuck_processing.find_each do |ecl| ecl.notes = [ecl.notes.presence, "Auto-completed by PurgeOldTrackingEvents — ECL was stuck in processing state for more than 1 day."].compact.join("\n\n") ecl.complete! end # Purge old messaging logs messaging_logs = MessagingLog.where(MessagingLog[:created_at].lteq(6.months.ago)) log_info "Deleting #{messaging_logs.size} messaging logs" messaging_logs.delete_all # Purge old webhook logs (AI transcription webhooks) purge_old_webhook_logs # Purge old AI assistant SQL audit logs purge_old_sql_audit_logs end end |