From bf8669841677497e2ff2463c1b47f6185baadf2a Mon Sep 17 00:00:00 2001 From: xuxueli <931591021@qq.com> Date: Wed, 20 Dec 2017 20:41:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E4=BB=B6=E6=93=8D=E4=BD=9C=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../job/core/biz/impl/ExecutorBizImpl.java | 2 +- .../com/xxl/job/core/glue/GlueTypeEnum.java | 33 ++++++++-- .../core/handler/impl/ScriptJobHandler.java | 25 ++++---- .../xxl/job/core/log/XxlJobFileAppender.java | 64 +++++++++---------- .../com/xxl/job/core/thread/JobThread.java | 2 +- .../com/xxl/job/core/util/ScriptUtil.java | 14 ---- .../resources/xxl-job-executor.properties | 2 +- .../resources/xxl-job-executor.properties | 2 +- .../src/main/resources/application.properties | 2 +- 9 files changed, 73 insertions(+), 73 deletions(-) diff --git a/xxl-job-core/src/main/java/com/xxl/job/core/biz/impl/ExecutorBizImpl.java b/xxl-job-core/src/main/java/com/xxl/job/core/biz/impl/ExecutorBizImpl.java index 5fc3ea7e..13fa9440 100644 --- a/xxl-job-core/src/main/java/com/xxl/job/core/biz/impl/ExecutorBizImpl.java +++ b/xxl-job-core/src/main/java/com/xxl/job/core/biz/impl/ExecutorBizImpl.java @@ -59,7 +59,7 @@ public class ExecutorBizImpl implements ExecutorBiz { @Override public ReturnT log(long logDateTim, int logId, int fromLineNum) { - // log filename: yyyy-MM-dd/9999.log + // log filename: logPath/yyyy-MM-dd/9999.log String logFileName = XxlJobFileAppender.makeLogFileName(new Date(logDateTim), logId); LogResult logResult = XxlJobFileAppender.readLog(logFileName, fromLineNum); diff --git a/xxl-job-core/src/main/java/com/xxl/job/core/glue/GlueTypeEnum.java b/xxl-job-core/src/main/java/com/xxl/job/core/glue/GlueTypeEnum.java index 17ab97ae..c60ddf44 100644 --- a/xxl-job-core/src/main/java/com/xxl/job/core/glue/GlueTypeEnum.java +++ b/xxl-job-core/src/main/java/com/xxl/job/core/glue/GlueTypeEnum.java @@ -5,20 +5,40 @@ package com.xxl.job.core.glue; */ public enum GlueTypeEnum { - BEAN("BEAN模式"), - GLUE_GROOVY("GLUE模式(Java)"), - GLUE_SHELL("GLUE模式(Shell)"), - GLUE_PYTHON("GLUE模式(Python)"), - GLUE_NODEJS("GLUE模式(Nodejs)"); + BEAN("BEAN模式", false, null, null), + GLUE_GROOVY("GLUE模式(Java)", false, null, null), + GLUE_SHELL("GLUE模式(Shell)", true, "bash", ".sh"), + GLUE_PYTHON("GLUE模式(Python)", true, "python", ".py"), + GLUE_NODEJS("GLUE模式(Nodejs)", true, "node", ".js"); private String desc; - private GlueTypeEnum(String desc) { + private boolean isScript; + private String cmd; + private String suffix; + + private GlueTypeEnum(String desc, boolean isScript, String cmd, String suffix) { this.desc = desc; + this.isScript = isScript; + this.cmd = cmd; + this.suffix = suffix; } + public String getDesc() { return desc; } + public boolean isScript() { + return isScript; + } + + public String getCmd() { + return cmd; + } + + public String getSuffix() { + return suffix; + } + public static GlueTypeEnum match(String name){ for (GlueTypeEnum item: GlueTypeEnum.values()) { if (item.name().equals(name)) { @@ -27,4 +47,5 @@ public enum GlueTypeEnum { } return null; } + } diff --git a/xxl-job-core/src/main/java/com/xxl/job/core/handler/impl/ScriptJobHandler.java b/xxl-job-core/src/main/java/com/xxl/job/core/handler/impl/ScriptJobHandler.java index 70ebb7a8..2449440d 100644 --- a/xxl-job-core/src/main/java/com/xxl/job/core/handler/impl/ScriptJobHandler.java +++ b/xxl-job-core/src/main/java/com/xxl/job/core/handler/impl/ScriptJobHandler.java @@ -31,25 +31,24 @@ public class ScriptJobHandler extends IJobHandler { @Override public ReturnT execute(String... params) throws Exception { - // cmd + script-file-name - String cmd = "bash"; - String scriptFileName = null; - if (GlueTypeEnum.GLUE_SHELL == glueType) { - cmd = "bash"; - scriptFileName = XxlJobFileAppender.getLogPath().concat("gluesource/").concat(String.valueOf(jobId)).concat("_").concat(String.valueOf(glueUpdatetime)).concat(".sh"); - } else if (GlueTypeEnum.GLUE_PYTHON == glueType) { - cmd = "python"; - scriptFileName = XxlJobFileAppender.getLogPath().concat("gluesource/").concat(String.valueOf(jobId)).concat("_").concat(String.valueOf(glueUpdatetime)).concat(".py"); - } else if (GlueTypeEnum.GLUE_NODEJS == glueType) { - cmd = "node"; - scriptFileName = XxlJobFileAppender.getLogPath().concat("gluesource/").concat(String.valueOf(jobId)).concat("_").concat(String.valueOf(glueUpdatetime)).concat(".js"); + if (!glueType.isScript()) { + return new ReturnT(IJobHandler.FAIL.getCode(), "glueType["+ glueType +"] invalid."); } + // cmd + String cmd = glueType.getCmd(); + // make script file + String scriptFileName = XxlJobFileAppender.getLogPath() + .concat("/gluesource/") + .concat(String.valueOf(jobId)) + .concat("_") + .concat(String.valueOf(glueUpdatetime)) + .concat(glueType.getSuffix()); ScriptUtil.markScriptFile(scriptFileName, gluesource); // log file - String logFileName = XxlJobFileAppender.getLogPath().concat(XxlJobFileAppender.contextHolder.get()); + String logFileName = XxlJobFileAppender.contextHolder.get(); // invoke XxlJobLogger.log("----------- script file:"+ scriptFileName +" -----------"); diff --git a/xxl-job-core/src/main/java/com/xxl/job/core/log/XxlJobFileAppender.java b/xxl-job-core/src/main/java/com/xxl/job/core/log/XxlJobFileAppender.java index cd6ffacf..60166e6b 100644 --- a/xxl-job-core/src/main/java/com/xxl/job/core/log/XxlJobFileAppender.java +++ b/xxl-job-core/src/main/java/com/xxl/job/core/log/XxlJobFileAppender.java @@ -20,53 +20,47 @@ public class XxlJobFileAppender { public static final InheritableThreadLocal contextHolder = new InheritableThreadLocal(); - private static String logPath = "/data/applogs/xxl-job/jobhandler/"; + // log base path + private static String logBasePath = "/data/applogs/xxl-job/jobhandler"; public static void initLogPath(String logPath){ // init if (logPath!=null && logPath.trim().length()>0) { - /*if (!logPath.endsWith("/")) { - logPath = logPath.concat("/"); - }*/ - XxlJobFileAppender.logPath = logPath; + logBasePath = logPath; } - // mk dir - File logPathDir = new File(XxlJobFileAppender.logPath); + // mk base dir + File logPathDir = new File(logBasePath); if (!logPathDir.exists()) { logPathDir.mkdirs(); } - XxlJobFileAppender.logPath = logPathDir.getPath(); + logBasePath = logPathDir.getPath(); + + // mk glue dir + File glueBaseDir = new File(logPathDir, "gluesource"); + if (!glueBaseDir.exists()) { + glueBaseDir.mkdirs(); + } } public static String getLogPath() { - return logPath; + return logBasePath; } /** - * log filename: yyyy-MM-dd/9999.log + * log filename, like "logPath/yyyy-MM-dd/9999.log" * * @param triggerDate * @param logId * @return */ public static String makeLogFileName(Date triggerDate, int logId) { - - // filePath/ - File filePathDir = new File(logPath); - if (!filePathDir.exists()) { - filePathDir.mkdirs(); - } - - // filePath/yyyy-MM-dd/ + // filePath/yyyy-MM-dd/9999.log SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // avoid concurrent problem, can not be static - - String nowFormat = sdf.format(new Date()); - File filePathDateDir = new File(filePathDir, nowFormat); - if (!filePathDateDir.exists()) { - filePathDateDir.mkdirs(); - } - - // filePath/yyyy-MM-dd/9999.log - String logFileName = sdf.format(triggerDate).concat("/").concat(String.valueOf(logId)).concat(".log"); + String logFileName = getLogPath() + .concat("/") + .concat(sdf.format(triggerDate)) + .concat("/") + .concat(String.valueOf(logId)) + .concat(".log"); return logFileName; } @@ -78,17 +72,11 @@ public class XxlJobFileAppender { */ public static void appendLog(String logFileName, String appendLog) { - // log - if (appendLog == null) { - appendLog = ""; - } - appendLog += "\r\n"; - // log file if (logFileName==null || logFileName.trim().length()==0) { return; } - File logFile = new File(logPath, logFileName); + File logFile = new File(logFileName); if (!logFile.exists()) { try { @@ -98,6 +86,12 @@ public class XxlJobFileAppender { return; } } + + // log + if (appendLog == null) { + appendLog = ""; + } + appendLog += "\r\n"; // append file content try { @@ -133,7 +127,7 @@ public class XxlJobFileAppender { if (logFileName==null || logFileName.trim().length()==0) { return new LogResult(fromLineNum, 0, "readLog fail, logFile not found", true); } - File logFile = new File(logPath, logFileName); + File logFile = new File(logFileName); if (!logFile.exists()) { return new LogResult(fromLineNum, 0, "readLog fail, logFile not exists", true); diff --git a/xxl-job-core/src/main/java/com/xxl/job/core/thread/JobThread.java b/xxl-job-core/src/main/java/com/xxl/job/core/thread/JobThread.java index a777f647..cf1e985a 100644 --- a/xxl-job-core/src/main/java/com/xxl/job/core/thread/JobThread.java +++ b/xxl-job-core/src/main/java/com/xxl/job/core/thread/JobThread.java @@ -111,7 +111,7 @@ public class JobThread extends Thread{ ? (String[])(Arrays.asList(triggerParam.getExecutorParams().split(",")).toArray()) : null; - // log filename: yyyy-MM-dd/9999.log + // log filename, like "logPath/yyyy-MM-dd/9999.log" String logFileName = XxlJobFileAppender.makeLogFileName(new Date(triggerParam.getLogDateTim()), triggerParam.getLogId()); XxlJobFileAppender.contextHolder.set(logFileName); ShardingUtil.setShardingVo(new ShardingUtil.ShardingVO(triggerParam.getBroadcastIndex(), triggerParam.getBroadcastTotal())); diff --git a/xxl-job-core/src/main/java/com/xxl/job/core/util/ScriptUtil.java b/xxl-job-core/src/main/java/com/xxl/job/core/util/ScriptUtil.java index dc335a63..fea47d44 100644 --- a/xxl-job-core/src/main/java/com/xxl/job/core/util/ScriptUtil.java +++ b/xxl-job-core/src/main/java/com/xxl/job/core/util/ScriptUtil.java @@ -1,11 +1,9 @@ package com.xxl.job.core.util; -import com.xxl.job.core.log.XxlJobFileAppender; import org.apache.commons.exec.CommandLine; import org.apache.commons.exec.DefaultExecutor; import org.apache.commons.exec.PumpStreamHandler; -import java.io.File; import java.io.FileOutputStream; import java.io.IOException; @@ -27,18 +25,6 @@ public class ScriptUtil { * @throws IOException */ public static void markScriptFile(String scriptFileName, String content) throws IOException { - // filePath/ - File filePathDir = new File(XxlJobFileAppender.getLogPath()); - if (!filePathDir.exists()) { - filePathDir.mkdirs(); - } - - // filePath/gluesource/ - File filePathSourceDir = new File(filePathDir, "gluesource"); - if (!filePathSourceDir.exists()) { - filePathSourceDir.mkdirs(); - } - // make file, filePath/gluesource/666-123456789.py FileOutputStream fileOutputStream = null; try { diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-jfinal/src/main/resources/xxl-job-executor.properties b/xxl-job-executor-samples/xxl-job-executor-sample-jfinal/src/main/resources/xxl-job-executor.properties index 7b363dc7..19be360e 100644 --- a/xxl-job-executor-samples/xxl-job-executor-sample-jfinal/src/main/resources/xxl-job-executor.properties +++ b/xxl-job-executor-samples/xxl-job-executor-sample-jfinal/src/main/resources/xxl-job-executor.properties @@ -7,7 +7,7 @@ xxl.job.executor.ip= xxl.job.executor.port=9997 ### xxl-job log path -xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler/ +xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler ### xxl-job, access token xxl.job.accessToken= \ No newline at end of file diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-spring/src/main/resources/xxl-job-executor.properties b/xxl-job-executor-samples/xxl-job-executor-sample-spring/src/main/resources/xxl-job-executor.properties index ab71b92b..d8b57728 100644 --- a/xxl-job-executor-samples/xxl-job-executor-sample-spring/src/main/resources/xxl-job-executor.properties +++ b/xxl-job-executor-samples/xxl-job-executor-sample-spring/src/main/resources/xxl-job-executor.properties @@ -7,7 +7,7 @@ xxl.job.executor.ip= xxl.job.executor.port=9999 ### xxl-job log path -xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler/ +xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler ### xxl-job, access token xxl.job.accessToken= \ No newline at end of file diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/resources/application.properties b/xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/resources/application.properties index a12408ac..6c441571 100644 --- a/xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/resources/application.properties +++ b/xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/resources/application.properties @@ -14,7 +14,7 @@ xxl.job.executor.ip= xxl.job.executor.port=-1 ### xxl-job log path -xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler/ +xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler ### xxl-job, access token xxl.job.accessToken=