新建实训 --事务一致性及异常处理

This commit is contained in:
huang 2017-04-12 13:36:13 +08:00
parent b2a74c7d5d
commit 55972fdbe6
1 changed files with 23 additions and 9 deletions

View File

@ -25,7 +25,8 @@ class ChallengesController < ApplicationController
@challenge.shixun = @shixun
@challenge.user = User.current
ActiveRecord::Base.transaction do
if @challenge.save
begin
@challenge.save!
if params[:sample][:input].length > 0
params[:sample][:input].each_with_index do |value, index|
unless (value == params[:sample][:output][index] && value.blank?)
@ -43,21 +44,34 @@ class ChallengesController < ApplicationController
ChallengeTag.create(:name => input, :challenge_id => @challenge.id)
end
end
# 向jenkins端发送请求构建pipeline片段
params = {:shixunType => "#{@challenge.evaluation_way.to_i}", :filePath => "#{@challenge.path}", :step => "#{@challenge.position}"}
uri = URI("#{jenkins_shixuns}/jenkins-exec/api/generateScriptPerChallenge")
res = uri_exec uri, params
# 向jenkins端发送请求构建pipeline片段实时返回结果
begin
jenkins_shixuns = Redmine::Configuration['jenkins_shixuns']
params = {:shixunType => "#{@challenge.evaluation_way.to_i}", :filePath => "#{@challenge.path}", :step => "#{@challenge.position}", :challengeId => "#{@challenge.id}"}
uri = URI("#{jenkins_shixuns}/jenkins-exec/api/generateScriptPerChallenge")
res = uri_exec uri, params
if res.blank?
raise("jenkins服务器异常")
end
rescue
raise("jenkins服务器异常")
end
respond_to do |format|
format.html {redirect_to shixun_challenge_path(@challenge, :shixun_id => @shixun), notice: '创建成功!'}
end
else
respond_to do |format|
format.html { render :action => 'new' }
end
rescue Exception => e
flash[:error] = "#{e.message}"
redirect_to new_shixun_challenge_path(:shixun_id => @shixun.id)
raise ActiveRecord::Rollback
end
end
end
# 处理新建challenge返回的pipeline片段
def fragment_from_jenkins
end
def index
# 顶部导航
@challenges = @shixun.challenges.order("position desc")