Merge pull request #4423 from okriuchykhin/ok_SCI_7180

Improve speed of group unassignment job as it is executed in foreground [SCI-7180]
This commit is contained in:
Alex Kriuchykhin 2022-09-09 15:58:44 +02:00 committed by GitHub
commit f62e4e8509
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 12 deletions

View file

@ -9,7 +9,7 @@ module UserAssignments
@assigned_by = assigned_by
ActiveRecord::Base.transaction do
team.users.where.not(id: assigned_by.id).find_each do |user|
team.users.where.not(id: assigned_by).find_each do |user|
user_assignment = UserAssignment.find_or_initialize_by(
user: user,
assignable: project

View file

@ -7,20 +7,30 @@ module UserAssignments
def perform(project)
ActiveRecord::Base.transaction do
user_assignments = project.user_assignments.where(assigned: :automatically)
users = User.find(user_assignments.pluck(:user_id))
users = User.where(id: user_assignments.select(:user_id))
remove_users_from_experiments_and_my_modules(project, users)
UserAssignment.where(user: users, assignable: project).destroy_all
remove_users_from_my_modules(project, users)
remove_users_from_experiments(project, users)
user_assignments.destroy_all
end
end
def remove_users_from_experiments_and_my_modules(project, users)
project.experiments.each do |experiment|
experiment.my_modules.each do |my_module|
UserAssignment.where(user: users, assignable: my_module).destroy_all
end
UserAssignment.where(user: users, assignable: experiment).destroy_all
end
def remove_users_from_experiments(project, users)
experiments = project.experiments.joins(:user_assignments).where(user_assignments: { user: users })
# rubocop:disable Rails/SkipsModelValidations
UserAssignment.where(assignable_type: 'Experiment', assignable_id: experiments, user: users).delete_all
experiments.update_all(updated_at: Time.current)
# rubocop:enable Rails/SkipsModelValidations
end
def remove_users_from_my_modules(project, users)
my_modules = MyModule.joins(:user_assignments, experiment: :project)
.where(user_assignments: { user: users })
.where(projects: project)
# rubocop:disable Rails/SkipsModelValidations
UserAssignment.where(assignable_type: 'MyModule', assignable_id: my_modules, user: users).delete_all
my_modules.update_all(updated_at: Time.current)
# rubocop:enable Rails/SkipsModelValidations
end
end
end

View file

@ -326,7 +326,7 @@ class Project < ApplicationRecord
UserAssignments::GroupAssignmentJob.perform_now(
team,
self,
created_by
last_modified_by || created_by
)
end