|
@@ -1,16 +1,22 @@
|
|
|
package com.gyee.impala.service.master.diagnose;
|
|
|
|
|
|
|
|
|
-import com.alibaba.fastjson.JSON;
|
|
|
import com.gyee.impala.common.config.GyeeConfig;
|
|
|
import com.gyee.impala.common.config.jsch.JSchConfig;
|
|
|
+import com.gyee.impala.model.master.diagnose.TrainInfo;
|
|
|
import com.gyee.impala.model.master.diagnose.TrainParam;
|
|
|
import com.gyee.impala.service.custom.SftpFileService;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
+import java.io.BufferedReader;
|
|
|
+import java.io.InputStreamReader;
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+import java.util.concurrent.BlockingQueue;
|
|
|
+import java.util.concurrent.LinkedBlockingQueue;
|
|
|
|
|
|
@Service
|
|
|
public class TrainFileModeService {
|
|
@@ -29,15 +35,25 @@ public class TrainFileModeService {
|
|
|
private GyeeConfig gyeeConfig;
|
|
|
|
|
|
|
|
|
- public synchronized void exec(TrainParam trainParam,MultipartFile file) throws Exception{
|
|
|
+ /**
|
|
|
+ * 是否执行结束
|
|
|
+ */
|
|
|
+ private boolean isComplete = true;
|
|
|
+
|
|
|
+
|
|
|
+ BlockingQueue<TrainInfo> console = new LinkedBlockingQueue<TrainInfo>();
|
|
|
+
|
|
|
+
|
|
|
+ public synchronized void exec(TrainParam trainParam, MultipartFile file) throws Exception {
|
|
|
|
|
|
//获取上传文件的文件名
|
|
|
- String type = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));;
|
|
|
+ String type = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
|
|
|
+ ;
|
|
|
/**上传文件**/
|
|
|
- fileService.uploadFile(file.getOriginalFilename(), file.getInputStream(),"10.155.32.14");
|
|
|
+ //fileService.uploadFile(file.getOriginalFilename(), file.getInputStream(),"10.155.32.14");
|
|
|
|
|
|
//文件路径
|
|
|
- String filePath = config.getPath()+file.getOriginalFilename();
|
|
|
+ String filePath = config.getPath() + file.getOriginalFilename();
|
|
|
|
|
|
System.out.println(new Date() + "开始执行脚本...");
|
|
|
Process p;
|
|
@@ -45,22 +61,130 @@ public class TrainFileModeService {
|
|
|
|
|
|
|
|
|
try {
|
|
|
- String[] cmd = {"/bin/sh", "-c", "python " + cmdPath + trainParam.getName() + ".py "+filePath};
|
|
|
+// String[] cmd = {"/bin/sh", "-c", "python " + cmdPath + trainParam.getName() + ".py "+filePath};
|
|
|
+ String cmd = "cmd /c python " + cmdPath + trainParam.getName() + ".py " + filePath;
|
|
|
|
|
|
- System.out.println(cmd[0] + " " + cmd[1] + " " + cmd[2]);
|
|
|
+// System.out.println(cmd[0] + " " + cmd[1] + " " + cmd[2]);
|
|
|
p = Runtime.getRuntime().exec(cmd);
|
|
|
+
|
|
|
+
|
|
|
+ BufferedReader bri = new BufferedReader(new InputStreamReader(p.getInputStream()));
|
|
|
+ BufferedReader bre = new BufferedReader(new InputStreamReader(p.getErrorStream()));
|
|
|
+ String si = null, se = null;
|
|
|
+ while ((si = bri.readLine()) != null || (se = bre.readLine()) != null) {
|
|
|
+ if (si != null) {
|
|
|
+ System.out.println(si);
|
|
|
+ }
|
|
|
+ if (se != null) {
|
|
|
+ System.out.println(se);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ Thread.sleep(5000);
|
|
|
p.waitFor();
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ isComplete = true;
|
|
|
}
|
|
|
System.out.println(new Date() + "脚本执行结束...");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
+ public synchronized void exec(String name, String forecastLabel, String[] inputLabel, String host, MultipartFile file) throws Exception {
|
|
|
|
|
|
+ if (!isComplete) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ isComplete = false;
|
|
|
+ //获取上传文件的文件名
|
|
|
+ String type = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
|
|
|
+ ;
|
|
|
+ /**上传文件**/
|
|
|
+ //fileService.uploadFile(file.getOriginalFilename(), file.getInputStream(),"10.155.32.14");
|
|
|
+
|
|
|
+ //文件路径
|
|
|
+ String filePath = config.getPath() + file.getOriginalFilename();
|
|
|
+
|
|
|
+ System.out.println(new Date() + "开始执行脚本...");
|
|
|
+ Process p;
|
|
|
+ String cmdPath = gyeeConfig.getDiagnosePath();
|
|
|
|
|
|
|
|
|
+ try {
|
|
|
+// String[] cmd = {"/bin/sh", "-c", "python " + cmdPath + trainParam.getName() + ".py "+filePath};
|
|
|
+ String cmd = "cmd /c python " + cmdPath + name + ".py " + filePath;
|
|
|
+
|
|
|
+// System.out.println(cmd[0] + " " + cmd[1] + " " + cmd[2]);
|
|
|
+ p = Runtime.getRuntime().exec(cmd);
|
|
|
|
|
|
|
|
|
+ BufferedReader bri = new BufferedReader(new InputStreamReader(p.getInputStream()));
|
|
|
+ BufferedReader bre = new BufferedReader(new InputStreamReader(p.getErrorStream()));
|
|
|
+ String si = null, se = null;
|
|
|
+ while ((si = bri.readLine()) != null || (se = bre.readLine()) != null) {
|
|
|
+ if (si != null) {
|
|
|
+ System.out.println(si);
|
|
|
+ }
|
|
|
+ if (se != null) {
|
|
|
+ System.out.println(se);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ Thread.sleep(10000);
|
|
|
+ p.waitFor();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ isComplete = true;
|
|
|
+ }
|
|
|
+ System.out.println(new Date() + "脚本执行结束...");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // 控制台信息
|
|
|
+ public void produce(TrainInfo trainInfo) throws Exception {
|
|
|
+ // put 控制台信息到队列中
|
|
|
+ trainInfo.setComplete(isComplete);
|
|
|
+ console.put(trainInfo);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // 输出控制台信息
|
|
|
+ public List<TrainInfo> consume() throws Exception {
|
|
|
+ // take方法取出一条记录
|
|
|
+ List<TrainInfo> list = new ArrayList<>();
|
|
|
+ int sise = console.size();
|
|
|
+ for (int i = 0; i < sise; i++) {
|
|
|
+ list.add(console.take());
|
|
|
+ }
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* // 定义苹果消费者
|
|
|
+ class Consumer implements Runnable {
|
|
|
+ private String instance;
|
|
|
+ private TrainInfo basket;
|
|
|
+
|
|
|
+ public Consumer(String instance, TrainInfo basket) {
|
|
|
+ this.instance = instance;
|
|
|
+ this.basket = basket;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void run() {
|
|
|
+ try {
|
|
|
+ while (true) {
|
|
|
+ // 消费苹果
|
|
|
+ System.out.println("消费者准备消费苹果:" + instance);
|
|
|
+ System.out.println(basket.getLog());
|
|
|
+ System.out.println("!消费者消费苹果完毕:" + instance);
|
|
|
+
|
|
|
+ }
|
|
|
+ } catch (Exception ex) {
|
|
|
+ System.out.println("Consumer Interrupted");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+*/
|
|
|
}
|