MDO0039Service.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package com.miniframe.bisiness.mdo;
  2. import java.io.File;
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5. import java.util.List;
  6. import java.util.Map;
  7. import java.util.concurrent.CompletableFuture;
  8. import com.github.dockerjava.api.async.ResultCallback;
  9. import com.github.dockerjava.api.model.Frame;
  10. import com.miniframe.core.ExecProcessFlow;
  11. import com.miniframe.core.exception.BusinessException;
  12. import com.miniframe.core.ext.UtilTools;
  13. import com.miniframe.generate.business.mdo.model.MDO0039BaseModel;
  14. import com.miniframe.mdo.service.LogService;
  15. import com.miniframe.model.mdo.MdoProPython;
  16. import com.miniframe.model.mdo.MdoProPythonSQLBuilder;
  17. import com.miniframe.model.mdo.MdoProject;
  18. import com.miniframe.model.mdo.dao.MdoProPythonMapper;
  19. import com.miniframe.model.mdo.dao.MdoProjectMapper;
  20. import com.miniframe.modo.temp.TemplateGenerator;
  21. import com.miniframe.tools.XIFileUtils;
  22. import com.miniframe.tools.docker.DockerExe;
  23. import freemarker.template.Configuration;
  24. import freemarker.template.Template;
  25. import lombok.SneakyThrows;
  26. /**
  27. * 西工大系统,“新版求解v2”逻辑处理(重新生成不覆盖)。
  28. */
  29. public class MDO0039Service extends MDO0039BaseModel implements ExecProcessFlow {
  30. private static final long serialVersionUID = -7051358269847459502L;
  31. /**
  32. * 西工大系统,“新版求解v2”业务核心处理
  33. */
  34. public void transExecute() throws Exception {
  35. String pid = this.getA_mdo0039().getPid();
  36. MdoProjectMapper proDao = UtilTools.getBean(MdoProjectMapper.class);
  37. MdoProject pro =proDao.selectByPrimaryKey(pid);
  38. if(pro==null){
  39. throw new BusinessException("MDO000001");
  40. }
  41. //创建文件路径
  42. XIFileUtils.mkdir(LogService.BPATH);
  43. XIFileUtils.mkdir(LogService.BPATH+"/"+pro.getId());
  44. XIFileUtils.mkdir(LogService.BPATH+"/"+pro.getId()+"/in");
  45. XIFileUtils.mkdir(LogService.BPATH+"/"+pro.getId()+"/out");
  46. //日志文件清空
  47. LogService.clearLog(pid);
  48. //创建problem.xml
  49. TemplateGenerator.createProblemXml(pid);
  50. //创建run.py
  51. TemplateGenerator.createRunpy(pid);
  52. run(pid);
  53. }
  54. //异步执行
  55. @SneakyThrows
  56. public void run(String pid )throws Exception{
  57. LogService.addLog(pid,"求解——————————————————开始");
  58. DockerExe.stopDocker(pid);
  59. CompletableFuture<Void> cf = CompletableFuture.supplyAsync(()-> {try {
  60. DockerExe.getDockerLogs(pid,new ResultCallback.Adapter<Frame>() {
  61. @Override
  62. public void onNext(Frame frame) {
  63. LogService.addLog(pid,new String(frame.getPayload()));
  64. System.out.print(new String(frame.getPayload()));
  65. super.onNext(frame);
  66. }
  67. @Override
  68. public void onError(Throwable throwable) {
  69. System.err.println("日志获取失败");
  70. throwable.printStackTrace();
  71. super.onError(throwable);
  72. }
  73. @Override
  74. public void onComplete() {
  75. System.out.println("日志获取完成");
  76. super.onComplete();
  77. }
  78. });
  79. LogService.addLog(pid,"求解——————————————————成功");
  80. } catch (Exception e) {
  81. LogService.addLog(pid,"求解——————————————————失败");
  82. e.printStackTrace();
  83. }
  84. return null;
  85. });
  86. }
  87. /**
  88. * 西工大系统,“新版求解v2”业务前处理
  89. */
  90. public void preTransFlow() throws Exception {
  91. this.validater();
  92. }
  93. /**
  94. * 西工大系统,“新版求解v2”业务后处理
  95. */
  96. public void afterTransFlow() throws Exception {
  97. }
  98. /**
  99. * 西工大系统,“新版求解v2”逻辑入口处理方法
  100. */
  101. @SuppressWarnings("rawtypes")
  102. @Override
  103. public Map execute(Map vars) throws Exception {
  104. this.setTransMap(vars);
  105. preTransFlow();// 执行业务开始的规则检查和校验
  106. transExecute();// 执行核心业务段
  107. afterTransFlow();// 执行核心逻辑完成后的收尾逻辑
  108. return this.getTransMap();
  109. }
  110. }