|
@@ -18,6 +18,7 @@
|
|
package org.apache.dolphinscheduler.plugin.task.python;
|
|
package org.apache.dolphinscheduler.plugin.task.python;
|
|
|
|
|
|
import org.apache.dolphinscheduler.plugin.task.api.AbstractTaskExecutor;
|
|
import org.apache.dolphinscheduler.plugin.task.api.AbstractTaskExecutor;
|
|
|
|
+import org.apache.dolphinscheduler.plugin.task.api.ShellCommandExecutor;
|
|
import org.apache.dolphinscheduler.plugin.task.api.TaskException;
|
|
import org.apache.dolphinscheduler.plugin.task.api.TaskException;
|
|
import org.apache.dolphinscheduler.plugin.task.api.TaskResponse;
|
|
import org.apache.dolphinscheduler.plugin.task.api.TaskResponse;
|
|
import org.apache.dolphinscheduler.plugin.task.util.MapUtils;
|
|
import org.apache.dolphinscheduler.plugin.task.util.MapUtils;
|
|
@@ -29,6 +30,13 @@ import org.apache.dolphinscheduler.spi.task.paramparser.ParameterUtils;
|
|
import org.apache.dolphinscheduler.spi.task.request.TaskRequest;
|
|
import org.apache.dolphinscheduler.spi.task.request.TaskRequest;
|
|
import org.apache.dolphinscheduler.spi.utils.JSONUtils;
|
|
import org.apache.dolphinscheduler.spi.utils.JSONUtils;
|
|
|
|
|
|
|
|
+import org.apache.commons.io.FileUtils;
|
|
|
|
+
|
|
|
|
+import java.io.File;
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
|
+import java.nio.file.Files;
|
|
|
|
+import java.nio.file.Paths;
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
|
|
|
@@ -43,14 +51,9 @@ public class PythonTask extends AbstractTaskExecutor {
|
|
private PythonParameters pythonParameters;
|
|
private PythonParameters pythonParameters;
|
|
|
|
|
|
/**
|
|
/**
|
|
- * task dir
|
|
|
|
- */
|
|
|
|
- private String taskDir;
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * python command executor
|
|
|
|
|
|
+ * shell command executor
|
|
*/
|
|
*/
|
|
- private PythonCommandExecutor pythonCommandExecutor;
|
|
|
|
|
|
+ private ShellCommandExecutor shellCommandExecutor;
|
|
|
|
|
|
private TaskRequest taskRequest;
|
|
private TaskRequest taskRequest;
|
|
|
|
|
|
@@ -63,7 +66,7 @@ public class PythonTask extends AbstractTaskExecutor {
|
|
super(taskRequest);
|
|
super(taskRequest);
|
|
this.taskRequest = taskRequest;
|
|
this.taskRequest = taskRequest;
|
|
|
|
|
|
- this.pythonCommandExecutor = new PythonCommandExecutor(this::logHandle,
|
|
|
|
|
|
+ this.shellCommandExecutor = new ShellCommandExecutor(this::logHandle,
|
|
taskRequest,
|
|
taskRequest,
|
|
logger);
|
|
logger);
|
|
}
|
|
}
|
|
@@ -93,13 +96,20 @@ public class PythonTask extends AbstractTaskExecutor {
|
|
@Override
|
|
@Override
|
|
public void handle() throws Exception {
|
|
public void handle() throws Exception {
|
|
try {
|
|
try {
|
|
- // construct process
|
|
|
|
- String command = buildCommand();
|
|
|
|
- TaskResponse taskResponse = pythonCommandExecutor.run(command);
|
|
|
|
|
|
+ // generate the content of this python script
|
|
|
|
+ String pythonScriptContent = buildPythonScriptContent();
|
|
|
|
+ // generate the file path of this python script
|
|
|
|
+ String pythonScriptFile = buildPythonCommandFilePath();
|
|
|
|
+
|
|
|
|
+ // create this file
|
|
|
|
+ createPythonCommandFileIfNotExists(pythonScriptContent,pythonScriptFile);
|
|
|
|
+ String command = "python " + pythonScriptFile;
|
|
|
|
+
|
|
|
|
+ TaskResponse taskResponse = shellCommandExecutor.run(command);
|
|
setExitStatusCode(taskResponse.getExitStatusCode());
|
|
setExitStatusCode(taskResponse.getExitStatusCode());
|
|
setAppIds(taskResponse.getAppIds());
|
|
setAppIds(taskResponse.getAppIds());
|
|
setProcessId(taskResponse.getProcessId());
|
|
setProcessId(taskResponse.getProcessId());
|
|
- setVarPool(pythonCommandExecutor.getVarPool());
|
|
|
|
|
|
+ setVarPool(shellCommandExecutor.getVarPool());
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
logger.error("python task failure", e);
|
|
logger.error("python task failure", e);
|
|
setExitStatusCode(TaskConstants.EXIT_CODE_FAILURE);
|
|
setExitStatusCode(TaskConstants.EXIT_CODE_FAILURE);
|
|
@@ -110,7 +120,7 @@ public class PythonTask extends AbstractTaskExecutor {
|
|
@Override
|
|
@Override
|
|
public void cancelApplication(boolean cancelApplication) throws Exception {
|
|
public void cancelApplication(boolean cancelApplication) throws Exception {
|
|
// cancel process
|
|
// cancel process
|
|
- pythonCommandExecutor.cancelApplication();
|
|
|
|
|
|
+ shellCommandExecutor.cancelApplication();
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
@@ -151,12 +161,48 @@ public class PythonTask extends AbstractTaskExecutor {
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * build command
|
|
|
|
|
|
+ * create python command file if not exists
|
|
|
|
+ *
|
|
|
|
+ * @param pythonScript exec python script
|
|
|
|
+ * @param pythonScriptFile python script file
|
|
|
|
+ * @throws IOException io exception
|
|
|
|
+ */
|
|
|
|
+ protected void createPythonCommandFileIfNotExists(String pythonScript, String pythonScriptFile) throws IOException {
|
|
|
|
+ logger.info("tenantCode :{}, task dir:{}", taskRequest.getTenantCode(), taskRequest.getExecutePath());
|
|
|
|
+
|
|
|
|
+ if (!Files.exists(Paths.get(pythonScriptFile))) {
|
|
|
|
+ logger.info("generate python script file:{}", pythonScriptFile);
|
|
|
|
+
|
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
|
+ sb.append("#-*- encoding=utf8 -*-\n");
|
|
|
|
+
|
|
|
|
+ sb.append("\n\n");
|
|
|
|
+ sb.append(pythonScript);
|
|
|
|
+ logger.info(sb.toString());
|
|
|
|
+
|
|
|
|
+ // write data to file
|
|
|
|
+ FileUtils.writeStringToFile(new File(pythonScriptFile),
|
|
|
|
+ sb.toString(),
|
|
|
|
+ StandardCharsets.UTF_8);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * build python command file path
|
|
|
|
+ *
|
|
|
|
+ * @return python command file path
|
|
|
|
+ */
|
|
|
|
+ protected String buildPythonCommandFilePath() {
|
|
|
|
+ return String.format("%s/py_%s.py", taskRequest.getExecutePath(), taskRequest.getTaskAppId());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * build python script content
|
|
*
|
|
*
|
|
* @return raw python script
|
|
* @return raw python script
|
|
* @throws Exception exception
|
|
* @throws Exception exception
|
|
*/
|
|
*/
|
|
- private String buildCommand() throws Exception {
|
|
|
|
|
|
+ private String buildPythonScriptContent() throws Exception {
|
|
String rawPythonScript = pythonParameters.getRawScript().replaceAll("\\r\\n", "\n");
|
|
String rawPythonScript = pythonParameters.getRawScript().replaceAll("\\r\\n", "\n");
|
|
|
|
|
|
// replace placeholder
|
|
// replace placeholder
|
|
@@ -170,7 +216,6 @@ public class PythonTask extends AbstractTaskExecutor {
|
|
rawPythonScript = ParameterUtils.convertParameterPlaceholders(rawPythonScript, ParamUtils.convert(paramsMap));
|
|
rawPythonScript = ParameterUtils.convertParameterPlaceholders(rawPythonScript, ParamUtils.convert(paramsMap));
|
|
|
|
|
|
logger.info("raw python script : {}", pythonParameters.getRawScript());
|
|
logger.info("raw python script : {}", pythonParameters.getRawScript());
|
|
- logger.info("task dir : {}", taskDir);
|
|
|
|
|
|
|
|
return rawPythonScript;
|
|
return rawPythonScript;
|
|
}
|
|
}
|