|
| 1 | +class CleanupSubmissionEntriesJob < ApplicationJob |
| 2 | + ENTRIES_BATCH_SIZE = 1000 |
| 3 | + |
| 4 | + # rubocop:disable Metrics/CyclomaticComplexity |
| 5 | + # rubocop:disable Metrics/PerceivedComplexity |
| 6 | + def perform(dry_run: false, max_run_time: 5.hours, task_batch_sixe: 100) |
| 7 | + start_time = Time.current |
| 8 | + total_deleted_entries = 0 |
| 9 | + |
| 10 | + tasks_with_unprocessed_submissions = Task.joins(:submissions) |
| 11 | + .where(submissions: { aasm_state: 'validation_failed', |
| 12 | +cleanup_processed: false }) |
| 13 | + .distinct |
| 14 | + |
| 15 | + tasks_with_unprocessed_submissions.find_in_batches(batch_size: task_batch_sixe) do |tasks_batch| |
| 16 | + tasks_batch.each do |task| |
| 17 | + active_id = task.active_submission&.id |
| 18 | + |
| 19 | + failed_submissions = task.submissions |
| 20 | + .where(aasm_state: 'validation_failed', cleanup_processed: false) |
| 21 | + .where.not(id: active_id) |
| 22 | + |
| 23 | + next if failed_submissions.empty? |
| 24 | + |
| 25 | + deleted_for_task = 0 |
| 26 | + |
| 27 | + failed_submissions.find_each do |submission| |
| 28 | + begin |
| 29 | + submission.entries.in_batches(of: ENTRIES_BATCH_SIZE) do |entries_batch| |
| 30 | + if dry_run |
| 31 | + Rollbar.info("Dry run: would delete #{entries_batch.count} entries for Submission ID #{submission.id}.") |
| 32 | + else |
| 33 | + deleted_count = entries_batch.delete_all |
| 34 | + deleted_for_task += deleted_count |
| 35 | + total_deleted_entries += deleted_count |
| 36 | + # rubocop:disable Layout/LineLength |
| 37 | + Rollbar.info("Task ID #{task.id}: Processed #{failed_submissions.count} failed submissions, deleted #{deleted_for_task} entries.") |
| 38 | + # rubocop:enable Layout/LineLength |
| 39 | + end |
| 40 | + end |
| 41 | + |
| 42 | + submission.update(cleanup_processed: true) unless dry_run |
| 43 | + rescue StandardError => e |
| 44 | + Rollbar.error(e, "Error processing Submission ID #{submission.id} for Task ID #{task.id}: #{e.message}") |
| 45 | + end |
| 46 | + |
| 47 | + break if Time.current - start_time >= max_run_time |
| 48 | + end |
| 49 | + end |
| 50 | + |
| 51 | + break if Time.current - start_time >= max_run_time |
| 52 | + |
| 53 | + sleep 1 # To avoid overwhelming the database |
| 54 | + end |
| 55 | + end |
| 56 | + # rubocop:enable Metrics/CyclomaticComplexity |
| 57 | + # rubocop:enable Metrics/PerceivedComplexity |
| 58 | +end |
0 commit comments