module CanvasSync module Jobs # Starts a Canvas report and enqueues a ReportChecker class ReportStarter < CanvasSync::Job # @param report_name [Hash] e.g., 'provisioning_csv' # @param report_params [Hash] The Canvas report parameters # @param processor [String] a stringified report processor class name # @param options [Hash] hash of options that will be passed to the job processor # @param allow_redownloads [Boolean] whether you want the job_chain to cache this report, # so that any later jobs in the chain will use the same generated report # @return [nil] def perform(report_name, report_params, processor, options, allow_redownloads: false) account_id = options[:account_id] || batch_context[:account_id] || "self" options[:sync_start_time] = DateTime.now.utc.iso8601 options[:report_params] = report_params report_id = start_report(account_id, report_name, report_params) # TODO: Restore report caching support (does nayone actually use it?) # report_id = if allow_redownloads # get_cached_report(account_id, report_name, report_params) # else # start_report(account_id, report_name, report_params) # end batch = JobBatches::Batch.new batch.description = "CanvasSync #{report_name} Fiber" batch.jobs do CanvasSync::Jobs::ReportChecker.set(wait: report_checker_wait_time).perform_later( report_name, report_id, processor, options ) end end protected # Ruby 3 changed how kwargs are handled. _kw_placeholder allows for backwards compatibility # In Ruby 2, merge_report_params(options, params) would parse as merge_report_params(options, params={}, **params) (wtf?), so # merge_report_params(options, params, {}) is used. That doesn't work in Ruby 3. # In order to maintain compatibility with 2 and with any apps, this oddness is needed def merge_report_params(options, params={}, _kw_placeholder=nil, term_scope: true) term_scope = options[:canvas_term_id] || batch_context[:canvas_term_id] if term_scope == true if term_scope.present? params[:enrollment_term_id] = term_scope end if (updated_after = batch_context[:updated_after]).present? params[:updated_after] = updated_after end params.merge!(options[:report_params]) if options[:report_params].present? params.merge!(options[:report_parameters]) if options[:report_parameters].present? { parameters: params } end private def get_cached_report(job_chain, account_id, report_name, report_params) # TODO: job_chain[:global_options] is no longer available and batch_context won't work for this if job_chain[:global_options][report_name].present? job_chain[:global_options][report_name] else report_id = start_report(job_chain, account_id, report_name, report_params) job_chain[:global_options][report_name] = report_id report_id end end def start_report(account_id, report_name, report_params) report = CanvasSync.get_canvas_sync_client(batch_context) .start_report(account_id, report_name, report_params) report["id"] end end end end