异步任务的处理

dev_cs
cxt 5 years ago
parent f3ff3841de
commit a7ec58e1b2

@ -812,8 +812,8 @@ class CoursesController < ApplicationController
# 搜索添加学生
def add_students_by_search
student_ids = []
ActiveRecord::Base.transaction do
begin
user_ids = params[:user_ids]
course_group_id = params[:course_group_id].to_i
if course_group_id != 0
@ -821,7 +821,6 @@ class CoursesController < ApplicationController
course_group_id = course_group.id
end
student_ids = []
user_ids.each do |user_id|
existing_course_member = @course.course_members.find_by(user_id: user_id.to_i)
new_student = CourseMember.new(user_id: user_id.to_i, course_id: @course.id, course_group_id: course_group_id, role: 4)
@ -839,16 +838,11 @@ class CoursesController < ApplicationController
student_ids << user_id
end
end
end
CourseAddStudentCreateWorksJob.perform_later(@course.id, student_ids) if student_ids.present?
TeacherInviteJoinCourseNotifyJob.perform_later(current_user.id, @course.id, 10, student_ids) if student_ids.present?
normal_status(0, "添加成功")
rescue => e
uid_logger(e.message)
tip_exception("添加失败")
raise ActiveRecord::Rollback
end
end
end
# 获取历史课堂,即用户管理的所有课堂以及课堂下的分班(去除当前课堂)

@ -927,19 +927,11 @@ class HomeworkCommonsController < ApplicationController
unless params[:category_id].blank?
@category = @course.course_second_categories.find_by(id: params[:category_id], category_type: "shixun_homework")
end
ActiveRecord::Base.transaction do
begin
shixuns.each do |shixun|
homework = HomeworksService.new.create_homework shixun, @course, @category, current_user
@homework_ids << homework.id
CreateStudentWorkJob.perform_later(homework.id)
end
rescue Exception => e
uid_logger(e.message)
tip_exception("创建失败")
raise ActiveRecord::Rollback
end
end
end
# 选用实训课程
@ -1019,8 +1011,6 @@ class HomeworkCommonsController < ApplicationController
none_shixun_ids = ShixunSchool.where("school_id != #{current_user.school_id}").pluck(:shixun_id)
course_module = @course.course_modules.find_by(module_type: "shixun_homework")
ActiveRecord::Base.transaction do
begin
subjects.each do |subject|
subject.stages.each do |stage|
@ -1038,12 +1028,6 @@ class HomeworkCommonsController < ApplicationController
end
end
end
rescue Exception => e
uid_logger(e.message)
tip_exception("创建失败")
raise ActiveRecord::Rollback
end
end
end
def publish_groups
@ -1070,7 +1054,7 @@ class HomeworkCommonsController < ApplicationController
charge_group_ids = @course.charge_group_ids(current_user)
publish_groups = charge_group_ids & params[:group_ids] if params[:group_ids]
ActiveRecord::Base.transaction do
# ActiveRecord::Base.transaction do
begin
homeworks.each do |homework|
# 作业未发布时
@ -1141,7 +1125,7 @@ class HomeworkCommonsController < ApplicationController
tip_exception("发布失败")
raise ActiveRecord::Rollback
end
end
# end
end
def end_groups
@ -1170,9 +1154,9 @@ class HomeworkCommonsController < ApplicationController
charge_group_ids = @course.charge_group_ids(current_user)
end_groups = charge_group_ids & params[:group_ids] if params[:group_ids]
ActiveRecord::Base.transaction do
begin
homeworks.each do |homework|
ActiveRecord::Base.transaction do
homework_detail_manual = homework.homework_detail_manual
# 分组设置
@ -1233,11 +1217,12 @@ class HomeworkCommonsController < ApplicationController
=end
# 更新所有学生的效率分(重新取homework确保是更新后的)
HomeworkEndUpdateScoreJob.perform_later(homework.id) if !homework.allow_late && homework.end_time <= time
end
end
homework.save!
end
HomeworkEndUpdateScoreJob.perform_later(homework.id) if !homework.allow_late && homework.end_time <= time
end
normal_status(0, "更新成功")
rescue Exception => e
uid_logger(e.message)
@ -1245,7 +1230,6 @@ class HomeworkCommonsController < ApplicationController
raise ActiveRecord::Rollback
end
end
end
def set_public
tip_exception("仅公开课堂才能公开作业") if @course.is_public == 0

@ -203,9 +203,9 @@ class SubjectsController < ApplicationController
stages = @subject.stages.where(id: @subject.stage_shixuns.where(shixun_id: params[:shixun_ids]).pluck(:stage_id))
course_module = @course.course_modules.where(module_type: "shixun_homework").first
homework_ids = []
ActiveRecord::Base.transaction do
begin
# 将实训课程下的所有已发布实训按顺序发送到课堂,同时创建与章节同名的实训作业目录
stages.each do |stage|
category = CourseSecondCategory.where(name: stage.name, course_id: @course.id, category_type: "shixun_homework").first ||
@ -214,14 +214,12 @@ class SubjectsController < ApplicationController
stage.shixuns.where(id: params[:shixun_ids], status: 2).each do |shixun|
homework = HomeworksService.new.create_homework shixun, @course, category, current_user
CreateStudentWorkJob.perform_later(homework.id)
homework_ids << homework.id
end
end
rescue Exception => e
uid_logger(e.message)
tip_exception(e.message)
raise ActiveRecord::Rollback
end
homework_ids.each do |homework_id|
CreateStudentWorkJob.perform_later(homework_id)
end
end

@ -0,0 +1,18 @@
class MigrateShixunStudentWork < ActiveRecord::Migration[5.2]
def change
homework_commons = HomeworkCommon.where(homework_type: 4).where("created_at > '2019-07-01 00:00:00'")
attrs = %i[homework_common_id user_id created_at updated_at]
StudentWork.bulk_insert(*attrs) do |worker|
homework_commons.each do |homework|
course = homework.course
if course && homework.student_works.count != course.students.count
course.students.each do |student|
next if StudentWork.where(homework_common_id: homework.id, user_id: student.user_id).any?
same_attrs = {user_id: student.user_id}
worker.add same_attrs.merge(homework_common_id: homework.id)
end
end
end
end
end
end
Loading…
Cancel
Save