文件操作优化
This commit is contained in:
parent
3c95ba5ae4
commit
bf86698416
|
@ -59,7 +59,7 @@ public class ExecutorBizImpl implements ExecutorBiz {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ReturnT<LogResult> log(long logDateTim, int logId, int fromLineNum) {
|
public ReturnT<LogResult> 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);
|
String logFileName = XxlJobFileAppender.makeLogFileName(new Date(logDateTim), logId);
|
||||||
|
|
||||||
LogResult logResult = XxlJobFileAppender.readLog(logFileName, fromLineNum);
|
LogResult logResult = XxlJobFileAppender.readLog(logFileName, fromLineNum);
|
||||||
|
|
|
@ -5,20 +5,40 @@ package com.xxl.job.core.glue;
|
||||||
*/
|
*/
|
||||||
public enum GlueTypeEnum {
|
public enum GlueTypeEnum {
|
||||||
|
|
||||||
BEAN("BEAN模式"),
|
BEAN("BEAN模式", false, null, null),
|
||||||
GLUE_GROOVY("GLUE模式(Java)"),
|
GLUE_GROOVY("GLUE模式(Java)", false, null, null),
|
||||||
GLUE_SHELL("GLUE模式(Shell)"),
|
GLUE_SHELL("GLUE模式(Shell)", true, "bash", ".sh"),
|
||||||
GLUE_PYTHON("GLUE模式(Python)"),
|
GLUE_PYTHON("GLUE模式(Python)", true, "python", ".py"),
|
||||||
GLUE_NODEJS("GLUE模式(Nodejs)");
|
GLUE_NODEJS("GLUE模式(Nodejs)", true, "node", ".js");
|
||||||
|
|
||||||
private String desc;
|
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.desc = desc;
|
||||||
|
this.isScript = isScript;
|
||||||
|
this.cmd = cmd;
|
||||||
|
this.suffix = suffix;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getDesc() {
|
public String getDesc() {
|
||||||
return desc;
|
return desc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isScript() {
|
||||||
|
return isScript;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCmd() {
|
||||||
|
return cmd;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getSuffix() {
|
||||||
|
return suffix;
|
||||||
|
}
|
||||||
|
|
||||||
public static GlueTypeEnum match(String name){
|
public static GlueTypeEnum match(String name){
|
||||||
for (GlueTypeEnum item: GlueTypeEnum.values()) {
|
for (GlueTypeEnum item: GlueTypeEnum.values()) {
|
||||||
if (item.name().equals(name)) {
|
if (item.name().equals(name)) {
|
||||||
|
@ -27,4 +47,5 @@ public enum GlueTypeEnum {
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,25 +31,24 @@ public class ScriptJobHandler extends IJobHandler {
|
||||||
@Override
|
@Override
|
||||||
public ReturnT<String> execute(String... params) throws Exception {
|
public ReturnT<String> execute(String... params) throws Exception {
|
||||||
|
|
||||||
// cmd + script-file-name
|
if (!glueType.isScript()) {
|
||||||
String cmd = "bash";
|
return new ReturnT<String>(IJobHandler.FAIL.getCode(), "glueType["+ glueType +"] invalid.");
|
||||||
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");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// cmd
|
||||||
|
String cmd = glueType.getCmd();
|
||||||
|
|
||||||
// make script file
|
// 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);
|
ScriptUtil.markScriptFile(scriptFileName, gluesource);
|
||||||
|
|
||||||
// log file
|
// log file
|
||||||
String logFileName = XxlJobFileAppender.getLogPath().concat(XxlJobFileAppender.contextHolder.get());
|
String logFileName = XxlJobFileAppender.contextHolder.get();
|
||||||
|
|
||||||
// invoke
|
// invoke
|
||||||
XxlJobLogger.log("----------- script file:"+ scriptFileName +" -----------");
|
XxlJobLogger.log("----------- script file:"+ scriptFileName +" -----------");
|
||||||
|
|
|
@ -20,53 +20,47 @@ public class XxlJobFileAppender {
|
||||||
public static final InheritableThreadLocal<String> contextHolder = new InheritableThreadLocal<String>();
|
public static final InheritableThreadLocal<String> contextHolder = new InheritableThreadLocal<String>();
|
||||||
|
|
||||||
|
|
||||||
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){
|
public static void initLogPath(String logPath){
|
||||||
// init
|
// init
|
||||||
if (logPath!=null && logPath.trim().length()>0) {
|
if (logPath!=null && logPath.trim().length()>0) {
|
||||||
/*if (!logPath.endsWith("/")) {
|
logBasePath = logPath;
|
||||||
logPath = logPath.concat("/");
|
|
||||||
}*/
|
|
||||||
XxlJobFileAppender.logPath = logPath;
|
|
||||||
}
|
}
|
||||||
// mk dir
|
// mk base dir
|
||||||
File logPathDir = new File(XxlJobFileAppender.logPath);
|
File logPathDir = new File(logBasePath);
|
||||||
if (!logPathDir.exists()) {
|
if (!logPathDir.exists()) {
|
||||||
logPathDir.mkdirs();
|
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() {
|
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 triggerDate
|
||||||
* @param logId
|
* @param logId
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static String makeLogFileName(Date triggerDate, int logId) {
|
public static String makeLogFileName(Date triggerDate, int logId) {
|
||||||
|
// filePath/yyyy-MM-dd/9999.log
|
||||||
// filePath/
|
|
||||||
File filePathDir = new File(logPath);
|
|
||||||
if (!filePathDir.exists()) {
|
|
||||||
filePathDir.mkdirs();
|
|
||||||
}
|
|
||||||
|
|
||||||
// filePath/yyyy-MM-dd/
|
|
||||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // avoid concurrent problem, can not be static
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // avoid concurrent problem, can not be static
|
||||||
|
String logFileName = getLogPath()
|
||||||
String nowFormat = sdf.format(new Date());
|
.concat("/")
|
||||||
File filePathDateDir = new File(filePathDir, nowFormat);
|
.concat(sdf.format(triggerDate))
|
||||||
if (!filePathDateDir.exists()) {
|
.concat("/")
|
||||||
filePathDateDir.mkdirs();
|
.concat(String.valueOf(logId))
|
||||||
}
|
.concat(".log");
|
||||||
|
|
||||||
// filePath/yyyy-MM-dd/9999.log
|
|
||||||
String logFileName = sdf.format(triggerDate).concat("/").concat(String.valueOf(logId)).concat(".log");
|
|
||||||
return logFileName;
|
return logFileName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,17 +72,11 @@ public class XxlJobFileAppender {
|
||||||
*/
|
*/
|
||||||
public static void appendLog(String logFileName, String appendLog) {
|
public static void appendLog(String logFileName, String appendLog) {
|
||||||
|
|
||||||
// log
|
|
||||||
if (appendLog == null) {
|
|
||||||
appendLog = "";
|
|
||||||
}
|
|
||||||
appendLog += "\r\n";
|
|
||||||
|
|
||||||
// log file
|
// log file
|
||||||
if (logFileName==null || logFileName.trim().length()==0) {
|
if (logFileName==null || logFileName.trim().length()==0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
File logFile = new File(logPath, logFileName);
|
File logFile = new File(logFileName);
|
||||||
|
|
||||||
if (!logFile.exists()) {
|
if (!logFile.exists()) {
|
||||||
try {
|
try {
|
||||||
|
@ -98,6 +86,12 @@ public class XxlJobFileAppender {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// log
|
||||||
|
if (appendLog == null) {
|
||||||
|
appendLog = "";
|
||||||
|
}
|
||||||
|
appendLog += "\r\n";
|
||||||
|
|
||||||
// append file content
|
// append file content
|
||||||
try {
|
try {
|
||||||
|
@ -133,7 +127,7 @@ public class XxlJobFileAppender {
|
||||||
if (logFileName==null || logFileName.trim().length()==0) {
|
if (logFileName==null || logFileName.trim().length()==0) {
|
||||||
return new LogResult(fromLineNum, 0, "readLog fail, logFile not found", true);
|
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()) {
|
if (!logFile.exists()) {
|
||||||
return new LogResult(fromLineNum, 0, "readLog fail, logFile not exists", true);
|
return new LogResult(fromLineNum, 0, "readLog fail, logFile not exists", true);
|
||||||
|
|
|
@ -111,7 +111,7 @@ public class JobThread extends Thread{
|
||||||
? (String[])(Arrays.asList(triggerParam.getExecutorParams().split(",")).toArray()) : null;
|
? (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());
|
String logFileName = XxlJobFileAppender.makeLogFileName(new Date(triggerParam.getLogDateTim()), triggerParam.getLogId());
|
||||||
XxlJobFileAppender.contextHolder.set(logFileName);
|
XxlJobFileAppender.contextHolder.set(logFileName);
|
||||||
ShardingUtil.setShardingVo(new ShardingUtil.ShardingVO(triggerParam.getBroadcastIndex(), triggerParam.getBroadcastTotal()));
|
ShardingUtil.setShardingVo(new ShardingUtil.ShardingVO(triggerParam.getBroadcastIndex(), triggerParam.getBroadcastTotal()));
|
||||||
|
|
|
@ -1,11 +1,9 @@
|
||||||
package com.xxl.job.core.util;
|
package com.xxl.job.core.util;
|
||||||
|
|
||||||
import com.xxl.job.core.log.XxlJobFileAppender;
|
|
||||||
import org.apache.commons.exec.CommandLine;
|
import org.apache.commons.exec.CommandLine;
|
||||||
import org.apache.commons.exec.DefaultExecutor;
|
import org.apache.commons.exec.DefaultExecutor;
|
||||||
import org.apache.commons.exec.PumpStreamHandler;
|
import org.apache.commons.exec.PumpStreamHandler;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
@ -27,18 +25,6 @@ public class ScriptUtil {
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public static void markScriptFile(String scriptFileName, String content) 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
|
// make file, filePath/gluesource/666-123456789.py
|
||||||
FileOutputStream fileOutputStream = null;
|
FileOutputStream fileOutputStream = null;
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -7,7 +7,7 @@ xxl.job.executor.ip=
|
||||||
xxl.job.executor.port=9997
|
xxl.job.executor.port=9997
|
||||||
|
|
||||||
### xxl-job log path
|
### 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, access token
|
||||||
xxl.job.accessToken=
|
xxl.job.accessToken=
|
|
@ -7,7 +7,7 @@ xxl.job.executor.ip=
|
||||||
xxl.job.executor.port=9999
|
xxl.job.executor.port=9999
|
||||||
|
|
||||||
### xxl-job log path
|
### 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, access token
|
||||||
xxl.job.accessToken=
|
xxl.job.accessToken=
|
|
@ -14,7 +14,7 @@ xxl.job.executor.ip=
|
||||||
xxl.job.executor.port=-1
|
xxl.job.executor.port=-1
|
||||||
|
|
||||||
### xxl-job log path
|
### 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, access token
|
||||||
xxl.job.accessToken=
|
xxl.job.accessToken=
|
||||||
|
|
Loading…
Reference in New Issue