-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bulk student create (Backend) (#443)
closes #442 --------- Co-authored-by: create-issue-branch[bot] <53036503+create-issue-branch[bot]@users.noreply.github.com> Co-authored-by: Dan Halson <[email protected]>
- Loading branch information
1 parent
e9d5529
commit c3064ec
Showing
23 changed files
with
390 additions
and
137 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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,41 @@ | ||
# frozen_string_literal: true | ||
|
||
module Api | ||
class UserJobsController < ApiController | ||
before_action :authorize_user | ||
|
||
def index | ||
user_jobs = UserJob.where(user_id: current_user.id).includes(:good_job) | ||
jobs = user_jobs.map { |user_job| job_attributes(user_job.good_job) } | ||
if jobs.any? | ||
render json: { jobs: }, status: :ok | ||
else | ||
render json: { error: 'No jobs found' }, status: :not_found | ||
end | ||
end | ||
|
||
def show | ||
user_job = UserJob.find_by(job_id: params[:id], teacher_id: current_user.id) | ||
job = job_attributes(user_job.good_job) | ||
if job | ||
render json: { job: }, status: :ok | ||
else | ||
render json: { error: 'Job not found' }, status: :not_found | ||
end | ||
end | ||
|
||
private | ||
|
||
def job_attributes(job) | ||
{ | ||
id: job.id, | ||
concurrency_key: job.concurrency_key, | ||
status: job.status, | ||
scheduled_at: job.scheduled_at, | ||
performed_at: job.performed_at, | ||
finished_at: job.finished_at, | ||
error: job.error | ||
} | ||
end | ||
end | ||
end |
This file contains 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,59 @@ | ||
# frozen_string_literal: true | ||
|
||
class ConcurrencyExceededForSchool < StandardError; end | ||
|
||
class CreateStudentsJob < ApplicationJob | ||
include GoodJob::ActiveJobExtensions::Concurrency | ||
|
||
queue_as :default | ||
|
||
# Restrict to one job per school to avoid duplicates | ||
good_job_control_concurrency_with( | ||
key: -> { "create_students_job_#{arguments.first[:school_id]}" }, | ||
total_limit: 1 | ||
) | ||
|
||
def self.attempt_perform_later(school_id:, students:, token:, user_id:) | ||
concurrency_key = "create_students_job_#{school_id}" | ||
existing_jobs = GoodJob::Job.where(concurrency_key:, finished_at: nil) | ||
|
||
raise ConcurrencyExceededForSchool, 'Only one job per school can be enqueued at a time.' if existing_jobs.exists? | ||
|
||
ActiveRecord::Base.transaction do | ||
job = perform_later(school_id:, students:, token:) | ||
UserJob.create!(user_id:, good_job_id: job.job_id) unless job.nil? | ||
|
||
job | ||
end | ||
end | ||
|
||
def perform(school_id:, students:, token:) | ||
students = Array(students) | ||
|
||
responses = ProfileApiClient.create_school_students(token:, students:, school_id:) | ||
return if responses[:created].blank? | ||
|
||
responses[:created].each do |user_id| | ||
Role.student.create!(school_id:, user_id:) | ||
end | ||
end | ||
|
||
# Don't retry... | ||
rescue_from ConcurrencyExceededForSchool do |e| | ||
Rails.logger.error "Only one job per school can be enqueued at a time: #{school_id}" | ||
Sentry.capture_exception(e) | ||
raise e | ||
end | ||
|
||
# Don't retry... | ||
rescue_from ActiveRecord::RecordInvalid do |e| | ||
Rails.logger.error "Failed to create student role: #{e.record.errors.full_messages.join(', ')}" | ||
Sentry.capture_exception(e) | ||
raise e | ||
end | ||
|
||
retry_on StandardError, attempts: 3 do |_job, e| | ||
Sentry.capture_exception(e) | ||
raise e | ||
end | ||
end |
This file contains 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,7 @@ | ||
# frozen_string_literal: true | ||
|
||
class UserJob < ApplicationRecord | ||
belongs_to :good_job, class_name: 'GoodJob::Job' | ||
|
||
attr_accessor :user | ||
end |
This file contains 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 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,3 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'awesome_print' if Rails.env.development? |
This file contains 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
17 changes: 17 additions & 0 deletions
17
db/migrate/20241008171501_add_jobs_finished_at_to_good_job_batches.rb
This file contains 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,17 @@ | ||
# frozen_string_literal: true | ||
|
||
class AddJobsFinishedAtToGoodJobBatches < ActiveRecord::Migration[7.1] | ||
def change | ||
reversible do |dir| | ||
dir.up do | ||
# Ensure this incremental update migration is idempotent | ||
# with monolithic install migration. | ||
return if connection.column_exists?(:good_job_batches, :jobs_finished_at) | ||
end | ||
end | ||
|
||
change_table :good_job_batches do |t| | ||
t.datetime :jobs_finished_at | ||
end | ||
end | ||
end |
15 changes: 15 additions & 0 deletions
15
db/migrate/20241008171600_create_good_job_execution_duration.rb
This file contains 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,15 @@ | ||
# frozen_string_literal: true | ||
|
||
class CreateGoodJobExecutionDuration < ActiveRecord::Migration[7.1] | ||
def change | ||
reversible do |dir| | ||
dir.up do | ||
# Ensure this incremental update migration is idempotent | ||
# with monolithic install migration. | ||
return if connection.column_exists?(:good_job_executions, :duration) | ||
end | ||
end | ||
|
||
add_column :good_job_executions, :duration, :interval | ||
end | ||
end |
6 changes: 6 additions & 0 deletions
6
db/migrate/20241009082132_add_concurrency_key_to_good_job_executions.rb
This file contains 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,6 @@ | ||
class AddConcurrencyKeyToGoodJobExecutions < ActiveRecord::Migration[6.0] | ||
def change | ||
add_column :good_job_executions, :concurrency_key, :string | ||
add_index :good_job_executions, :concurrency_key | ||
end | ||
end |
This file contains 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,13 @@ | ||
class CreateUserJobs < ActiveRecord::Migration[7.1] | ||
def change | ||
create_table :user_jobs, id: :uuid do |t| | ||
t.uuid :user_id, null: false, type: :uuid | ||
t.uuid :good_job_id, null: false, type: :uuid | ||
|
||
t.timestamps | ||
end | ||
|
||
add_foreign_key :user_jobs, :good_jobs, column: :good_job_id | ||
add_index :user_jobs, [:user_id, :good_job_id], unique: true | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.