|
@@ -9,15 +9,12 @@ import com.xxl.job.core.glue.GlueFactory;
|
|
|
import com.xxl.job.core.glue.GlueTypeEnum;
|
|
import com.xxl.job.core.glue.GlueTypeEnum;
|
|
|
import com.xxl.job.core.handler.IJobHandler;
|
|
import com.xxl.job.core.handler.IJobHandler;
|
|
|
import com.xxl.job.core.handler.impl.GlueJobHandler;
|
|
import com.xxl.job.core.handler.impl.GlueJobHandler;
|
|
|
|
|
+import com.xxl.job.core.handler.impl.ScriptJobHandler;
|
|
|
import com.xxl.job.core.log.XxlJobFileAppender;
|
|
import com.xxl.job.core.log.XxlJobFileAppender;
|
|
|
import com.xxl.job.core.thread.JobThread;
|
|
import com.xxl.job.core.thread.JobThread;
|
|
|
-import com.xxl.job.core.util.ScriptUtil;
|
|
|
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
-import java.io.File;
|
|
|
|
|
-import java.io.FileOutputStream;
|
|
|
|
|
-import java.io.IOException;
|
|
|
|
|
import java.util.Date;
|
|
import java.util.Date;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -85,11 +82,11 @@ public class ExecutorBizImpl implements ExecutorBiz {
|
|
|
|
|
|
|
|
} else if (GlueTypeEnum.GLUE_GROOVY==GlueTypeEnum.match(triggerParam.getGlueType())) {
|
|
} else if (GlueTypeEnum.GLUE_GROOVY==GlueTypeEnum.match(triggerParam.getGlueType())) {
|
|
|
|
|
|
|
|
- // valid exists job thread:change handler or glue timeout, need kill old thread
|
|
|
|
|
|
|
+ // valid exists job thread:change handler or gluesource updated, need kill old thread
|
|
|
if (jobThread != null &&
|
|
if (jobThread != null &&
|
|
|
!(jobThread.getHandler() instanceof GlueJobHandler
|
|
!(jobThread.getHandler() instanceof GlueJobHandler
|
|
|
&& ((GlueJobHandler) jobThread.getHandler()).getGlueUpdatetime()==triggerParam.getGlueUpdatetime() )) {
|
|
&& ((GlueJobHandler) jobThread.getHandler()).getGlueUpdatetime()==triggerParam.getGlueUpdatetime() )) {
|
|
|
- // change glue model or glue timeout, kill old job thread
|
|
|
|
|
|
|
+ // change glue model or gluesource updated, kill old job thread
|
|
|
jobThread.toStop("更换任务模式或JobHandler,终止旧任务线程");
|
|
jobThread.toStop("更换任务模式或JobHandler,终止旧任务线程");
|
|
|
jobThread.interrupt();
|
|
jobThread.interrupt();
|
|
|
XxlJobExecutor.removeJobThread(triggerParam.getJobId());
|
|
XxlJobExecutor.removeJobThread(triggerParam.getJobId());
|
|
@@ -107,61 +104,25 @@ public class ExecutorBizImpl implements ExecutorBiz {
|
|
|
}
|
|
}
|
|
|
jobThread = XxlJobExecutor.registJobThread(triggerParam.getJobId(), new GlueJobHandler(jobHandler, triggerParam.getGlueUpdatetime()));
|
|
jobThread = XxlJobExecutor.registJobThread(triggerParam.getJobId(), new GlueJobHandler(jobHandler, triggerParam.getGlueUpdatetime()));
|
|
|
}
|
|
}
|
|
|
- } else if (GlueTypeEnum.GLUE_SHELL==GlueTypeEnum.match(triggerParam.getGlueType())) {
|
|
|
|
|
-
|
|
|
|
|
- // make path
|
|
|
|
|
- String scriptPath = XxlJobFileAppender.filePath + "gluesource/";
|
|
|
|
|
- String scriptFileName = triggerParam.getJobId() + "_" + triggerParam.getGlueUpdatetime() + ".sh";
|
|
|
|
|
-
|
|
|
|
|
- // valid file
|
|
|
|
|
- File scriptFile = new File(scriptPath, scriptFileName);
|
|
|
|
|
- if (!scriptFile.exists()) {
|
|
|
|
|
- // valid glue source
|
|
|
|
|
- if (triggerParam.getGlueSource()==null) {
|
|
|
|
|
- return new ReturnT<String>(ReturnT.FAIL_CODE, "glueSource is null.");
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // .../gluesource/
|
|
|
|
|
- File scriptPathDir = new File(scriptPath);
|
|
|
|
|
- if (!scriptPathDir.exists()) {
|
|
|
|
|
- scriptPathDir.mkdirs();
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // .../gluesource/666-156465656.sh
|
|
|
|
|
- scriptFile = new File(scriptPath, scriptFileName);
|
|
|
|
|
- FileOutputStream fos = null;
|
|
|
|
|
- try {
|
|
|
|
|
- scriptFile.createNewFile();
|
|
|
|
|
-
|
|
|
|
|
- fos = new FileOutputStream(scriptFile, true);
|
|
|
|
|
- fos.write(triggerParam.getGlueSource().getBytes("utf-8"));
|
|
|
|
|
- fos.flush();
|
|
|
|
|
- } catch (IOException e) {
|
|
|
|
|
- logger.error(e.getMessage(), e);
|
|
|
|
|
- return new ReturnT<String>(ReturnT.FAIL_CODE, e.getMessage());
|
|
|
|
|
- } finally {
|
|
|
|
|
- if (fos != null) {
|
|
|
|
|
- try {
|
|
|
|
|
- fos.close();
|
|
|
|
|
- } catch (IOException e) {
|
|
|
|
|
- logger.error(e.getMessage(), e);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ } else if (GlueTypeEnum.GLUE_SHELL==GlueTypeEnum.match(triggerParam.getGlueType())
|
|
|
|
|
+ || GlueTypeEnum.GLUE_PYTHON==GlueTypeEnum.match(triggerParam.getGlueType()) ) {
|
|
|
|
|
|
|
|
|
|
+ // valid exists job thread:change script or gluesource updated, need kill old thread
|
|
|
|
|
+ if (jobThread != null &&
|
|
|
|
|
+ !(jobThread.getHandler() instanceof ScriptJobHandler
|
|
|
|
|
+ && ((ScriptJobHandler) jobThread.getHandler()).getGlueUpdatetime()==triggerParam.getGlueUpdatetime() )) {
|
|
|
|
|
+ // change glue model or gluesource updated, kill old job thread
|
|
|
|
|
+ jobThread.toStop("更换任务模式或JobHandler,终止旧任务线程");
|
|
|
|
|
+ jobThread.interrupt();
|
|
|
|
|
+ XxlJobExecutor.removeJobThread(triggerParam.getJobId());
|
|
|
|
|
+ jobThread = null;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // log File
|
|
|
|
|
- String logFileName = XxlJobFileAppender.makeLogFileName(new Date(triggerParam.getLogDateTim()), triggerParam.getLogId());
|
|
|
|
|
-
|
|
|
|
|
- // run script
|
|
|
|
|
- ScriptUtil.execToFile("python", scriptFile.getName(), (XxlJobFileAppender.filePath + logFileName) );
|
|
|
|
|
-
|
|
|
|
|
- return ReturnT.FAIL;
|
|
|
|
|
- } else if (GlueTypeEnum.GLUE_PYTHON==GlueTypeEnum.match(triggerParam.getGlueType())) {
|
|
|
|
|
- String scriptFilePath = XxlJobFileAppender.filePath + "gluesource/" + triggerParam.getJobId() + "_" + triggerParam.getGlueUpdatetime() + ".py";
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
|
|
+ // make thread: new or exists invalid
|
|
|
|
|
+ if (jobThread == null) {
|
|
|
|
|
+ ScriptJobHandler scriptJobHandler = new ScriptJobHandler(triggerParam.getJobId(), triggerParam.getGlueUpdatetime(), triggerParam.getGlueSource(), GlueTypeEnum.match(triggerParam.getGlueType()));
|
|
|
|
|
+ jobThread = XxlJobExecutor.registJobThread(triggerParam.getJobId(), scriptJobHandler);
|
|
|
|
|
+ }
|
|
|
} else {
|
|
} else {
|
|
|
return new ReturnT<String>(ReturnT.FAIL_CODE, "glueType[" + triggerParam.getGlueType() + "] is not valid.");
|
|
return new ReturnT<String>(ReturnT.FAIL_CODE, "glueType[" + triggerParam.getGlueType() + "] is not valid.");
|
|
|
}
|
|
}
|