Commit 621fea4a authored by 李丛阳's avatar 李丛阳

task

parent 606c472e
......@@ -5,7 +5,7 @@ import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* Created by lcy on 18/1/3.
......@@ -27,5 +27,14 @@ public class BCodeFile {
private String id;
private String pId;
/**
* 容器属性
*
* port 端口
*
* endTime 结束时间 毫秒
*
*/
private Map lxcParam;
}
......@@ -13,8 +13,11 @@ public class BCodeLxc {
/*容器id*/
private String containerId;
/*容器 port*/
private int containerPort;
/*已开启时间*/
private String totalMin;
private int totalMin;
/*开始时间*/
private Date startDate;
......
package org.rcisoft.business.bcode.service.impl;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobBuilder;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.*;
import org.rcisoft.business.bcode.model.BCodeLxc;
import org.rcisoft.business.bcode.service.BCodeLxcService;
import org.rcisoft.business.bcode.task.StopLxcJob;
import org.rcisoft.common.component.Global;
import org.rcisoft.core.command.LxcCommand;
import org.rcisoft.core.result.CommandResult;
import org.rcisoft.core.result.ResultCode;
import org.rcisoft.core.service.RcRedisService;
import org.rcisoft.core.service.SerializationUtils;
import org.rcisoft.core.util.FreemarkerUtil;
......@@ -18,10 +17,7 @@ import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Service;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
/**
* Created by lcy on 18/1/9.
......@@ -69,6 +65,7 @@ public class BCodeLxcServiceImpl implements BCodeLxcService {
port = this.getIdleWithPorts(ports,port,maxPort);
/*端口满了*/
if(0 == port) {
result = new CommandResult(ResultCode.FAIL,"端口全部占用,请稍后!",lxc);
return result;
}
ports.add(port);
......@@ -83,12 +80,21 @@ public class BCodeLxcServiceImpl implements BCodeLxcService {
global.getLxcDockerfilePath() + File.separator + lxc.getUserId() ;
result = freemarkerUtil.generatorFileFromModel(this.getModelProject(lxc.getCode()),destPath
,param);
if(!result.isSuccess())
if(!result.isSuccess()) {
result = new CommandResult(ResultCode.FAIL,"容器配置文件有误!",lxc);
return result;
}
/*2.docker-compose 起容器*/
result = lxcCommand.startOrDownLxc(destPath);
if(!result.isSuccess())
if(!result.isSuccess()) {
result = new CommandResult(ResultCode.FAIL,"容器启动失败!",lxc);
return result;
}
lxc.setContainerPort(port);
lxc.setStartDate(new Date());
lxc.setTotalMin(global.getLxcMinu());
Date endTime = new Date(lxc.getStartDate().getTime() + ( global.getLxcMinu() * 60 * 1000 ));
lxc.setShutdownDate(endTime);
/*3.redis 放置状态*/
rcRedisServiceImpl.setBytes(global.getLxcPrefix() + lxc.getUserId(),
SerializationUtils.serializer(lxc));
......@@ -102,7 +108,22 @@ public class BCodeLxcServiceImpl implements BCodeLxcService {
JobDetail job = JobBuilder.newJob(StopLxcJob.class).
withIdentity(global.getLxcPrefix() + lxc.getUserId(), JOB_LXC_GROUP).
usingJobData(new JobDataMap(jobParam)).build();
Trigger trigger = TriggerBuilder.newTrigger().forJob(job)
.usingJobData(new JobDataMap(jobParam))
.withIdentity(global.getLxcPrefix() + lxc.getUserId())
.startAt(endTime).build();
// 触发器时间设定
Scheduler sched = schedulerFactoryBean.getScheduler();
/*触发器*/
try {
sched.scheduleJob(job, trigger);
// 启动
if (!sched.isShutdown()) {
sched.start();
}
}catch (Exception e){
log.error(e.getMessage());
}
/*5.端口放进去 */
ports.add(port);
rcRedisServiceImpl.setBytes("lxcPorts",
......
......@@ -7,15 +7,17 @@ import org.rcisoft.business.bchapter.entity.BChapter;
import org.rcisoft.business.bcode.model.BCodeFile;
import org.rcisoft.business.bcode.model.BCodeLxc;
import org.rcisoft.business.bcode.model.CodeType;
import org.rcisoft.business.bcode.service.BCodeLxcService;
import org.rcisoft.business.bcode.service.BCodeService;
import org.rcisoft.business.brslstudent.dao.BRSlStudentRepository;
import org.rcisoft.business.brslstudent.entity.BRSlStudent;
import org.rcisoft.business.bsl.dao.BSlRepository;
import org.rcisoft.business.bsl.entity.BSl;
import org.rcisoft.business.bstudent.entity.BStudent;
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.service.RcRedisService;
import org.rcisoft.core.service.SerializationUtils;
import org.rcisoft.core.util.IdGen;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
......@@ -23,9 +25,7 @@ import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
......@@ -52,6 +52,9 @@ public class BCodeServiceImpl implements BCodeService {
@Autowired
private BSlRepository bSlRepository;
@Autowired
private BCodeLxcService bCodeLxcServiceImpl;
/**
* 读文件
* @param slId
......@@ -107,18 +110,18 @@ public class BCodeServiceImpl implements BCodeService {
File simpleFile = new File(file);
if(!simpleFile.exists()) {
simpleFile.createNewFile();
codeFile = new BCodeFile("0", "", null, simpleFile.getName(),IdGen.uuid(), "");
codeFile = new BCodeFile("0", "", null, simpleFile.getName(),IdGen.uuid(), "",null);
codeFileList.add(codeFile);
}else{
String content = FileUtils.readFileToString(simpleFile);
///List<String> fileName = new ArrayList<>();
//fileName.add(simpleFile.getName());
codeFile = new BCodeFile("0", content,new Date(simpleFile.lastModified()), simpleFile.getName(), IdGen.uuid(),"");
codeFile = new BCodeFile("0", content,new Date(simpleFile.lastModified()), simpleFile.getName(), IdGen.uuid(),"",null);
codeFileList.add(codeFile);
}
}catch (Exception e){
log.debug(file + " is not exist");
codeFile = new BCodeFile("0", "", null, "","", "");
codeFile = new BCodeFile("0", "", null, "","", "",null);
codeFileList.add(codeFile);
}
......@@ -136,6 +139,7 @@ public class BCodeServiceImpl implements BCodeService {
BCodeLxc lxc = new BCodeLxc();
List<BCodeFile> codeFile = new ArrayList<>();
/*工程存储路径*/
String path = global.getBASE_UPLOAD_SERVER_LOCATION() + File.separator
+ global.getCOURSE_LOCATION() + File.separator
+ global.getSL_LOCATION() + File.separator
......@@ -146,11 +150,9 @@ public class BCodeServiceImpl implements BCodeService {
+ StudentCode;
lxc.setUserId(userInfo);
lxc.setWorkingDir(path);
try{
//xuehao
File simplePath = new File(path);
BCodeFile simpleCodeFile = new BCodeFile("0", "", null, "",IdGen.uuid(),"");
BCodeFile simpleCodeFile = new BCodeFile("0", "", null, "",IdGen.uuid(),"",null);
codeFile.add(simpleCodeFile);
if(!simplePath.exists()) {
simplePath.mkdirs();
......@@ -159,10 +161,35 @@ public class BCodeServiceImpl implements BCodeService {
boolean isFile = true;
getFileList(simplePath,codeFile,simpleCodeFile,isFile);
}
/*容器是否已启动*/
byte[] results = rcRedisServiceImpl.getBytes(global.getLxcPrefix() + lxc.getUserId());
if(null != results) {
Map param = new HashMap<>();
lxc = SerializationUtils.deserializer(results, BCodeLxc.class);
/*已开启*/
if(null != lxc && codeFile.size() > 0){
/*容器参数*/
param.put("port", lxc.getContainerPort());
param.put("endTime",lxc.getShutdownDate().getTime());
codeFile.get(0).setLxcParam(param);
}
/*开启容器*/
else{
CommandResult result = bCodeLxcServiceImpl.startBCodeLxc(lxc);
/*启动失败*/
if(null == result || !result.isSuccess()){
log.error("启动失败" + result.getMessage());
throw new ServiceException(400,result.getMessage());
}
param.put("port", lxc.getContainerPort());
param.put("endTime",lxc.getShutdownDate().getTime());
}
codeFile.get(0).setLxcParam(param);
}
}catch (Exception e){
log.debug(path + " is not exist");
BCodeFile simpleCodeFile = new BCodeFile("0", "", null,"","", "");
BCodeFile simpleCodeFile = new BCodeFile("0", "", null,"","", "",null);
}
return codeFile;
......@@ -264,11 +291,11 @@ public class BCodeServiceImpl implements BCodeService {
FileUtils.write(simpleFile,content,"UTF-8");
//List<String> fileName = new ArrayList<>();
//fileName.add(simpleFile.getName());
codeFile = new BCodeFile("0", content,new Date(simpleFile.lastModified()),simpleFile.getName(),IdGen.uuid(), "");
codeFile = new BCodeFile("0", content,new Date(simpleFile.lastModified()),simpleFile.getName(),IdGen.uuid(), "",null);
}catch (Exception e){
log.debug(file + "创建失败");
codeFile = new BCodeFile("0", "", null, "","","");
codeFile = new BCodeFile("0", "", null, "","","",null);
}
return codeFile;
}
......
......@@ -257,5 +257,11 @@ public class Global {
@Value("${lxc.lxc_maxPort}")
private Integer lxcMaxPort;
/**
* 容器时间
*/
@Value("${lxc.lxc_minu}")
private Integer lxcMinu;
}
......@@ -60,10 +60,9 @@ public class RemoteExecuteCommand {
* 命令执行完后返回的结果值
* @since V0.1
*/
public String execute(String cmd){
public String execute(String cmd,int execTime){
log.info("执行:{}",cmd);
String result="";
int exceTime=0;
try {
if(login()){ //这里偶尔会由于网络原因抛出异常
Session session= conn.openSession();//打开一个会话
......@@ -77,14 +76,14 @@ public class RemoteExecuteCommand {
}
} catch (Exception e) {
e.printStackTrace();
if(exceTime<10) {
if(execTime<10) {
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
exceTime++;
return execute(cmd); //异常了就循环执行方法,直到正常了才返回
execTime++;
return execute(cmd,execTime); //异常了就循环执行方法,直到正常了才返回
}else{
//throw new BusinessException(ResultEnums.NETWORK_ERROR);
}
......@@ -107,7 +106,7 @@ public class RemoteExecuteCommand {
* @since V0.1
*/
public String execute(String cmd,String location){
return execute("cd "+location+" && "+cmd);
return execute("cd "+location+" && "+cmd,1);
}
......
......@@ -20,6 +20,7 @@ public enum ResultExceptionEnum {
SERVER_NOT_FOUND(4006,"服务器不存在"),
SERVER_HAS_APP(4007,"服务器仍有关联应用,不可删除"),
OS_HAS_SERVER(4008,"操作系统仍有关联服务器,不可删除"),
START_LXC_SERVER(4009,"启动容器失败"),
;
private Integer code;
......
......@@ -74,6 +74,7 @@ lxc:
lxc_dockerfilePath: /eduLxc
lxc_initPort: 20001
lxc_maxPort: 30000
lxc_minu: 30
springfox:
documentation:
swagger:
......
......@@ -77,6 +77,7 @@ lxc:
lxc_dockerfilePath: /eduLxc
lxc_initPort: 20001
lxc_maxPort: 30000
lxc_minu: 30
jwt:
header: Authorization
secret: mySecret
......
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