Commit 4e8cf607 authored by 李丛阳's avatar 李丛阳

关闭容器

parent 96f5fb05
......@@ -11,6 +11,7 @@ import org.rcisoft.common.controller.PaginationController;
import org.rcisoft.core.constant.MessageConstant;
import org.rcisoft.core.exception.ServiceException;
import org.rcisoft.core.model.PersistModel;
import org.rcisoft.core.result.CommandResult;
import org.rcisoft.core.result.Result;
import org.rcisoft.core.result.ResultServiceEnums;
import org.rcisoft.core.util.CompileUtil;
......@@ -74,6 +75,37 @@ public class BCodeController extends PaginationController {
@ApiOperation(value="实验是否启动", notes="实验是否启动")
@ApiImplicitParams({@ApiImplicitParam(name = "slId", value = "开课ID", required = true, dataType = "varchar"),
@ApiImplicitParam(name = "chapId", value = "章节ID", required = true, dataType = "varchar")})
@GetMapping(value = "/isStartedExam")
public Result isStartedExam(@RequestParam("slId") String slId,
@RequestParam("chapId") String chapId){
Integer result = bCodeServiceImpl.isOpenLxcFromUser(UserUtil.getUserInfoProp(getToken(), UserUtil.USER_ID), slId, chapId);
return Result.builder(new PersistModel(result),
MessageConstant.MESSAGE_ALERT_SUCCESS,
MessageConstant.MESSAGE_ALERT_ERROR,
result);
}
@ApiOperation(value="关闭实验", notes="关闭实验")
@ApiImplicitParams({@ApiImplicitParam(name = "slId", value = "开课ID", required = true, dataType = "varchar"),
@ApiImplicitParam(name = "chapId", value = "章节ID", required = true, dataType = "varchar")})
@GetMapping(value = "/stopExam")
public Result stopExam(@RequestParam("slId") String slId,
@RequestParam("chapId") String chapId){
CommandResult result = bCodeServiceImpl.stopExam(UserUtil.getUserInfoProp(getToken(), UserUtil.USER_ID), slId, chapId);
return Result.builder(new PersistModel(result.isSuccess()? 1 : 0),
result.getMessage(),
result.getMessage(),
result);
}
@ApiOperation(value="保存实验代码", notes="学生保存自己的实验")
@ApiImplicitParams({@ApiImplicitParam(name = "slId", value = "开课ID", required = true, dataType = "varchar"),
@ApiImplicitParam(name = "chapId", value = "章节ID", required = true, dataType = "varchar"),
......
......@@ -20,7 +20,7 @@ public interface BCodeLxcService {
* 关闭code 容器
* @param lxc
*/
public void stopBCodeLxc(BCodeLxc lxc);
public CommandResult stopBCodeLxc(BCodeLxc lxc);
/**
* 清理 code 容器
......
......@@ -2,6 +2,7 @@ package org.rcisoft.business.bcode.service;
import org.rcisoft.business.bcode.model.BCodeFile;
import org.rcisoft.business.bcode.model.StudentFile;
import org.rcisoft.core.result.CommandResult;
import java.util.List;
......@@ -34,4 +35,22 @@ public interface BCodeService {
String exportFiles(String slId, String studentId, String type, String slCode);
List<StudentFile> queryAllStudentCode(String slId, String chapterId);
/**
* 判断是否开启
*
* @param userId
* @return 1 未开
* 0 已开
*/
Integer isOpenLxcFromUser(String userId,String slId,String chapterId);
/**
* 关闭 实验环境
* @param userId
* @param slId
* @param chapterId
* @return
*/
CommandResult stopExam(String userId, String slId, String chapterId);
}
......@@ -174,8 +174,24 @@ public class BCodeLxcServiceImpl implements BCodeLxcService {
@Override
public void stopBCodeLxc(BCodeLxc lxc) {
public CommandResult stopBCodeLxc(BCodeLxc lxc) {
/*1.拼key*/
String key = global.getLxcPrefix() + "_" + lxc.getUserId();
/*2.拼docker-compose.yml */
String destPath = global.getPHYSICAL_UPLOAD_SERVER_LOCATION() + File.separator +
global.getLxcDockerfilePath() + File.separator + lxc.getUserId() + File.separator;;
CommandResult commandResult = null;
commandResult = lxcCommand.startOrDownLxc(destPath,false);
if(!commandResult.isSuccess())
return commandResult;
/*3.移除容器*/
rcRedisServiceImpl.remove(key);
/*4.移除端口*/
rcRedisServiceImpl.removeList("lxcKeys",
key);
rcRedisServiceImpl.removeList("lxcPorts",lxc.getContainerPort() + "");
return commandResult;
}
......
......@@ -18,6 +18,7 @@ import org.rcisoft.business.bstudent.entity.BStudentDto;
import org.rcisoft.common.component.Global;
import org.rcisoft.core.exception.ServiceException;
import org.rcisoft.core.result.CommandResult;
import org.rcisoft.core.result.ResultCode;
import org.rcisoft.core.result.ResultExceptionEnum;
import org.rcisoft.core.result.ResultServiceEnums;
import org.rcisoft.core.service.RcRedisService;
......@@ -507,4 +508,48 @@ public class BCodeServiceImpl implements BCodeService {
}
return studentFiles;
}
@Override
public Integer isOpenLxcFromUser(String userId,String slId,String chapterId) {
BCodeLxc lxc = null;
/*容器是否已启动*/
byte[] results = rcRedisServiceImpl.getBytes(global.getLxcPrefix() + "_" + userId);
if(null != results) {
lxc = SerializationUtils.deserializer(results, BCodeLxc.class);
/*别的课或别的节已开启容器,返回错误*/
if(null != lxc && (!slId.equals(lxc.getSl()) || !chapterId.equals(lxc.getChapterId()))){
/*已开*/
return 0;
}
/*再次打开已开启的容器*/
return 1;
}
/*未开*/
return 1;
}
@Override
public CommandResult stopExam(String userId, String slId, String chapterId) {
BCodeLxc lxc = null;
/*容器是否已启动*/
byte[] results = rcRedisServiceImpl.getBytes(global.getLxcPrefix() + "_" + userId);
if(null != results) {
lxc = SerializationUtils.deserializer(results, BCodeLxc.class);
/*别的课或别的节已开启容器,返回错误*/
if(null != lxc && (!slId.equals(lxc.getSl()) || !chapterId.equals(lxc.getChapterId()))){
/*已开*/
return new CommandResult(ResultCode.FAIL,"未开启对应的实验",null);
}
else if(null == lxc)
return new CommandResult(ResultCode.FAIL,"未开启实验",null);
else {
/*关闭容器*/
return bCodeLxcServiceImpl.stopBCodeLxc(lxc);
}
}
/*未开*/
return new CommandResult(ResultCode.FAIL,"未开启实验",null);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment