Skip to content

Make sync enrollments and sync assignments async with Good_job gem#379

Open
gobears01 wants to merge 10 commits intoberkeley-cdss:mainfrom
cs169:async-requests
Open

Make sync enrollments and sync assignments async with Good_job gem#379
gobears01 wants to merge 10 commits intoberkeley-cdss:mainfrom
cs169:async-requests

Conversation

@gobears01
Copy link
Copy Markdown
Contributor

@gobears01 gobears01 commented Apr 10, 2026

General Info

We need a framework to run async functions in the background (show status syncing). Async cast (sync enrollments/assignments should show status). We don’t want to wait 30/ 60s as per the current implementation

Changes

Previously, "Sync Enrollments" and "Sync Assignments" were synchronous: the HTTP request would block until all Canvas API calls completed, which could take 30–60 seconds and risk timing out. This PR makes both operations asynchronous using GoodJob, a PostgreSQL-backed Active Job backend.

Background job infrastructure:
Added GoodJob gem and ran its migration to create the good_jobs table in Postgres
Configured config.active_job.queue_adapter = :good_job in application.rb
Added a Procfile so Heroku runs a separate worker dyno alongside web
Added dotenv-rails gem to support loading .env locally
Async sync behavior:
Course#sync_assignments and Course#sync_users_from_canvas now call perform_later instead of running inline; the controller returns immediately after enqueuing
Added GET /courses/:id/sync_status endpoint that returns the last roster_synced_at / assignments_synced_at timestamps from CourseToLms

UI:
Both sync buttons now disable and show a "Syncing..." label + spinner immediately on click
The Stimulus controllers poll /sync_status every 1 second and reload the page with a banner notification once the job's timestamp changes, confirming completion
On timeout (60s) or error, an alert flash is shown and the button re-enabled

Testing

Added a full RSpec spec for SyncUsersFromCanvasJob covering: user upsert, enrollment creation, role-based removal, multi-role sync, return values/persistence, and non-array Canvas error responses
Rewrote the stale pending spec for SyncAllCourseAssignmentsJob#sync_assignment to test the instance method directly (create, update, unchanged)
Updated course_spec to verify sync_users_from_canvas enqueues the job rather than running inline
Added Cucumber scenarios for both sync buttons: visibility, disabled state, and spinner appearance after clicking
Set config.good_job.execution_mode = :external in test.rb to prevent GoodJob from executing jobs in-process during tests (which would cause the page to reload before Capybara could assert on the loading state)

Documentation

No documentation needed

Checklist

  • Name of branch corresponds to story

@gobears01 gobears01 changed the title Make sync enrollments and sync assignment AJAX with Good_job gem Make sync enrollments and sync assignments AJAX with Good_job gem Apr 10, 2026
@gobears01 gobears01 changed the title Make sync enrollments and sync assignments AJAX with Good_job gem Make sync enrollments and sync assignments async with Good_job gem Apr 10, 2026
@cycomachead cycomachead self-requested a review April 14, 2026 05:10
@cycomachead
Copy link
Copy Markdown
Contributor

@gobears01 Can you merge in the upstream changes?

I think for this PR we're pretty close, but there's some git conflicts.
We will want to enable the good_job web interface -- but we can do that in a follow up PR. (Should be quick)

@gobears01
Copy link
Copy Markdown
Contributor Author

@gobears01 Can you merge in the upstream changes?

I think for this PR we're pretty close, but there's some git conflicts. We will want to enable the good_job web interface -- but we can do that in a follow up PR. (Should be quick)

I have just resolved the conflicts and redeployed the latest version from this branch to heroku at:
https://sp26-02-flextensions-45eafe44bb33.herokuapp.com/

Please let me know if you see any issues. Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants