Sha256: 4732a63ddb3ad0241acff0fad004bc155f65acfe848a827b7439e0e41f23af2a

Contents?: true

Size: 1.65 KB

Versions: 8

Compression:

Stored size: 1.65 KB

Contents

module CanvasSync::Jobs
  class CanvasProcessWaiter < ActiveJob::Base
    # rubocop:disable Metrics/PerceivedComplexity
    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
    # rubocop:enable Metrics/PerceivedComplexity

    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

    # This is a separate job so that, if it fails and a retry is triggered, it doesn't query the API needlessly
    class InvokeCallbackWorker < ActiveJob::Base
      def perform(job)
        CanvasSync::MiscHelper.invoke_task(job)
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
canvas_sync-0.22.9 lib/canvas_sync/jobs/canvas_process_waiter.rb
canvas_sync-0.22.8 lib/canvas_sync/jobs/canvas_process_waiter.rb
canvas_sync-0.22.6 lib/canvas_sync/jobs/canvas_process_waiter.rb
canvas_sync-0.22.5 lib/canvas_sync/jobs/canvas_process_waiter.rb
canvas_sync-0.22.4 lib/canvas_sync/jobs/canvas_process_waiter.rb
canvas_sync-0.22.3 lib/canvas_sync/jobs/canvas_process_waiter.rb
canvas_sync-0.22.2 lib/canvas_sync/jobs/canvas_process_waiter.rb
canvas_sync-0.22.1 lib/canvas_sync/jobs/canvas_process_waiter.rb