From b887f08e9a077c03a8a09c3f2b7b217f7eeeafaf Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Wed, 11 Dec 2019 09:24:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=BB=BA=E5=AE=9E=E8=AE=AD=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/shixuns_controller.rb | 5 ++ app/services/shixuns/create_shixun_service.rb | 61 +++++++++---------- 2 files changed, 33 insertions(+), 33 deletions(-) diff --git a/app/controllers/shixuns_controller.rb b/app/controllers/shixuns_controller.rb index 702d2254e..f5c951836 100644 --- a/app/controllers/shixuns_controller.rb +++ b/app/controllers/shixuns_controller.rb @@ -364,7 +364,12 @@ class ShixunsController < ApplicationController end def create + begin @shixun = CreateShixunService.call(current_user, shixun_params, params) + rescue => e + logger_error("shixun_create_error: #{e.message}") + tip_exception("创建实训失败!") + end end def update diff --git a/app/services/shixuns/create_shixun_service.rb b/app/services/shixuns/create_shixun_service.rb index 7f511b357..cf40c55ab 100644 --- a/app/services/shixuns/create_shixun_service.rb +++ b/app/services/shixuns/create_shixun_service.rb @@ -13,39 +13,34 @@ class CreateShixunService < ApplicationService shixun.identifier= identifier main_mirror = MirrorRepository.find params[:main_type] sub_mirrors = MirrorRepository.where(id: params[:sub_type]) - begin - ActiveRecord::Base.transaction do - shixun.save! - # 获取脚本内容 - shixun_script = get_shixun_script(shixun, main_mirror, sub_mirrors) - # 创建额外信息 - ShixunInfo.new(shixun_id: shixun.id, evaluate_script: shixun_script, description: params[:description]) - # 创建合作者 - shixun.shixun_members.create!(user_id: user.id, role: 1) - # 创建镜像 - ShixunMirrorRepository.create!(:shixun_id => shixun.id, :mirror_repository_id => main_mirror.id) - # 创建主服务配置 - ShixunServiceConfig.create!(:shixun_id => shixun.id, :mirror_repository_id => main_mirror.id) - # 创建子镜像相关数据(实训镜像关联表,子镜像服务配置) - sub_mirrors.each do |sub| - ShixunMirrorRepository.create!(:shixun_id => shixun.id, :mirror_repository_id => sub.id) - # 实训子镜像服务配置 - name = sub.name #查看镜像是否有名称,如果没有名称就不用服务配置 - ShixunServiceConfig.create!(:shixun_id => shixun.id, :mirror_repository_id => sub.id) if name.present? - end - # 创建版本库 - repo_path = repo_namespace(user.login, shixun.identifier) - GitService.add_repository(repo_path: repo_path) - shixun.update_column(:repo_name, repo_path.split(".")[0]) - # 如果是云上实验室,创建相关记录 - if !Laboratory.current.main_site? - Laboratory.current.laboratory_shixuns.create!(shixun: shixun, ownership: true) - end - return shixun + ActiveRecord::Base.transaction do + shixun.save! + # 获取脚本内容 + shixun_script = get_shixun_script(shixun, main_mirror, sub_mirrors) + # 创建额外信息 + ShixunInfo.new(shixun_id: shixun.id, evaluate_script: shixun_script, description: params[:description]) + # 创建合作者 + shixun.shixun_members.create!(user_id: user.id, role: 1) + # 创建镜像 + ShixunMirrorRepository.create!(:shixun_id => shixun.id, :mirror_repository_id => main_mirror.id) + # 创建主服务配置 + ShixunServiceConfig.create!(:shixun_id => shixun.id, :mirror_repository_id => main_mirror.id) + # 创建子镜像相关数据(实训镜像关联表,子镜像服务配置) + sub_mirrors.each do |sub| + ShixunMirrorRepository.create!(:shixun_id => shixun.id, :mirror_repository_id => sub.id) + # 实训子镜像服务配置 + name = sub.name #查看镜像是否有名称,如果没有名称就不用服务配置 + ShixunServiceConfig.create!(:shixun_id => shixun.id, :mirror_repository_id => sub.id) if name.present? + end + # 创建版本库 + repo_path = repo_namespace(user.login, shixun.identifier) + GitService.add_repository(repo_path: repo_path) + shixun.update_column(:repo_name, repo_path.split(".")[0]) + # 如果是云上实验室,创建相关记录 + if !Laboratory.current.main_site? + Laboratory.current.laboratory_shixuns.create!(shixun: shixun, ownership: true) end - rescue => e - Rails.logger.error(e.message) - render_error("实训创建失败:#{e.message}") + return shixun end end @@ -102,7 +97,7 @@ class CreateShixunService < ApplicationService # 版本库目录空间 def repo_namespace(user, shixun_identifier) - "#{user.login}/#{shixun_identifier}.git" + "#{user}/#{shixun_identifier}.git" end end