# Background job to clean up expired draft tickets # # This job runs periodically to find and expire draft tickets that have # passed their expiry time (typically 30 minutes after creation). # Should be scheduled via cron or similar scheduling system. class CleanupExpiredDraftsJob < ApplicationJob queue_as :default # Find and expire all draft tickets that have passed their expiry time # # Uses find_each to process tickets in batches to avoid memory issues # with large datasets. Continues processing even if individual tickets fail. def perform expired_count = 0 # Process expired draft tickets in batches Ticket.expired_drafts.find_each do |ticket| begin Rails.logger.info "Expiring draft ticket #{ticket.id} for user #{ticket.user.id}" ticket.expire_if_overdue! expired_count += 1 rescue => e # Log error but continue processing other tickets Rails.logger.error "Failed to expire ticket #{ticket.id}: #{e.message}" next end end # Log summary if any tickets were processed Rails.logger.info "Expired #{expired_count} draft tickets" if expired_count > 0 Rails.logger.info "No expired draft tickets found" if expired_count == 0 end end