fix:执行引擎

1.添加资源里逻辑
2.添加对象深度拷贝注释。
hz_1122_temp
李杰应 2024-11-25 10:25:41 +08:00
parent dd1a2df1f9
commit bd78df1ae4
5 changed files with 65 additions and 11 deletions

View File

@ -74,6 +74,10 @@ public class DefaultExecThread implements AtuExecThread{
private RestTemplate restTemplate;
private Date createdTime = new Date();
private Date finishedTime;
private boolean startRecordFlag = false;
public DefaultExecThread(AutoTask task, String deviceToken,boolean running, RabbitTemplate rabbitTemplate) {
@ -139,6 +143,7 @@ public class DefaultExecThread implements AtuExecThread{
if (this.deviceConnection != null) {
try {
this.deviceConnection.close();
this.deviceConnection = null;
} catch(Exception e) {
log.error("任务:"+taskId+"终止断开连接异常");
}
@ -148,6 +153,7 @@ public class DefaultExecThread implements AtuExecThread{
releaseDevice(deviceToken);
}
this.listener = null;
this.finishedTime = new Date();
log.debug("task destroyed.");
}
@ -159,8 +165,10 @@ public class DefaultExecThread implements AtuExecThread{
if (this.deviceConnection != null) {
this.deviceConnection.close();
this.deviceConnection = null;
}
this.listener = null;
this.finishedTime = new Date();
log.debug("finished task destroyed.");
}
@ -228,21 +236,17 @@ public class DefaultExecThread implements AtuExecThread{
try {
script = initScript(execResult);
} catch (Exception e) {
log.error("脚本初始化失败:",e);
//释放设备
releaseDevice(deviceToken);
throw new ExecuteException("脚本初始化失败", e);
}
try {
//脚本执行逻辑处理
handleAutoTask(execResult, script);
} catch (Exception e) {
throw new ExecuteException("执行异常", e);
} finally {
//断开连接
destroyWhenTaskFinish(task.getTaskId());
} catch (Exception e) {
log.error("执行异常", e);
} finally {
//释放设备 调整设备token的释放时间。
releaseDevice(deviceToken);
}
} catch (Exception e) {
log.error("执行任务出现异常", e);
@ -251,6 +255,8 @@ public class DefaultExecThread implements AtuExecThread{
SpringUtils.getBean(PlanDeviceService.class).removeExecThread(task.getTaskId());
SpringUtils.getBean(ScriptResolutionService.class).removeScriptExecutor(task.getTaskId());
SpringUtils.getBean(DeviceConnectionService.class).removeFromMap(task.getDeviceId());
//释放设备
releaseDevice(deviceToken);
}
}

View File

@ -142,7 +142,10 @@ public class PlanDeviceServiceImpl implements PlanDeviceService {
public void removeExecThread(String taskId) {
lock.lock();
try {
threadMap.remove(taskId);
DefaultExecThread removeThread = threadMap.remove(taskId);
if (removeThread != null) {
removeThread.destroy(taskId);
}
} catch (Exception e) {
logger.error("移除任务执行线程失败", e);
} finally {

View File

@ -93,4 +93,9 @@ public interface ScriptExecutor extends IScriptRuntimeContext{
* id
*/
String getProjectId();
/**
*
*/
void empty();
}

View File

@ -46,7 +46,11 @@ public class ScriptResolutionServiceImpl implements ScriptResolutionService {
*/
@Override
public ScriptExecutor removeScriptExecutor(String identifier) {
return scriptExecutorMap.remove(identifier);
ScriptExecutor removeExecutor = scriptExecutorMap.remove(identifier);
if (removeExecutor != null) {
removeExecutor.empty();
}
return removeExecutor;
}
@Override

View File

@ -99,6 +99,9 @@ public class ScriptRuntimeExecutor implements ScriptExecutor {
private ScriptRuntimeListener listener;
private String currentTaskId;
private Date createdTime = new Date();
private Date finishedTime;
public ScriptRuntimeExecutor(String identifier, String projectId, String envId, Script script) {
this(identifier, projectId, script, false, false, false);
}
@ -698,7 +701,7 @@ public class ScriptRuntimeExecutor implements ScriptExecutor {
return null;
}
private void clear() {
protected void clear() {
scriptStepMap.clear();
variables.clear();
variableMap.clear();
@ -707,6 +710,7 @@ public class ScriptRuntimeExecutor implements ScriptExecutor {
dependScriptResourceMap.clear();
commands.clear();
libraryInstanceMap.clear();
contextVars.clear();
clearResource();
}
@ -775,6 +779,7 @@ public class ScriptRuntimeExecutor implements ScriptExecutor {
for (int i = 0; i < commands.size(); i++) {
try {
ScriptStep originStep = commands.get(i);
// 通过流拷贝对象,可以重新创建对象,而不是赋值内存地址
step = ObjectUtil.cloneByStream(originStep);
stepResult = createStepResult(step, commands, i + 1); // 脚本步骤
ret.addStepResult(stepResult);
@ -2054,4 +2059,35 @@ public class ScriptRuntimeExecutor implements ScriptExecutor {
}
return StringUtils.abbreviate(object.toString(), 500);
}
/**
*
*/
public void empty() {
if (parentContext == null) {
log.info("执行结束,开始清理资源...");
}
if (!sonExecutorMap.isEmpty()) {
for (ScriptRuntimeExecutor son : sonExecutorMap.values()) {
try {
son.empty();
son.clear();
son.listener = null;
} catch (Exception e) {
log.error("清理集合发生异常", e);
}
}
sonExecutorMap.clear();
}
try {
clear();
this.listener = null;
} catch (Exception e) {
log.error("清理集合发生异常.", e);
}
if (parentContext == null) {
log.info("资源清理完成...");
}
this.finishedTime = new Date();
}
}