Module: CanvasSync

Defined in:
lib/canvas_sync.rb,
lib/canvas_sync/job.rb,
lib/canvas_sync/engine.rb,
lib/canvas_sync/version.rb,
app/models/canvas_sync/job_log.rb,
lib/canvas_sync/jobs/report_checker.rb,
lib/canvas_sync/jobs/report_starter.rb,
lib/canvas_sync/jobs/sync_terms_job.rb,
lib/canvas_sync/jobs/sync_users_job.rb,
lib/canvas_sync/importers/bulk_importer.rb,
lib/canvas_sync/importers/legacy_importer.rb,
lib/canvas_sync/jobs/report_processor_job.rb,
lib/canvas_sync/generators/install_generator.rb,
lib/canvas_sync/jobs/sync_provisioning_report_job.rb,
lib/canvas_sync/processors/provisioning_report_processor.rb

Defined Under Namespace

Modules: Importers, Jobs, Processors Classes: Engine, InstallGenerator, Job, JobLog

Constant Summary collapse

SUPPORTED_MODELS =
%w(users courses terms enrollments sections)
VERSION =
"0.2.3"

Class Method Summary collapse

Class Method Details

.default_provisioning_report_chain(models, term_scope = nil, legacy_support = false, account_id = nil) ⇒ Hash

Syncs terms, users if necessary, then the rest of the specified models.

Parameters:

  • models (Array<String>)
  • term_scope (String) (defaults to: nil)
  • legacy_support (Boolean, false) (defaults to: false) —

    This enables legacy_support, where rows are not bulk inserted. For this to work your models must have a create_or_udpate_from_csv class method that takes a row and inserts it into the database.

  • account_id (Integer, nil) (defaults to: nil) —

    This optional parameter can be used if your Term creation and canvas_sync_client methods require an account ID.

Returns:

  • (Hash)


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/canvas_sync.rb', line 76

def self.default_provisioning_report_chain(models, term_scope=nil, legacy_support=false, =nil)
  term_scope = term_scope.to_s if term_scope


  # Always sync Terms first
  jobs = [{ job: CanvasSync::Jobs::SyncTermsJob.to_s, options: {} }]

  if models.include?('users') && term_scope.present?
    # Sync all users first when scoping by term, because users cannot be scoped to term
    jobs.push({ job: CanvasSync::Jobs::SyncUsersJob.to_s, options: {} })
    models = models - ['users']
  end

  jobs.push({ job: CanvasSync::Jobs::SyncProvisioningReportJob.to_s, options: { term_scope: term_scope, models: models } })

  global_options = { legacy_support: legacy_support }
  global_options[:account_id] =  if .present?

  { jobs: jobs, global_options: global_options }
end

.get_canvas_sync_client(options) ⇒ Object

Calls the canvas_sync_client in your app. If you have specified an account ID when starting the job it will pass the account ID to your canvas_sync_client method.

Parameters:

  • options (Hash)


101
102
103
104
105
106
107
# File 'lib/canvas_sync.rb', line 101

def self.get_canvas_sync_client(options)
  if options[:account_id]
    canvas_sync_client(options[:account_id])
  else
    canvas_sync_client
  end
end

.invoke_next(job_chain) ⇒ Object

Invokes the next job in a chain of jobs.

This should typically be called automatically by the gem where necessary.

Parameters:

  • job_chain (Hash) —

    A chain of jobs to execute



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/canvas_sync.rb', line 53

def self.invoke_next(job_chain)
  return if job_chain[:jobs].empty?

  # Make sure all job classes are serialized as strings
  job_chain[:jobs].each { |job| job[:job] = job[:job].to_s }

  duped_job_chain = Marshal.load(Marshal.dump(job_chain))
  jobs = duped_job_chain[:jobs]
  next_job = jobs.shift
  next_job_class = next_job[:job].constantize
  next_job_class.perform_later(duped_job_chain, next_job[:options])
end

.process_jobs(job_chain) ⇒ Object

Runs a chain of ordered jobs

See the README for usage and examples

Parameters:

  • job_chain (Hash)


44
45
46
# File 'lib/canvas_sync.rb', line 44

def self.process_jobs(job_chain)
  invoke_next(job_chain)
end

.provisioning_sync(models, term_scope: nil, legacy_support: false, account_id: nil) ⇒ Object

Runs a standard provisioning sync job with no extra report types. Terms will be synced first using the API. If you are syncing users and have also specified a Term scope, Users will by synced first, before every other model (as Users are never scoped to Term).

Parameters:

  • models (Array<String>) —

    A list of models to sync. e.g., ['users', 'courses']. must be one of SUPPORTED_MODELS

  • term_scope (Symbol, nil) (defaults to: nil) —

    An optional symbol representing a scope that exists on the Term model. The provisioning report will be run for each of the terms contained in that scope.

  • legacy_support (Boolean, false) (defaults to: false) —

    This enables legacy_support, where rows are not bulk inserted. For this to work your models must have a create_or_udpate_from_csv class method that takes a row and inserts it into the database.

  • account_id (Integer, nil) (defaults to: nil) —

    This optional parameter can be used if your Term creation and canvas_sync_client methods require an account ID.



34
35
36
37
# File 'lib/canvas_sync.rb', line 34

def self.provisioning_sync(models, term_scope: nil, legacy_support: false, account_id: nil)
  validate_models!(models)
  invoke_next(default_provisioning_report_chain(models, term_scope, legacy_support, ))
end

.validate_models!(models) ⇒ Object



111
112
113
114
115
116
# File 'lib/canvas_sync.rb', line 111

def self.validate_models!(models)
  invalid = models - SUPPORTED_MODELS
  if invalid.length > 0
    raise "Invalid model(s) specified: #{invalid.join(', ')}. Only #{SUPPORTED_MODELS.join(', ')} are supported."
  end
end