Class: CanvasSync::Jobs::ReportSyncTask::CheckerJob

Inherits:
ReportTaskJob show all
Defined in:
lib/canvas_sync/jobs/report_sync_task.rb

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

#performObject



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/canvas_sync/jobs/report_sync_task.rb', line 213

def perform()
  options = report_task.options
  report_status = CanvasSync.get_canvas_sync_client(batch_context).report_status(, *batch_context[:report_id].split("/"))

  case report_status["status"].downcase
  when "complete"
    ProcessJob.perform_later(report_status["attachment"]["url"])
  when "error", "deleted"
    max_tries = options[:report_max_tries] || batch_context[:report_max_tries] || report_task.max_tries || MAX_TRIES
    report_attempt = batch_context[:report_attempt]

    message = "Report failed to process; status was #{report_status} for #{batch_context[:report_id]}. This report has now failed #{batch_context[:report_attempt]} times."
    Rails.logger.error(message)

    if report_attempt >= max_tries
      Rails.logger.error("This report has failed #{report_attempt} times. Giving up.")
      raise FatalReportError, message
    else
      batch_context[:report_attempt] += 1

      # Remove the cache entry (if it wasn't already restarted by some other cacher) so that we actually start a new report
      if batch_context[:caching_key].present?
        CanvasSync.redis do |r|
          # This could _technically_ result in a race condition, but the chances are quite low and the
          #   consequences are just that an extra report is triggered (which Canvas may de-dup anyway)
          centry = r.hgetall(batch_context[:caching_key])
          if centry.present? && centry["report_id"] == batch_context[:report_id]
            r.del(batch_context[:caching_key])
            Rails.logger.info("Deleted cache entry for #{batch_context[:report_id]} at #{batch_context[:caching_key]} due to report failure")
          end
        end
      end

      trigger_canvas_report
      enqueue_checker!
    end
  else
    report_timeout = options[:report_timeout] || batch_context[:report_timeout] || REPORT_TIMEOUT
    if timeout_met?(batch_context[:sync_start_time], report_timeout)
      raise FatalReportError, "Report appears to be stuck #{batch_context[:report_id]}"
    end

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

    enqueue_checker!
  end
end