|
|
@ -19,7 +19,6 @@ package org.apache.dolphinscheduler.plugin.task.api; |
|
|
|
|
|
|
|
|
|
|
|
import static org.apache.dolphinscheduler.plugin.task.api.TaskConstants.EXIT_CODE_FAILURE; |
|
|
|
import static org.apache.dolphinscheduler.plugin.task.api.TaskConstants.EXIT_CODE_FAILURE; |
|
|
|
import static org.apache.dolphinscheduler.plugin.task.api.TaskConstants.EXIT_CODE_KILL; |
|
|
|
import static org.apache.dolphinscheduler.plugin.task.api.TaskConstants.EXIT_CODE_KILL; |
|
|
|
import static org.apache.dolphinscheduler.plugin.task.api.utils.ProcessUtils.getPidsStr; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.apache.dolphinscheduler.common.thread.ThreadUtils; |
|
|
|
import org.apache.dolphinscheduler.common.thread.ThreadUtils; |
|
|
|
import org.apache.dolphinscheduler.common.utils.PropertyUtils; |
|
|
|
import org.apache.dolphinscheduler.common.utils.PropertyUtils; |
|
|
@ -191,7 +190,7 @@ public abstract class AbstractCommandExecutor { |
|
|
|
command.add(String.format("--uid=%s", taskRequest.getTenantCode())); |
|
|
|
command.add(String.format("--uid=%s", taskRequest.getTenantCode())); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public TaskResponse run(String execCommand, TaskCallBack taskCallBack) throws IOException, InterruptedException { |
|
|
|
public TaskResponse run(String execCommand, TaskCallBack taskCallBack) throws Exception { |
|
|
|
TaskResponse result = new TaskResponse(); |
|
|
|
TaskResponse result = new TaskResponse(); |
|
|
|
int taskInstanceId = taskRequest.getTaskInstanceId(); |
|
|
|
int taskInstanceId = taskRequest.getTaskInstanceId(); |
|
|
|
if (null == TaskExecutionContextCacheManager.getByTaskInstanceId(taskInstanceId)) { |
|
|
|
if (null == TaskExecutionContextCacheManager.getByTaskInstanceId(taskInstanceId)) { |
|
|
@ -223,8 +222,8 @@ public abstract class AbstractCommandExecutor { |
|
|
|
boolean updateTaskExecutionContextStatus = |
|
|
|
boolean updateTaskExecutionContextStatus = |
|
|
|
TaskExecutionContextCacheManager.updateTaskExecutionContext(taskRequest); |
|
|
|
TaskExecutionContextCacheManager.updateTaskExecutionContext(taskRequest); |
|
|
|
if (Boolean.FALSE.equals(updateTaskExecutionContextStatus)) { |
|
|
|
if (Boolean.FALSE.equals(updateTaskExecutionContextStatus)) { |
|
|
|
ProcessUtils.kill(taskRequest); |
|
|
|
|
|
|
|
result.setExitStatusCode(EXIT_CODE_KILL); |
|
|
|
result.setExitStatusCode(EXIT_CODE_KILL); |
|
|
|
|
|
|
|
cancelApplication(); |
|
|
|
return result; |
|
|
|
return result; |
|
|
|
} |
|
|
|
} |
|
|
|
// print process id
|
|
|
|
// print process id
|
|
|
@ -262,14 +261,13 @@ public abstract class AbstractCommandExecutor { |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
logger.error("process has failure, the task timeout configuration value is:{}, ready to kill ...", |
|
|
|
logger.error("process has failure, the task timeout configuration value is:{}, ready to kill ...", |
|
|
|
taskRequest.getTaskTimeout()); |
|
|
|
taskRequest.getTaskTimeout()); |
|
|
|
ProcessUtils.kill(taskRequest); |
|
|
|
|
|
|
|
result.setExitStatusCode(EXIT_CODE_FAILURE); |
|
|
|
result.setExitStatusCode(EXIT_CODE_FAILURE); |
|
|
|
|
|
|
|
cancelApplication(); |
|
|
|
} |
|
|
|
} |
|
|
|
int exitCode = process.exitValue(); |
|
|
|
int exitCode = process.exitValue(); |
|
|
|
String exitLogMessage = EXIT_CODE_KILL == exitCode ? "process has killed." : "process has exited."; |
|
|
|
String exitLogMessage = EXIT_CODE_KILL == exitCode ? "process has killed." : "process has exited."; |
|
|
|
logger.info(exitLogMessage |
|
|
|
logger.info("{} execute path:{}, processId:{} ,exitStatusCode:{} ,processWaitForStatus:{} ,processExitValue:{}", |
|
|
|
+ " execute path:{}, processId:{} ,exitStatusCode:{} ,processWaitForStatus:{} ,processExitValue:{}", |
|
|
|
exitLogMessage, taskRequest.getExecutePath(), processId, result.getExitStatusCode(), status, exitCode); |
|
|
|
taskRequest.getExecutePath(), processId, result.getExitStatusCode(), status, exitCode); |
|
|
|
|
|
|
|
return result; |
|
|
|
return result; |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
@ -278,53 +276,18 @@ public abstract class AbstractCommandExecutor { |
|
|
|
return varPool.toString(); |
|
|
|
return varPool.toString(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
public void cancelApplication() throws InterruptedException { |
|
|
|
* cancel application |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @throws Exception exception |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public void cancelApplication() throws Exception { |
|
|
|
|
|
|
|
if (process == null) { |
|
|
|
if (process == null) { |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
int processId = getProcessId(process); |
|
|
|
// soft kill
|
|
|
|
logger.info("Begin to kill process process, pid is : {}", processId); |
|
|
|
logger.info("Begin to kill process process, pid is : {}", taskRequest.getProcessId()); |
|
|
|
// kill , waiting for completion
|
|
|
|
process.destroy(); |
|
|
|
boolean alive = softKill(processId); |
|
|
|
if (!process.waitFor(5, TimeUnit.SECONDS)) { |
|
|
|
|
|
|
|
process.destroyForcibly(); |
|
|
|
if (alive) { |
|
|
|
|
|
|
|
String cmd = String.format("kill -9 %s", getPidsStr(processId)); |
|
|
|
|
|
|
|
cmd = OSUtils.getSudoCmd(taskRequest.getTenantCode(), cmd); |
|
|
|
|
|
|
|
OSUtils.exeCmd(cmd); |
|
|
|
|
|
|
|
logger.info("Success kill task: {}, pid: {}, cmd: {}", taskRequest.getTaskAppId(), processId, cmd); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
logger.info("The process: {} is not alive, no need to kill", processId); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
logger.info("Success kill task: {}, pid: {}", taskRequest.getTaskAppId(), taskRequest.getProcessId()); |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* soft kill |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param processId process id |
|
|
|
|
|
|
|
* @return process is alive |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private boolean softKill(int processId) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (processId != 0 && process.isAlive()) { |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
// sudo -u user command to run command
|
|
|
|
|
|
|
|
String cmd = String.format("kill %d", processId); |
|
|
|
|
|
|
|
cmd = OSUtils.getSudoCmd(taskRequest.getTenantCode(), cmd); |
|
|
|
|
|
|
|
logger.info("soft kill task:{}, process id:{}, cmd:{}", taskRequest.getTaskAppId(), processId, cmd); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Runtime.getRuntime().exec(cmd); |
|
|
|
|
|
|
|
} catch (IOException e) { |
|
|
|
|
|
|
|
logger.info("kill attempt failed", e); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return process.isAlive(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void printCommand(List<String> commands) { |
|
|
|
private void printCommand(List<String> commands) { |
|
|
@ -424,7 +387,7 @@ public abstract class AbstractCommandExecutor { |
|
|
|
f.setAccessible(true); |
|
|
|
f.setAccessible(true); |
|
|
|
|
|
|
|
|
|
|
|
processId = f.getInt(process); |
|
|
|
processId = f.getInt(process); |
|
|
|
} catch (Throwable e) { |
|
|
|
} catch (Exception e) { |
|
|
|
logger.error("Get task pid failed", e); |
|
|
|
logger.error("Get task pid failed", e); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|