Class: CanvasSync::Jobs::CanvasProcessWaiter

Inherits:
ActiveJob::Base
  • Object
show all
Defined in:
lib/canvas_sync/jobs/canvas_process_waiter.rb

Defined Under Namespace

Classes: InvokeCallbackWorker

Instance Method Summary collapse

Instance Method Details

#build_next_job(job, kwargs, response) ⇒ Object

rubocop:enable Metrics/PerceivedComplexity



25
26
27
28
29
30
31
32
# File 'lib/canvas_sync/jobs/canvas_process_waiter.rb', line 25

def build_next_job(job, kwargs, response)
  job = job.symbolize_keys
  if kwargs[:progress_as].present?
    job[:kwargs] ||= {}
    job[:kwargs][kwargs[:progress_as].to_sym] = response
  end
  job
end

#perform(progress_url, next_job, kwargs = {}) ⇒ Object

rubocop:disable Metrics/PerceivedComplexity



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/canvas_sync/jobs/canvas_process_waiter.rb', line 4

def perform(progress_url, next_job, kwargs = {})
  kwargs = kwargs.symbolize_keys

  response = canvas_sync_client.get(progress_url)
  status = kwargs[:status_key].present? ? response[kwargs[:status_key]] : response['workflow_state'] || response['status']

  if %w[completed complete imported imported_with_messages].include? status
    InvokeCallbackWorker.perform_later(build_next_job(next_job, kwargs, response)) if next_job
  elsif %w[failed error failed_with_messages imports_failed exports_failed].include? status
    if kwargs[:on_failure].is_a?(Hash)
      InvokeCallbackWorker.perform_later(build_next_job(kwargs[:on_failure], kwargs, response))
    else
      Rails.logger.error("Progress #{progress_url} failed")
    end
  else # if status == 'queued' || status == 'running'
    interval = kwargs[:interval] || (Rails.env.development? ? 3 : 60)
    CanvasProcessWaiter.set(wait: interval).perform_later(progress_url, next_job, kwargs)
  end
end