|
@@ -0,0 +1,269 @@
|
|
|
|
+package com.caesolver.server;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+import java.io.BufferedReader;
|
|
|
|
+import java.io.File;
|
|
|
|
+import java.io.FileWriter;
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.io.InputStream;
|
|
|
|
+import java.io.InputStreamReader;
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Map;
|
|
|
|
+
|
|
|
|
+import org.apache.http.HttpResponse;
|
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
|
+import org.springframework.scheduling.annotation.Async;
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
+
|
|
|
|
+import com.caesolver.server.util.HttpDownload;
|
|
|
|
+import com.caesolver.server.util.HttpUtils;
|
|
|
|
+import com.caesolver.sftp.FileUtil;
|
|
|
|
+import com.caesolver.sftp.SFTPUtil;
|
|
|
|
+
|
|
|
|
+import net.sf.json.JSONObject;
|
|
|
|
+
|
|
|
|
+@Component
|
|
|
|
+public class SolverService {
|
|
|
|
+ @Value("${caeserverurl}")
|
|
|
|
+ String url;
|
|
|
|
+ @Value("${esserverurl}")
|
|
|
|
+ String esurl;
|
|
|
|
+ @Value("${solverBatchStopUrl}")
|
|
|
|
+ String solverBatchStopUrl;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ SolverHcfdOrderUtil hcfdOrderUtil;
|
|
|
|
+ @Autowired
|
|
|
|
+ SFTPUtil sftpUtil;
|
|
|
|
+ @Value("${HCFD.workdir}")
|
|
|
|
+ String hcfdWorkDir ;
|
|
|
|
+
|
|
|
|
+ public static boolean ISRUN =false;
|
|
|
|
+ protected static Logger log=LoggerFactory.getLogger(SolverService.class);
|
|
|
|
+
|
|
|
|
+ public boolean isRun() {
|
|
|
|
+ return ISRUN;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void precessController(SolverBatchHcfd vo)
|
|
|
|
+ throws IOException, Exception {
|
|
|
|
+
|
|
|
|
+ //logPut(vo,"Solver start");
|
|
|
|
+ filehHcfdDown(vo);//下载文件
|
|
|
|
+ String jobOrder =hcfdOrderUtil.createOrder(vo);
|
|
|
|
+ jobExec( jobOrder, vo);
|
|
|
|
+ sftpUtil.uploadBatchDataOut(vo);
|
|
|
|
+// //求解结束
|
|
|
|
+ logPut(vo,"Solver end");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ private void jobExec( String jobOrder, SolverBatchHcfd vo) throws Exception {
|
|
|
|
+ Process p =null;
|
|
|
|
+ Runtime runtime = Runtime.getRuntime();
|
|
|
|
+ p= runtime.exec(jobOrder);
|
|
|
|
+ jobStateLine(vo.getNmlId(), solverBatchStopUrl);//修改状态 为求解中
|
|
|
|
+ InputStream fis = p.getInputStream();
|
|
|
|
+ InputStreamReader isr = new InputStreamReader(fis);
|
|
|
|
+ BufferedReader br = new BufferedReader(isr);
|
|
|
|
+ String line = null;
|
|
|
|
+ boolean isSuccess=true;
|
|
|
|
+ while ((line = br.readLine()) != null) {
|
|
|
|
+ log.info(line);
|
|
|
|
+ if(!line.trim().equals("")) {
|
|
|
|
+ logPut(vo,line);
|
|
|
|
+ }
|
|
|
|
+ if(isSuccess&&line.contains("Error")) {//包含Error
|
|
|
|
+ isSuccess=false;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ p.waitFor();
|
|
|
|
+ if(isSuccess) {//成功
|
|
|
|
+ jobStateSuccess(vo.getNmlId());//修改状态 为求解成功
|
|
|
|
+ }else {//失败
|
|
|
|
+ jobStateError(vo.getNmlId());;//修改状态 为求解失败
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ //日志文件输入
|
|
|
|
+ private void logPut(SolverBatchHcfd vo,String line) throws Exception {
|
|
|
|
+ String gridFristName =vo.getGridName().split("\\.")[0];
|
|
|
|
+ File workerDataDataout =new File(hcfdWorkDir+"/"+vo.getNmlId()+"/"+gridFristName+"/test/"+"data_out");
|
|
|
|
+ File f = new File(workerDataDataout+"/run.log");
|
|
|
|
+ if(!f.exists()) {
|
|
|
|
+ f.createNewFile();
|
|
|
|
+ }
|
|
|
|
+ FileWriter writer = new FileWriter(f,true);
|
|
|
|
+ writer.append(line);
|
|
|
|
+ writer.write("\n");//换行
|
|
|
|
+ writer.flush();
|
|
|
|
+ writer.close();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 文件下载
|
|
|
|
+ *
|
|
|
|
+ */
|
|
|
|
+ private void filehHcfdDown(SolverBatchHcfd vo) throws Exception {
|
|
|
|
+ String boundaryFileId= vo.getBoundaryFileId();
|
|
|
|
+ String boundaryName =vo.getBoundaryName();
|
|
|
|
+ String gridFileId =vo.getGridFileId();
|
|
|
|
+ String gridName =vo.getGridName();
|
|
|
|
+ String nmlFileId =vo.getNmlFileId();
|
|
|
|
+ //删除历史
|
|
|
|
+ FileUtil.deleteDir(hcfdWorkDir+"/"+vo.getNmlId());
|
|
|
|
+ String gridFristName =gridName.split("\\.")[0];
|
|
|
|
+ //创建文件夹
|
|
|
|
+ File workerDir =new File(hcfdWorkDir+"/"+vo.getNmlId());
|
|
|
|
+ if(!workerDir.isDirectory()) {
|
|
|
|
+ workerDir.mkdirs();
|
|
|
|
+ }
|
|
|
|
+ File workerDataIn =new File(hcfdWorkDir+"/"+vo.getNmlId()+"/"+gridFristName+"/"+"data_in");
|
|
|
|
+ if(!workerDataIn.isDirectory()) {
|
|
|
|
+ workerDataIn.mkdirs();
|
|
|
|
+ }
|
|
|
|
+ File workerDataDataout =new File(hcfdWorkDir+"/"+vo.getNmlId()+"/"+gridFristName+"/test/"+"data_out");
|
|
|
|
+ if(!workerDataDataout.isDirectory()) {
|
|
|
|
+ workerDataDataout.mkdirs();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ fileDown(gridFileId, workerDataIn+"/"+gridName);
|
|
|
|
+ fileDown(boundaryFileId, workerDataIn+"/"+boundaryName);
|
|
|
|
+ fileDown(nmlFileId,hcfdWorkDir+"/"+vo.getNmlId()+"/"+gridFristName+"/test/hcfd.nml");
|
|
|
|
+ }
|
|
|
|
+ /**
|
|
|
|
+ * 文件下载
|
|
|
|
+ *
|
|
|
|
+ */
|
|
|
|
+ private void fileDown(String fileId,String saveFilePath) throws Exception {
|
|
|
|
+ String downUrl=url+"?channelNo=service&transCode=B00022&clientToken=e47b87eec69545559d1e81e56626da68&id="+fileId;
|
|
|
|
+ HttpDownload.download(downUrl, saveFilePath);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 任务状态为执行中
|
|
|
|
+ *
|
|
|
|
+ */
|
|
|
|
+ @Async
|
|
|
|
+ private void jobStateLine(String nmlId,String solverIp) throws Exception {
|
|
|
|
+ Map<String, String> paramMap=new HashMap<>();
|
|
|
|
+ paramMap.put("channelNo", "service");
|
|
|
|
+ paramMap.put("clientToken", "e47b87eec69545559d1e81e56626da68");
|
|
|
|
+ paramMap.put("transCode", "SBH011");
|
|
|
|
+ paramMap.put("nmlId", nmlId);
|
|
|
|
+ paramMap.put("solverIp", solverIp);
|
|
|
|
+ paramMap.put("userId","5f06c8bc77234f969d13e160b54c27e3");
|
|
|
|
+ HttpUtils.doPost(url,"","",new HashMap<String, String>(),new HashMap<String, String>(),paramMap);
|
|
|
|
+ }
|
|
|
|
+ /**
|
|
|
|
+ * 任务状态为成功
|
|
|
|
+ *
|
|
|
|
+ */
|
|
|
|
+ @Async
|
|
|
|
+ private void jobStateSuccess(String nmlId) throws Exception {
|
|
|
|
+ Map<String, String> paramMap=new HashMap<>();
|
|
|
|
+ paramMap.put("channelNo", "service");
|
|
|
|
+ paramMap.put("clientToken", "e47b87eec69545559d1e81e56626da68");
|
|
|
|
+ paramMap.put("transCode", "SBH009");
|
|
|
|
+ paramMap.put("nmlId", nmlId);
|
|
|
|
+ paramMap.put("userId","5f06c8bc77234f969d13e160b54c27e3");
|
|
|
|
+ HttpUtils.doPost(url,"","",new HashMap<String, String>(),new HashMap<String, String>(),paramMap);
|
|
|
|
+ }
|
|
|
|
+ /**
|
|
|
|
+ * 任务状态为失败
|
|
|
|
+ *
|
|
|
|
+ */
|
|
|
|
+ @Async
|
|
|
|
+ private void jobStateError(String nmlId) throws Exception {
|
|
|
|
+ Map<String, String> paramMap=new HashMap<>();
|
|
|
|
+ paramMap.put("channelNo", "service");
|
|
|
|
+ paramMap.put("clientToken", "e47b87eec69545559d1e81e56626da68");
|
|
|
|
+ paramMap.put("transCode", "SBH010");
|
|
|
|
+ paramMap.put("nmlId", nmlId);
|
|
|
|
+ paramMap.put("userId","5f06c8bc77234f969d13e160b54c27e3");
|
|
|
|
+ HttpUtils.doPost(url,"","",new HashMap<String, String>(),new HashMap<String, String>(),paramMap);
|
|
|
|
+ }
|
|
|
|
+ public boolean isWait(String nmlId) {//挂起任务 不执行
|
|
|
|
+ Map<String, String> paramMap=new HashMap<>();
|
|
|
|
+ paramMap.put("channelNo", "service");
|
|
|
|
+ paramMap.put("clientToken", "e47b87eec69545559d1e81e56626da68");
|
|
|
|
+ paramMap.put("transCode", "SBH014");
|
|
|
|
+ paramMap.put("nmlId", nmlId);
|
|
|
|
+ paramMap.put("userId","5f06c8bc77234f969d13e160b54c27e3");
|
|
|
|
+ try {
|
|
|
|
+ HttpResponse response= HttpUtils.doPost(url,"","",new HashMap<String, String>(),new HashMap<String, String>(),paramMap);
|
|
|
|
+ JSONObject jsonObject = JSONObject.fromObject(EntityUtils.toString(response.getEntity()));
|
|
|
|
+ JSONObject result = JSONObject.fromObject(jsonObject.get("result"));
|
|
|
|
+ String state = String.valueOf(jsonObject.get("state"));
|
|
|
|
+ String returnCode = String.valueOf(jsonObject.get("returnCode"));
|
|
|
|
+ if ("000000000".equals(returnCode)) {//调用成功
|
|
|
|
+ if("3".equals(state)) {////挂起任务
|
|
|
|
+ return true;
|
|
|
|
+ }else{
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ } else {//调用失败
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ *
|
|
|
|
+ * @param youNumber
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static String int2String(int youNumber) {
|
|
|
|
+ String str = String.format("%010d", youNumber);
|
|
|
|
+ return str;
|
|
|
|
+ }
|
|
|
|
+ /**
|
|
|
|
+ * 强制停止进程
|
|
|
|
+ * @param jobId
|
|
|
|
+ * @throws InterruptedException
|
|
|
|
+ * @throws IOException
|
|
|
|
+ */
|
|
|
|
+ public void solverStop(String nmlId) throws Exception{
|
|
|
|
+ if(SolverHcfdOrderUtil.isWindows()) {//windows
|
|
|
|
+ //winows
|
|
|
|
+ }else {
|
|
|
|
+
|
|
|
|
+ log.info("stop:: "+nmlId);
|
|
|
|
+ Process p =null;
|
|
|
|
+ Runtime runtime = Runtime.getRuntime();
|
|
|
|
+ String[] command = { "/bin/sh", "-c", "ps -ef|grep "+nmlId+"|grep mpirun|awk '{print $2}'"};
|
|
|
|
+ // String[] command = { "/bin/sh", "-c", "ps -ef|grep java |awk '{print $2}'"};
|
|
|
|
+ log.info("ps -ef|grep "+nmlId+"|grep mpirun|awk '{print $2}'");
|
|
|
|
+// p= runtime.exec("ps -ef|grep "+nmlId+"|grep mpirun|awk '{print $2}'");
|
|
|
|
+ p= runtime.exec(command);
|
|
|
|
+ InputStream fis = p.getInputStream();
|
|
|
|
+ InputStreamReader isr = new InputStreamReader(fis);
|
|
|
|
+ BufferedReader br = new BufferedReader(isr);
|
|
|
|
+ String line = null;
|
|
|
|
+ List<String> pids =new ArrayList<>();
|
|
|
|
+ while ((line = br.readLine()) != null) {
|
|
|
|
+ log.info(line);
|
|
|
|
+ pids.add(line);
|
|
|
|
+ }
|
|
|
|
+ p.waitFor();
|
|
|
|
+ for (String pid : pids) {
|
|
|
|
+ log.info("kill -9 "+pid);
|
|
|
|
+ p= runtime.exec("kill -9 "+pid);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|