-
Notifications
You must be signed in to change notification settings - Fork 78
feat(submission): offload background operations from finalise transaction #8579
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
app/jobs/course/assessment/submission/publish_task_completion_job.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| # frozen_string_literal: true | ||
| # Pushes a submission's task status to Cikgo once the transaction that changed it has committed. | ||
| # | ||
| # The push is a synchronous HTTP call with a five second timeout, so it is kept off the request: a | ||
| # slow or unavailable Cikgo must not delay a response, hold locks on a submission that is already | ||
| # saved, or fail a request for work that has been committed. | ||
| # | ||
| # Unlike the inline +publish_task_completion+, which logs and swallows in production, a failure here | ||
| # is allowed to propagate. That is the point of running it as a job: Sidekiq retries it and Rollbar | ||
| # records it, rather than the error disappearing into the log. | ||
| class Course::Assessment::Submission::PublishTaskCompletionJob < ApplicationJob | ||
| rescue_from(ActiveJob::DeserializationError) do |_| | ||
| # The submission was deleted before the job ran; there is no status left to publish. | ||
| end | ||
|
|
||
| def perform(submission) | ||
| instance = Course.unscoped { submission.assessment.course.instance } | ||
|
|
||
| ActsAsTenant.with_tenant(instance) do | ||
| # Re-read rather than trusting the state at enqueue time. A later transition may already have | ||
| # been pushed, and the status Cikgo should end up with is the current one either way — which | ||
| # also makes two pushes racing each other harmless. | ||
| next unless submission.should_publish_task_completion? | ||
|
|
||
| submission.publish_task_completion! | ||
| end | ||
| end | ||
| end |
28 changes: 28 additions & 0 deletions
28
app/jobs/course/lesson_plan/personalized_timeline_update_job.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| # frozen_string_literal: true | ||
| # Recomputes one course user's personalised timeline. | ||
| # | ||
| # Enqueued after a submission is finalised, rather than run inside the finalise transaction: the | ||
| # recomputation walks every lesson plan item in the course and writes a personal time per shiftable | ||
| # item, which is by far the largest unit of work in that transaction and the only part that contends | ||
| # with +CoursewidePersonalizedTimelineUpdateJob+ for the same rows. | ||
| # | ||
| # The timeline is derived state. If this job never succeeds the student's timeline is one submission | ||
| # stale but still valid, and the next recomputation for them — their next submission, an instructor | ||
| # pressing "Recompute all times", or a coursewide run — brings it forward. | ||
| class Course::LessonPlan::PersonalizedTimelineUpdateJob < ApplicationJob | ||
| include Course::LessonPlan::PersonalizationConcern | ||
|
|
||
| queue_as :lowest | ||
|
|
||
| rescue_from(ActiveJob::DeserializationError) do |_| | ||
| # The course user was removed from the course before the job ran; there is no timeline to update. | ||
| end | ||
|
|
||
| def perform(course_user) | ||
| instance = Course.unscoped { course_user.course.instance } | ||
|
|
||
| ActsAsTenant.with_tenant(instance) do | ||
| update_personalized_timeline_for_user(course_user) | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
spec/jobs/course/assessment/submission/publish_task_completion_job_spec.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| # frozen_string_literal: true | ||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe Course::Assessment::Submission::PublishTaskCompletionJob do | ||
| let(:instance) { Instance.default } | ||
| with_tenant(:instance) do | ||
| let(:course) { create(:course) } | ||
| let(:assessment) { create(:assessment, course: course) } | ||
| let(:course_student) { create(:course_student, course: course) } | ||
| let(:submission) do | ||
| create(:submission, :attempting, assessment: assessment, | ||
| creator: course_student.user, course_user: course_student) | ||
| end | ||
| subject { Course::Assessment::Submission::PublishTaskCompletionJob } | ||
|
|
||
| it 'can be queued' do | ||
| expect { subject.perform_later(submission) }.to have_enqueued_job(subject).exactly(:once) | ||
| end | ||
|
|
||
| it 'pushes the current status' do | ||
| allow(submission).to receive(:should_publish_task_completion?).and_return(true) | ||
| expect(submission).to receive(:publish_task_completion!) | ||
|
|
||
| subject.perform_now(submission) | ||
| end | ||
|
|
||
| # Re-read at run time rather than trusted from enqueue time, so a superseded push is dropped. | ||
| it 'does not push when the submission no longer qualifies' do | ||
| allow(submission).to receive(:should_publish_task_completion?).and_return(false) | ||
| expect(submission).not_to receive(:publish_task_completion!) | ||
|
|
||
| subject.perform_now(submission) | ||
| end | ||
|
|
||
| it 'lets a failed push propagate, so Sidekiq retries and Rollbar records it' do | ||
| allow(submission).to receive(:should_publish_task_completion?).and_return(true) | ||
| allow(submission).to receive(:publish_task_completion!).and_raise(StandardError, 'cikgo down') | ||
|
|
||
| expect { subject.perform_now(submission) }.to raise_error(StandardError, 'cikgo down') | ||
| end | ||
| end | ||
| end |
72 changes: 72 additions & 0 deletions
72
spec/jobs/course/lesson_plan/personalized_timeline_update_job_spec.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| # frozen_string_literal: true | ||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe Course::LessonPlan::PersonalizedTimelineUpdateJob do | ||
| let(:instance) { Instance.default } | ||
| with_tenant(:instance) do | ||
| let(:course) { create(:course) } | ||
| let!(:submitted_assessment) do | ||
| create(:assessment, course: course, start_at: 2.days.ago, end_at: 3.days.from_now, published: true) | ||
| end | ||
| let!(:upcoming_assessment) do | ||
| create(:assessment, course: course, start_at: 5.days.from_now, end_at: 10.days.from_now, published: true) | ||
| end | ||
| let(:timeline_algorithm) { 'fomo' } | ||
| let(:course_user) { create(:course_user, course: course, timeline_algorithm: timeline_algorithm) } | ||
| let(:submission) do | ||
| create(:course_assessment_submission, assessment: submitted_assessment, creator: course_user.user) | ||
| end | ||
| subject { Course::LessonPlan::PersonalizedTimelineUpdateJob } | ||
|
|
||
| it 'can be queued' do | ||
| expect { subject.perform_later(course_user) }.to have_enqueued_job(subject).exactly(:once) | ||
| end | ||
|
|
||
| it 'is enqueued when a submission is finalised' do | ||
| expect { submission.finalise! }.to have_enqueued_job(subject).exactly(:once) | ||
| end | ||
|
|
||
| context 'when the course user is on a personalized timeline' do | ||
| it 'shifts the timeline for the course user', :sidekiq_same_thread do | ||
| submission.finalise! | ||
| submission.save! | ||
| expect(course_user.personal_times).to be_empty | ||
|
|
||
| perform_sidekiq_jobs { subject.perform_later(course_user) } | ||
|
|
||
| # The submitted item is never shifted, so only the upcoming one gets a personal time. | ||
| expect(course_user.personal_times.count).to eq(1) | ||
| expect(course_user.personal_times.first.lesson_plan_item_id). | ||
| to eq(upcoming_assessment.lesson_plan_item.id) | ||
| end | ||
|
|
||
| it 'is safe to run more than once', :sidekiq_same_thread do | ||
| submission.finalise! | ||
| submission.save! | ||
|
|
||
| perform_sidekiq_jobs { subject.perform_later(course_user) } | ||
| first_run = course_user.personal_times.pluck(:lesson_plan_item_id, :start_at, :end_at) | ||
|
|
||
| perform_sidekiq_jobs { subject.perform_later(course_user) } | ||
|
|
||
| # Idempotent, and — importantly for the offload — it never accumulates rows: the timeline is | ||
| # recomputed from current state rather than appended to. | ||
| expect(course_user.personal_times.reload.pluck(:lesson_plan_item_id, :start_at, :end_at)). | ||
| to match_array(first_run) | ||
| end | ||
| end | ||
|
|
||
| context 'when the course user is on the fixed timeline' do | ||
| let(:timeline_algorithm) { 'fixed' } | ||
|
|
||
| it 'creates no personal times', :sidekiq_same_thread do | ||
| submission.finalise! | ||
| submission.save! | ||
|
|
||
| perform_sidekiq_jobs { subject.perform_later(course_user) } | ||
|
|
||
| expect(course_user.personal_times).to be_empty | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.