|
@@ -1,49 +1,57 @@
|
1
|
1
|
package com.xxl.job.core.util;
|
2
|
2
|
|
|
3
|
+import com.xxl.job.core.log.XxlJobFileAppender;
|
3
|
4
|
import org.apache.commons.exec.CommandLine;
|
4
|
5
|
import org.apache.commons.exec.DefaultExecutor;
|
5
|
6
|
import org.apache.commons.exec.PumpStreamHandler;
|
6
|
7
|
|
7
|
8
|
import java.io.File;
|
8
|
9
|
import java.io.FileOutputStream;
|
|
10
|
+import java.io.IOException;
|
9
|
11
|
|
10
|
12
|
/**
|
11
|
13
|
* 1、内嵌编译器如"PythonInterpreter"无法引用扩展包,因此推荐使用java调用控制台进程方式"Runtime.getRuntime().exec()"来运行脚本(shell或python);
|
12
|
14
|
* 2、因为通过java调用控制台进程方式实现,需要保证目标机器PATH路径正确配置对应编译器;
|
13
|
15
|
* 3、暂时脚本执行日志只能在脚本执行结束后一次性获取,无法保证实时性;因此为确保日志实时性,可改为将脚本打印的日志存储在指定的日志文件上;
|
14
|
|
- *
|
15
|
|
- * 知识点:
|
16
|
|
- * 1、日志输出到日志文件:[>>logfile 2>&1]:将错误输出2以及标准输出1都一起以附加写方式导入logfile文件
|
17
|
|
- * 2、python 异常输出优先级高于标准输出,体现在Log文件中,因此推荐通过logging方式打日志保持和异常信息一致;否则用prinf日志顺序会错乱
|
|
16
|
+ * 4、python 异常输出优先级高于标准输出,体现在Log文件中,因此推荐通过logging方式打日志保持和异常信息一致;否则用prinf日志顺序会错乱
|
18
|
17
|
*
|
19
|
18
|
* Created by xuxueli on 17/2/25.
|
20
|
19
|
*/
|
21
|
20
|
public class ScriptUtil {
|
22
|
21
|
|
23
|
|
- private static String pyCmd = "python";
|
24
|
|
- private static String shllCmd = "bash";
|
25
|
|
- private static String pyFile = "/Users/xuxueli/workspaces/idea-git-workspace/github/xxl-incubator/xxl-util/src/main/resources/script/pytest.py";
|
26
|
|
- private static String shellFile = "/Users/xuxueli/workspaces/idea-git-workspace/github/xxl-incubator/xxl-util/src/main/resources/script/shelltest.sh";
|
27
|
|
- private static String pyLogFile = "/Users/xuxueli/Downloads/tmp/pylog.log";
|
28
|
|
- private static String shLogFile = "/Users/xuxueli/Downloads/tmp/shlog.log";
|
29
|
|
-
|
30
|
|
- public static void main(String[] args) {
|
31
|
|
-
|
32
|
|
- String command = pyCmd;
|
33
|
|
- String filename = pyFile;
|
34
|
|
- String logFile = pyLogFile;
|
35
|
|
- if (false) {
|
36
|
|
- command = shllCmd;
|
37
|
|
- filename = shellFile;
|
38
|
|
- logFile = shLogFile;
|
|
22
|
+ /**
|
|
23
|
+ * make script file
|
|
24
|
+ *
|
|
25
|
+ * @param scriptFileName
|
|
26
|
+ * @param content
|
|
27
|
+ * @throws IOException
|
|
28
|
+ */
|
|
29
|
+ public static void markScriptFile(String scriptFileName, String content) throws IOException {
|
|
30
|
+ // filePath/
|
|
31
|
+ File filePathDir = new File(XxlJobFileAppender.filePath);
|
|
32
|
+ if (!filePathDir.exists()) {
|
|
33
|
+ filePathDir.mkdirs();
|
39
|
34
|
}
|
40
|
35
|
|
41
|
|
- execToFile(command, filename, logFile);
|
42
|
|
-
|
43
|
|
- }
|
|
36
|
+ // filePath/gluesource/
|
|
37
|
+ File filePathSourceDir = new File(filePathDir, "gluesource");
|
|
38
|
+ if (!filePathSourceDir.exists()) {
|
|
39
|
+ filePathSourceDir.mkdirs();
|
|
40
|
+ }
|
44
|
41
|
|
45
|
|
- public static File markScriptFile(){
|
46
|
|
- return null;
|
|
42
|
+ // make file, filePath/gluesource/666-123456789.py
|
|
43
|
+ FileOutputStream fileOutputStream = null;
|
|
44
|
+ try {
|
|
45
|
+ fileOutputStream = new FileOutputStream(scriptFileName);
|
|
46
|
+ fileOutputStream.write(content.getBytes("UTF-8"));
|
|
47
|
+ fileOutputStream.close();
|
|
48
|
+ } catch (Exception e) {
|
|
49
|
+ throw e;
|
|
50
|
+ }finally{
|
|
51
|
+ if(fileOutputStream != null){
|
|
52
|
+ fileOutputStream.close();
|
|
53
|
+ }
|
|
54
|
+ }
|
47
|
55
|
}
|
48
|
56
|
|
49
|
57
|
/**
|
|
@@ -58,29 +66,22 @@ public class ScriptUtil {
|
58
|
66
|
* @param scriptFile
|
59
|
67
|
* @param logFile
|
60
|
68
|
*/
|
61
|
|
- public static void execToFile(String command, String scriptFile, String logFile){
|
62
|
|
- try {
|
63
|
|
- // 标准输出:print (null if watchdog timeout)
|
64
|
|
- // 错误输出:logging + 异常 (still exists if watchdog timeout)
|
65
|
|
- // 标准输出
|
66
|
|
- FileOutputStream fileOutputStream = new FileOutputStream(logFile);
|
67
|
|
- PumpStreamHandler streamHandler = new PumpStreamHandler(fileOutputStream, fileOutputStream, null);
|
|
69
|
+ public static void execToFile(String command, String scriptFile, String logFile) throws IOException {
|
|
70
|
+ // 标准输出:print (null if watchdog timeout)
|
|
71
|
+ // 错误输出:logging + 异常 (still exists if watchdog timeout)
|
|
72
|
+ // 标准输入
|
|
73
|
+ FileOutputStream fileOutputStream = new FileOutputStream(logFile, true);
|
|
74
|
+ PumpStreamHandler streamHandler = new PumpStreamHandler(fileOutputStream, fileOutputStream, null);
|
68
|
75
|
|
69
|
|
- // command
|
70
|
|
- CommandLine commandline = new CommandLine(command);
|
71
|
|
- commandline.addArgument(scriptFile);
|
|
76
|
+ // command
|
|
77
|
+ CommandLine commandline = new CommandLine(command);
|
|
78
|
+ commandline.addArgument(scriptFile);
|
72
|
79
|
|
73
|
|
- // exec
|
74
|
|
- DefaultExecutor exec = new DefaultExecutor();
|
75
|
|
- exec.setExitValues(null);
|
76
|
|
- exec.setStreamHandler(streamHandler);
|
77
|
|
- int exitValue = exec.execute(commandline);
|
78
|
|
- } catch (Exception e) {
|
79
|
|
- e.printStackTrace();
|
80
|
|
- }
|
81
|
|
- /*Process process = Runtime.getRuntime().exec(cmdarray);
|
82
|
|
- IOUtils.copy(process.getInputStream(), out);
|
83
|
|
- IOUtils.copy(process.getErrorStream(), out);*/
|
|
80
|
+ // exec
|
|
81
|
+ DefaultExecutor exec = new DefaultExecutor();
|
|
82
|
+ exec.setExitValues(null);
|
|
83
|
+ exec.setStreamHandler(streamHandler);
|
|
84
|
+ int exitValue = exec.execute(commandline);
|
84
|
85
|
}
|
85
|
86
|
|
86
|
87
|
}
|