CanvasSync

This gem is intended to facilitate fast and easy syncing of Canvas data.

Installation

Add this line to your application's Gemfile:

gem 'canvas_sync'

Mount the engine in your config/routes.rb (required for Live Events):

mount CanvasSync::Engine, at: '/canvas_sync'

Models and migrations can be installed using the following generator:

bin/rails generate canvas_sync:install --models users,terms,courses

Use the --models option to specify what models you would like installed. This will add both the model files and their corresponding migrations. If you'd like to install all the models that CanvasSync supports then specify --models all.

Then run the migrations:

bundle exec rake db:migrate

For a list of currently supported models, see CanvasSync::SUPPORTED_MODELS.

Required Canvas reports

Most models are synced from Canvas custom reports, which must be enabled on your Canvas instance. The "Proserv Provisioning Report" (proservices_provisioning_csv) is always required. Other models need their own reports:

Model(s) Report
assignments Assignments Report (proserv_assignment_export_csv)
assignment_groups Assignment Group Export (proserv_assignment_group_export_csv)
assignment_overrides Assignment Overrides Export (proserv_assignment_overrides_csv)
submissions Student Submissions (proserv_student_submissions_csv)
scores Professional Services Scores Data Report (proserv_scores_data_csv)
course_progresses Student Course Completion Dates (proserv_course_completion_csv)
context_modules Professional Services Context Modules Report (proserv_context_modules_csv)
context_module_items Professional Services Context Module Items Report (proserv_context_module_items_csv)
content_migrations Professional Services Content Migrations Report (proserv_content_migrations_csv)
rubrics Rubrics Export Report (rubrics_csv)
rubric_associations Rubrics Associations Export Report (rubric_associations_csv)
rubric_assessments Rubrics Assessments Export Report (rubric_assessments_csv)

This table can drift. The source of truth is CanvasSync::ReportsMap, and you can print the reports your app needs (based on the models and jobs it uses) with:

bundle exec rake canvas_sync:reports

When PandaPal is installed, this list also prints at the end of the interactive org:install.

Prerequisites

Ruby and Rails

CanvasSync is tested against Rails 5.2 through 8.1. The current target is Ruby 3.4 / Rails 8.1.

Postgres

The bulk inserting is made possible by using a Postgres upsert, so you need Postgres 9.5 or above. CI runs against Postgres 18.

Redis

Job orchestration (via Joblin), updated_after tracking, and report caching use Redis. Both CanvasSync and Joblin read BEARCAT_REDIS_URL, falling back to REDIS_URL.

Sidekiq

Make sure you've setup sidekiq to work properly with ActiveJob as outlined here.

Apartment

If using apartment and sidekiq make sure you include the apartment-sidekiq gem so that the jobs are run in the correct tenant.

Live Events

Live Events need a few extra gems. See Live Events.

Quick start

Your tool needs an ActiveJob queue adapter, such as Sidekiq. Once you've generated your models and run the migrations, start a provisioning sync:

CanvasSync.provisioning_sync(['users', 'courses'], term_scope: :active)

This kicks off a chain of jobs that pulls the Canvas reports for those models and bulk-upserts them with activerecord-import. See Provisioning sync for the options.

Features

Start with Architecture for how data moves from Canvas into your database.

Feature What it does
Provisioning sync Syncs the models you pick, with term scoping, updated_after delta syncs, scheduled full syncs, custom jobs, chain options, and failure handling.
Custom report syncs Runs any Canvas report through ReportSyncTask (start, poll, download, retry, and import), with optional report caching.
Importing and mapping overrides Controls bulk imports: batch size, import callbacks, column mappings, alter_rows, and legacy row-by-row syncing.
Live Events Receives Canvas Live Events, checks where they came from, and updates models or calls your subscribers.
API sync Syncs a single record from the Canvas API with api_syncable.
AbilityHelper and JIT sync Maps LTI launches to synced records, roles, and permissions, and fetches missing records just in time.
UserViaPseudonym Links your models to Canvas users through pseudonyms, so links survive user merges.
Job orchestration (Joblin) Provides batches, pools, job uniqueness, background tasks, and a Sidekiq Web UI.
Job logs Records each job run in canvas_sync_job_logs and cleans up old rows.
Upgrading Covers re-running generators, schema changes, and fitting CanvasSync into an existing app.
Development Explains how to run the test suite, release the gem, and build the YARD docs.

Configuration

You can configure CanvasSync settings by doing the following:

CanvasSync.configure do |config|
  config.classes_to_only_log_errors_on << "ClassToOnlyLogErrorsOn"
end

Available config options (if you add more, please update this!):

  • config.classes_to_only_log_errors_on - use this if you are utilizing the CanvasSync::JobLog table, but want certain classes to only persist in the job_logs table if an error is encountered. This is useful if you've got a very frequently used job that's filling up your database, and only really care about tracking failures.
  • config.redis_key_prefix (default "cs") - the prefix for the per-Term "last synced" Redis keys used by updated_after. With Apartment, the current tenant is prepended automatically.

Custom Bearcat instance

You can define a global canvas_sync_client method to return a Bearcat Client instance for CanvasSync to use:

# config/initializers/canvas_sync.rb
def canvas_sync_client
  Bearcat::Client.new(token: current_organization.settings[:api_token], prefix: current_organization.settings[:base_url])
end

This used to be required, but when both CanvasSync and PandaPal are up to date, this is defined automagically.

Changelog

See CHANGELOG.md for major and breaking changes.