Browse Source

文件操作优化

xuxueli 8 years ago
parent
commit
bf86698416

+ 1 - 1
xxl-job-core/src/main/java/com/xxl/job/core/biz/impl/ExecutorBizImpl.java View File

59
 
59
 
60
     @Override
60
     @Override
61
     public ReturnT<LogResult> log(long logDateTim, int logId, int fromLineNum) {
61
     public ReturnT<LogResult> log(long logDateTim, int logId, int fromLineNum) {
62
-        // log filename: yyyy-MM-dd/9999.log
62
+        // log filename: logPath/yyyy-MM-dd/9999.log
63
         String logFileName = XxlJobFileAppender.makeLogFileName(new Date(logDateTim), logId);
63
         String logFileName = XxlJobFileAppender.makeLogFileName(new Date(logDateTim), logId);
64
 
64
 
65
         LogResult logResult = XxlJobFileAppender.readLog(logFileName, fromLineNum);
65
         LogResult logResult = XxlJobFileAppender.readLog(logFileName, fromLineNum);

+ 27 - 6
xxl-job-core/src/main/java/com/xxl/job/core/glue/GlueTypeEnum.java View File

5
  */
5
  */
6
 public enum GlueTypeEnum {
6
 public enum GlueTypeEnum {
7
 
7
 
8
-    BEAN("BEAN模式"),
9
-    GLUE_GROOVY("GLUE模式(Java)"),
10
-    GLUE_SHELL("GLUE模式(Shell)"),
11
-    GLUE_PYTHON("GLUE模式(Python)"),
12
-    GLUE_NODEJS("GLUE模式(Nodejs)");
8
+    BEAN("BEAN模式", false, null, null),
9
+    GLUE_GROOVY("GLUE模式(Java)", false, null, null),
10
+    GLUE_SHELL("GLUE模式(Shell)", true, "bash", ".sh"),
11
+    GLUE_PYTHON("GLUE模式(Python)", true, "python", ".py"),
12
+    GLUE_NODEJS("GLUE模式(Nodejs)", true, "node", ".js");
13
 
13
 
14
     private String desc;
14
     private String desc;
15
-    private GlueTypeEnum(String desc) {
15
+    private boolean isScript;
16
+    private String cmd;
17
+    private String suffix;
18
+
19
+    private GlueTypeEnum(String desc, boolean isScript, String cmd, String suffix) {
16
         this.desc = desc;
20
         this.desc = desc;
21
+        this.isScript = isScript;
22
+        this.cmd = cmd;
23
+        this.suffix = suffix;
17
     }
24
     }
25
+
18
     public String getDesc() {
26
     public String getDesc() {
19
         return desc;
27
         return desc;
20
     }
28
     }
21
 
29
 
30
+    public boolean isScript() {
31
+        return isScript;
32
+    }
33
+
34
+    public String getCmd() {
35
+        return cmd;
36
+    }
37
+
38
+    public String getSuffix() {
39
+        return suffix;
40
+    }
41
+
22
     public static GlueTypeEnum match(String name){
42
     public static GlueTypeEnum match(String name){
23
         for (GlueTypeEnum item: GlueTypeEnum.values()) {
43
         for (GlueTypeEnum item: GlueTypeEnum.values()) {
24
             if (item.name().equals(name)) {
44
             if (item.name().equals(name)) {
27
         }
47
         }
28
         return null;
48
         return null;
29
     }
49
     }
50
+
30
 }
51
 }

+ 12 - 13
xxl-job-core/src/main/java/com/xxl/job/core/handler/impl/ScriptJobHandler.java View File

31
     @Override
31
     @Override
32
     public ReturnT<String> execute(String... params) throws Exception {
32
     public ReturnT<String> execute(String... params) throws Exception {
33
 
33
 
34
-        // cmd + script-file-name
35
-        String cmd = "bash";
36
-        String scriptFileName = null;
37
-        if (GlueTypeEnum.GLUE_SHELL == glueType) {
38
-            cmd = "bash";
39
-            scriptFileName = XxlJobFileAppender.getLogPath().concat("gluesource/").concat(String.valueOf(jobId)).concat("_").concat(String.valueOf(glueUpdatetime)).concat(".sh");
40
-        } else if (GlueTypeEnum.GLUE_PYTHON == glueType) {
41
-            cmd = "python";
42
-            scriptFileName = XxlJobFileAppender.getLogPath().concat("gluesource/").concat(String.valueOf(jobId)).concat("_").concat(String.valueOf(glueUpdatetime)).concat(".py");
43
-        } else if (GlueTypeEnum.GLUE_NODEJS == glueType) {
44
-            cmd = "node";
45
-            scriptFileName = XxlJobFileAppender.getLogPath().concat("gluesource/").concat(String.valueOf(jobId)).concat("_").concat(String.valueOf(glueUpdatetime)).concat(".js");
34
+        if (!glueType.isScript()) {
35
+            return new ReturnT<String>(IJobHandler.FAIL.getCode(), "glueType["+ glueType +"] invalid.");
46
         }
36
         }
47
 
37
 
38
+        // cmd
39
+        String cmd = glueType.getCmd();
40
+
48
         // make script file
41
         // make script file
42
+        String scriptFileName = XxlJobFileAppender.getLogPath()
43
+                .concat("/gluesource/")
44
+                .concat(String.valueOf(jobId))
45
+                .concat("_")
46
+                .concat(String.valueOf(glueUpdatetime))
47
+                .concat(glueType.getSuffix());
49
         ScriptUtil.markScriptFile(scriptFileName, gluesource);
48
         ScriptUtil.markScriptFile(scriptFileName, gluesource);
50
 
49
 
51
         // log file
50
         // log file
52
-        String logFileName = XxlJobFileAppender.getLogPath().concat(XxlJobFileAppender.contextHolder.get());
51
+        String logFileName = XxlJobFileAppender.contextHolder.get();
53
 
52
 
54
         // invoke
53
         // invoke
55
         XxlJobLogger.log("----------- script file:"+ scriptFileName +" -----------");
54
         XxlJobLogger.log("----------- script file:"+ scriptFileName +" -----------");

+ 29 - 35
xxl-job-core/src/main/java/com/xxl/job/core/log/XxlJobFileAppender.java View File

20
 	public static final InheritableThreadLocal<String> contextHolder = new InheritableThreadLocal<String>();
20
 	public static final InheritableThreadLocal<String> contextHolder = new InheritableThreadLocal<String>();
21
 
21
 
22
 
22
 
23
-	private static String logPath = "/data/applogs/xxl-job/jobhandler/";
23
+	// log base path
24
+	private static String logBasePath = "/data/applogs/xxl-job/jobhandler";
24
 	public static void initLogPath(String logPath){
25
 	public static void initLogPath(String logPath){
25
 		// init
26
 		// init
26
 		if (logPath!=null && logPath.trim().length()>0) {
27
 		if (logPath!=null && logPath.trim().length()>0) {
27
-			/*if (!logPath.endsWith("/")) {
28
-				logPath = logPath.concat("/");
29
-			}*/
30
-			XxlJobFileAppender.logPath = logPath;
28
+			logBasePath = logPath;
31
 		}
29
 		}
32
-		// mk dir
33
-		File logPathDir = new File(XxlJobFileAppender.logPath);
30
+		// mk base dir
31
+		File logPathDir = new File(logBasePath);
34
 		if (!logPathDir.exists()) {
32
 		if (!logPathDir.exists()) {
35
 			logPathDir.mkdirs();
33
 			logPathDir.mkdirs();
36
 		}
34
 		}
37
-		XxlJobFileAppender.logPath = logPathDir.getPath();
35
+		logBasePath = logPathDir.getPath();
36
+
37
+		// mk glue dir
38
+		File glueBaseDir = new File(logPathDir, "gluesource");
39
+		if (!glueBaseDir.exists()) {
40
+			glueBaseDir.mkdirs();
41
+		}
38
 	}
42
 	}
39
 	public static String getLogPath() {
43
 	public static String getLogPath() {
40
-		return logPath;
44
+		return logBasePath;
41
 	}
45
 	}
42
 
46
 
43
 
47
 
44
 	/**
48
 	/**
45
-	 * log filename: yyyy-MM-dd/9999.log
49
+	 * log filename, like "logPath/yyyy-MM-dd/9999.log"
46
 	 *
50
 	 *
47
 	 * @param triggerDate
51
 	 * @param triggerDate
48
 	 * @param logId
52
 	 * @param logId
49
 	 * @return
53
 	 * @return
50
 	 */
54
 	 */
51
 	public static String makeLogFileName(Date triggerDate, int logId) {
55
 	public static String makeLogFileName(Date triggerDate, int logId) {
52
-
53
-        // filePath/
54
-        File filePathDir = new File(logPath);
55
-        if (!filePathDir.exists()) {
56
-            filePathDir.mkdirs();
57
-        }
58
-
59
-        // filePath/yyyy-MM-dd/
56
+		// filePath/yyyy-MM-dd/9999.log
60
 		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");	// avoid concurrent problem, can not be static
57
 		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");	// avoid concurrent problem, can not be static
61
-
62
-        String nowFormat = sdf.format(new Date());
63
-        File filePathDateDir = new File(filePathDir, nowFormat);
64
-        if (!filePathDateDir.exists()) {
65
-            filePathDateDir.mkdirs();
66
-        }
67
-
68
-        // filePath/yyyy-MM-dd/9999.log
69
-		String logFileName = sdf.format(triggerDate).concat("/").concat(String.valueOf(logId)).concat(".log");
58
+		String logFileName = getLogPath()
59
+				.concat("/")
60
+				.concat(sdf.format(triggerDate))
61
+				.concat("/")
62
+				.concat(String.valueOf(logId))
63
+				.concat(".log");
70
 		return logFileName;
64
 		return logFileName;
71
 	}
65
 	}
72
 
66
 
78
 	 */
72
 	 */
79
 	public static void appendLog(String logFileName, String appendLog) {
73
 	public static void appendLog(String logFileName, String appendLog) {
80
 
74
 
81
-		// log
82
-		if (appendLog == null) {
83
-			appendLog = "";
84
-		}
85
-		appendLog += "\r\n";
86
-
87
 		// log file
75
 		// log file
88
 		if (logFileName==null || logFileName.trim().length()==0) {
76
 		if (logFileName==null || logFileName.trim().length()==0) {
89
 			return;
77
 			return;
90
 		}
78
 		}
91
-		File logFile = new File(logPath, logFileName);
79
+		File logFile = new File(logFileName);
92
 
80
 
93
 		if (!logFile.exists()) {
81
 		if (!logFile.exists()) {
94
 			try {
82
 			try {
98
 				return;
86
 				return;
99
 			}
87
 			}
100
 		}
88
 		}
89
+
90
+		// log
91
+		if (appendLog == null) {
92
+			appendLog = "";
93
+		}
94
+		appendLog += "\r\n";
101
 		
95
 		
102
 		// append file content
96
 		// append file content
103
 		try {
97
 		try {
133
 		if (logFileName==null || logFileName.trim().length()==0) {
127
 		if (logFileName==null || logFileName.trim().length()==0) {
134
             return new LogResult(fromLineNum, 0, "readLog fail, logFile not found", true);
128
             return new LogResult(fromLineNum, 0, "readLog fail, logFile not found", true);
135
 		}
129
 		}
136
-		File logFile = new File(logPath, logFileName);
130
+		File logFile = new File(logFileName);
137
 
131
 
138
 		if (!logFile.exists()) {
132
 		if (!logFile.exists()) {
139
             return new LogResult(fromLineNum, 0, "readLog fail, logFile not exists", true);
133
             return new LogResult(fromLineNum, 0, "readLog fail, logFile not exists", true);

+ 1 - 1
xxl-job-core/src/main/java/com/xxl/job/core/thread/JobThread.java View File

111
 							? (String[])(Arrays.asList(triggerParam.getExecutorParams().split(",")).toArray()) : null;
111
 							? (String[])(Arrays.asList(triggerParam.getExecutorParams().split(",")).toArray()) : null;
112
 
112
 
113
 
113
 
114
-					// log filename: yyyy-MM-dd/9999.log
114
+					// log filename, like "logPath/yyyy-MM-dd/9999.log"
115
 					String logFileName = XxlJobFileAppender.makeLogFileName(new Date(triggerParam.getLogDateTim()), triggerParam.getLogId());
115
 					String logFileName = XxlJobFileAppender.makeLogFileName(new Date(triggerParam.getLogDateTim()), triggerParam.getLogId());
116
 					XxlJobFileAppender.contextHolder.set(logFileName);
116
 					XxlJobFileAppender.contextHolder.set(logFileName);
117
 					ShardingUtil.setShardingVo(new ShardingUtil.ShardingVO(triggerParam.getBroadcastIndex(), triggerParam.getBroadcastTotal()));
117
 					ShardingUtil.setShardingVo(new ShardingUtil.ShardingVO(triggerParam.getBroadcastIndex(), triggerParam.getBroadcastTotal()));

+ 0 - 14
xxl-job-core/src/main/java/com/xxl/job/core/util/ScriptUtil.java View File

1
 package com.xxl.job.core.util;
1
 package com.xxl.job.core.util;
2
 
2
 
3
-import com.xxl.job.core.log.XxlJobFileAppender;
4
 import org.apache.commons.exec.CommandLine;
3
 import org.apache.commons.exec.CommandLine;
5
 import org.apache.commons.exec.DefaultExecutor;
4
 import org.apache.commons.exec.DefaultExecutor;
6
 import org.apache.commons.exec.PumpStreamHandler;
5
 import org.apache.commons.exec.PumpStreamHandler;
7
 
6
 
8
-import java.io.File;
9
 import java.io.FileOutputStream;
7
 import java.io.FileOutputStream;
10
 import java.io.IOException;
8
 import java.io.IOException;
11
 
9
 
27
      * @throws IOException
25
      * @throws IOException
28
      */
26
      */
29
     public static void markScriptFile(String scriptFileName, String content) throws IOException {
27
     public static void markScriptFile(String scriptFileName, String content) throws IOException {
30
-        // filePath/
31
-        File filePathDir = new File(XxlJobFileAppender.getLogPath());
32
-        if (!filePathDir.exists()) {
33
-            filePathDir.mkdirs();
34
-        }
35
-
36
-        // filePath/gluesource/
37
-        File filePathSourceDir = new File(filePathDir, "gluesource");
38
-        if (!filePathSourceDir.exists()) {
39
-            filePathSourceDir.mkdirs();
40
-        }
41
-
42
         // make file,   filePath/gluesource/666-123456789.py
28
         // make file,   filePath/gluesource/666-123456789.py
43
         FileOutputStream fileOutputStream = null;
29
         FileOutputStream fileOutputStream = null;
44
         try {
30
         try {

+ 1 - 1
xxl-job-executor-samples/xxl-job-executor-sample-jfinal/src/main/resources/xxl-job-executor.properties View File

7
 xxl.job.executor.port=9997
7
 xxl.job.executor.port=9997
8
 
8
 
9
 ### xxl-job log path
9
 ### xxl-job log path
10
-xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler/
10
+xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler
11
 
11
 
12
 ### xxl-job, access token
12
 ### xxl-job, access token
13
 xxl.job.accessToken=
13
 xxl.job.accessToken=

+ 1 - 1
xxl-job-executor-samples/xxl-job-executor-sample-spring/src/main/resources/xxl-job-executor.properties View File

7
 xxl.job.executor.port=9999
7
 xxl.job.executor.port=9999
8
 
8
 
9
 ### xxl-job log path
9
 ### xxl-job log path
10
-xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler/
10
+xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler
11
 
11
 
12
 ### xxl-job, access token
12
 ### xxl-job, access token
13
 xxl.job.accessToken=
13
 xxl.job.accessToken=

+ 1 - 1
xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/resources/application.properties View File

14
 xxl.job.executor.port=-1
14
 xxl.job.executor.port=-1
15
 
15
 
16
 ### xxl-job log path
16
 ### xxl-job log path
17
-xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler/
17
+xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler
18
 
18
 
19
 ### xxl-job, access token
19
 ### xxl-job, access token
20
 xxl.job.accessToken=
20
 xxl.job.accessToken=