Files
aperonight/app/jobs/cleanup_expired_drafts_job.rb
kbe cb0de11de1 refactor: Improve code quality and add comprehensive documentation
- Remove unused create_stripe_session method from TicketsController
- Replace hardcoded API key with environment variable for security
- Fix typo in ApplicationHelper comment
- Improve User model validation constraints for better UX
- Add comprehensive YARD-style documentation across models, controllers, services, and helpers
- Enhance error handling in cleanup jobs with proper exception handling
- Suppress Prawn font warnings in PDF generator
- Update refactoring summary with complete change documentation

All tests pass (200 tests, 454 assertions, 0 failures)
RuboCop style issues resolved automatically

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-05 17:30:13 +02:00

34 lines
1.2 KiB
Ruby

# 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