diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 7f1383bc..c1c25151 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -132,6 +132,15 @@ class ApplicationController < ActionController::Base end end + DCODES = %W(2 3 4 5 6 7 8 9 a b c f e f g h i j k l m n o p q r s t u v w x y z) + # 随机生成字符 + def generates_identifier(container, num) + code = DCODES.sample(num).join + while container.exists?(identifier: code) do + code = DCODES.sample(num).join + end + code + end def ec_public_auth major_school unless User.current.admin? || major_school.template_major || major_school.school.users.where(:id => User.current.id).count > 0 || diff --git a/app/controllers/myshixuns_controller.rb b/app/controllers/myshixuns_controller.rb index b1ac0073..6316446e 100644 --- a/app/controllers/myshixuns_controller.rb +++ b/app/controllers/myshixuns_controller.rb @@ -535,7 +535,7 @@ class MyshixunsController < ApplicationController end logger.info("#############status: #{status}") logger.info("#############resubmit: #{resubmit}") - record = EvaluateRecord.where(:game_id => game_id).first + record = EvaluateRecord.where(:identifier => params[:sec_key]).first logger.info("training_task_status start#3**#{game_id}**** #{Time.now.strftime("%Y-%m-%d %H:%M:%S.%L")}") # status:0表示评测成功 diff --git a/app/services/games_service.rb b/app/services/games_service.rb index 8b5e486d..91645e84 100644 --- a/app/services/games_service.rb +++ b/app/services/games_service.rb @@ -347,10 +347,12 @@ class GamesService rev = params[:rev] ? params[:rev] : "master" content_modified = 0 ActiveRecord::Base.transaction do + sec_key = generates_identifier(EvaluateRecord, 10) # params[:evaluate] 实训评测时更新必须给的参数,需要依据该参数做性能统计,其它类型的更新可以跳过 # 自动保存的时候evaluate为0;点评测的时候为1 if params[:evaluate] == 1 - record = EvaluateRecord.create!(:user_id => current_user.id, :shixun_id => @myshixun.shixun.id, :game_id => @game.id) + record = EvaluateRecord.create!(:user_id => current_user.id, :shixun_id => @myshixun.shixun.id, + :game_id => @game.id, :identifier => sec_key) Rails.logger.warn("##game is is #{@game.id}, record id is #{record.id}, time is**** #{Time.now.strftime("%Y-%m-%d %H:%M:%S.%L")}") # @myshixun.student_works.update_all(:update_time => Time.now) if !@myshixun.student_works.blank? student_work_time = format("%.3f", (Time.now.to_f - record.created_at.to_f)).to_f @@ -385,7 +387,7 @@ class GamesService if content != last_content && code_file.blank? raise("update file failed") else - return {:success => "success", :resubmit => resubmit ,:content_modified => content_modified} + return {:success => "success", :resubmit => resubmit ,:content_modified => content_modified, sec_key: record.identifier} end end rescue Exception => e @@ -546,7 +548,7 @@ class GamesService :testCases => "#{testCases}", :resubmit => "#{resubmit}", :times => params[:first].to_i, :podType => shixun.webssh, :containers => "#{Base64.urlsafe_encode64(container_limit(shixun.mirror_repositories))}", :tpmScript => "#{tpmScript}", :timeLimit => "#{shixun.exec_time}", :content_modified => content_modified, :persistenceName => shixun.identifier, - :isPublished => (shixun.status < 2 ? 0 : 1)} + :isPublished => (shixun.status < 2 ? 0 : 1), :sec_key => params[:sec_key]} # 评测有文件输出的需要特殊传字段 path:表示文件存储的位置 params['file'] = Base64.urlsafe_encode64({:path => "#{game_challenge.picture_path}"}.to_json) if game_challenge.picture_path.present? @@ -569,7 +571,8 @@ class GamesService # ----单测模式end return {:result => "success", :resubmit => resubmit, :ableToCreate => res['ableToCreate'], :waitNum => res['waitNum'], - :waitingTime => res['waitingTime'], :position => game_challenge.position, :port => res['port'], :had_done => game.had_done } + :waitingTime => res['waitingTime'], :position => game_challenge.position, :port => res['port'], + :had_done => game.had_done} rescue Exception => e Rails.logger.error("评测出错,详情:" + e.message) return {:result => 'fail', :contents =>"实训云平台繁忙(繁忙等级:502),请稍后刷新并重试", :position => game_challenge.position, :had_done => game.had_done} @@ -878,7 +881,7 @@ class GamesService mirror_name = shixun.mirror_name # 轮询结束,更新评测耗时 - e_record = EvaluateRecord.where(:game_id => game.id).first + e_record = EvaluateRecord.where(:identifier => params[:sec_key]).first if game_status == 0 || game_status == 2 if e_record front_js = format("%.3f", (Time.now.to_f - e_record.try(:updated_at).to_f)).to_f diff --git a/db/migrate/20190524005242_add_identifier_to_evaluate_records.rb b/db/migrate/20190524005242_add_identifier_to_evaluate_records.rb new file mode 100644 index 00000000..7fe00179 --- /dev/null +++ b/db/migrate/20190524005242_add_identifier_to_evaluate_records.rb @@ -0,0 +1,8 @@ +class AddIdentifierToEvaluateRecords < ActiveRecord::Migration + def change + add_column :evaluate_records, :identifier, :string + add_index :evaluate_records, :identifier, unique: true + remove_index :evaluate_records, :name => :game + remove_index :evaluate_records, :name => :index_evaluate_records_on_user_id + end +end