From 595e4bcf6441ffcb004ba593052acc346c89511b Mon Sep 17 00:00:00 2001 From: p31729568 Date: Thu, 27 Jun 2019 09:02:20 +0800 Subject: [PATCH] modify shixun exec method --- app/controllers/shixuns_controller.rb | 20 +++++++++++--------- app/jobs/update_myshixun_work_status_job.rb | 10 ++++++++++ 2 files changed, 21 insertions(+), 9 deletions(-) create mode 100644 app/jobs/update_myshixun_work_status_job.rb diff --git a/app/controllers/shixuns_controller.rb b/app/controllers/shixuns_controller.rb index 360732120..3b14eea84 100644 --- a/app/controllers/shixuns_controller.rb +++ b/app/controllers/shixuns_controller.rb @@ -539,16 +539,21 @@ class ShixunsController < ApplicationController # 其它创建关卡等操作 challenges = @shixun.challenges # 之所以增加user_id是为了方便统计查询性能 - challenges.each_with_index do |challenge, index| - status = (index == 0 ? 0 : 3) - game_identifier = generate_identifier(Game, 12) - Game.create!(:challenge_id => challenge.id, :myshixun_id => myshixun.id, :status => status, :user_id => myshixun.user_id, - :open_time => Time.now, :identifier => game_identifier, :modify_time => challenge.modify_time) + game_attrs = %i[challenge_id myshixun_id status user_id open_time identifier modify_time created_at updated_at] + Game.bulk_insert(*game_attrs) do |worker| + base_attr = { myshixun_id: myshixun.id, user_id: myshixun.user_id } + challenges.each_with_index do |challenge, index| + status = (index == 0 ? 0 : 3) + game_identifier = generate_identifier(Game, 12) + worker.add(base_attr.merge(challenge_id: challenge.id, status: status, open_time: Time.now, + identifier: game_identifier, modify_time: challenge.modify_time)) + end end # REDO:开启实训时更新关联作品的状态 # TODO:这个可以异步。或者放到课程里面取,不要在开启实训这边做 - HomeworksService.new.update_myshixun_work_status myshixun + # HomeworksService.new.update_myshixun_work_status myshixun + UpdateMyshixunWorkStatusJob.perform_later(myshixun.id) @current_task = myshixun.current_task uid_logger("## shixun exec: myshixun id is #{myshixun.id}") @@ -559,9 +564,6 @@ class ShixunsController < ApplicationController end end end - - - end # gameID 及实训ID # status: 0 , 1 申请过, 2,实训关卡路径未填, 3 实训标签未填, 4 实训未创建关卡 diff --git a/app/jobs/update_myshixun_work_status_job.rb b/app/jobs/update_myshixun_work_status_job.rb new file mode 100644 index 000000000..27a53408f --- /dev/null +++ b/app/jobs/update_myshixun_work_status_job.rb @@ -0,0 +1,10 @@ +class UpdateMyshixunWorkStatusJob < ApplicationJob + queue_as :default + + def perform(myshixun_id) + myshixun = Myshixun.find_by(id: myshixun_id) + return if myshixun.blank? + + HomeworksService.new.update_myshixun_work_status myshixun + end +end