Class: CanvasSync::Jobs::ReportChecker

Inherits:
CanvasSync::Job show all
Defined in:
lib/canvas_sync/jobs/report_checker.rb

Overview

ActiveJob class used to check the status of a pending Canvas report. Re-enqueues itself if the report is still processing on Canvas. Enqueues the ReportProcessor when the report has completed.

Defined Under Namespace

Classes: FatalReportError

Constant Summary collapse

REPORT_TIMEOUT =
24.hours
COMPILATION_TIMEOUT =
1.hour
MAX_TRIES =
3

Instance Attribute Summary

Attributes inherited from CanvasSync::Job

#job_log

Instance Method Summary collapse

Methods inherited from CanvasSync::Job

#create_job_log, #report_checker_wait_time, #update_or_create_model

Instance Method Details

#perform(report_name, report_id, processor, options, checker_context = {}) ⇒ nil



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/canvas_sync/jobs/report_checker.rb', line 20

def perform(report_name, report_id, processor, options, checker_context = {}) # rubocop:disable Metrics/AbcSize
  max_tries = options[:report_max_tries] || batch_context[:report_max_tries] || MAX_TRIES
   = options[:account_id] || batch_context[:account_id] || "self"
  report_status = CanvasSync.get_canvas_sync_client(batch_context)
                            .report_status(, report_name, report_id)

  case report_status["status"].downcase
  when "complete"
    CanvasSync::Jobs::ReportProcessorJob.perform_later(
      report_name,
      report_status["attachment"]["url"],
      processor,
      options,
      report_id,
    )
  when "error", "deleted"
    checker_context[:failed_attempts] ||= 0
    checker_context[:failed_attempts] += 1
    failed_attempts = checker_context[:failed_attempts]
    message = "Report failed to process; status was #{report_status} for report_name: #{report_name}, report_id: #{report_id}, #{current_organization.name}.  This report has now failed #{checker_context[:failed_attempts]} time." # rubocop:disable Metrics/LineLength
    Rails.logger.error(message)
    if failed_attempts >= max_tries
      Rails.logger.error("This report has failed #{failed_attempts} times.  Giving up.")
      raise FatalReportError, message
    else
      restart_report(options, report_name, processor, checker_context)
    end
  else
    report_timeout = parse_timeout(options[:report_timeout] || batch_context[:report_timeout] || REPORT_TIMEOUT)
    if timeout_met?(options[:sync_start_time], report_timeout)
      raise FatalReportError, "Report appears to be stuck #{report_name}##{report_id}"
    end

    if report_status["status"].downcase == 'compiling'
      checker_context['compiling_since'] ||= DateTime.now.iso8601
      compilation_timeout = parse_timeout(options[:report_compilation_timeout] || batch_context[:report_compilation_timeout] || COMPILATION_TIMEOUT)
      if timeout_met?(checker_context['compiling_since'], compilation_timeout)
        raise FatalReportError, "Report appears to be stuck #{report_name}##{report_id}"
      end
    end

    CanvasSync::Jobs::ReportChecker
      .set(wait: report_checker_wait_time)
      .perform_later(
        report_name,
        report_id,
        processor,
        options,
        checker_context
      )
  end
end