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

View File

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

View File

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

View File

@ -99,6 +99,9 @@ public class ScriptRuntimeExecutor implements ScriptExecutor {
private ScriptRuntimeListener listener; private ScriptRuntimeListener listener;
private String currentTaskId; private String currentTaskId;
private Date createdTime = new Date();
private Date finishedTime;
public ScriptRuntimeExecutor(String identifier, String projectId, String envId, Script script) { public ScriptRuntimeExecutor(String identifier, String projectId, String envId, Script script) {
this(identifier, projectId, script, false, false, false); this(identifier, projectId, script, false, false, false);
} }
@ -698,7 +701,7 @@ public class ScriptRuntimeExecutor implements ScriptExecutor {
return null; return null;
} }
private void clear() { protected void clear() {
scriptStepMap.clear(); scriptStepMap.clear();
variables.clear(); variables.clear();
variableMap.clear(); variableMap.clear();
@ -707,6 +710,7 @@ public class ScriptRuntimeExecutor implements ScriptExecutor {
dependScriptResourceMap.clear(); dependScriptResourceMap.clear();
commands.clear(); commands.clear();
libraryInstanceMap.clear(); libraryInstanceMap.clear();
contextVars.clear();
clearResource(); clearResource();
} }
@ -775,6 +779,7 @@ public class ScriptRuntimeExecutor implements ScriptExecutor {
for (int i = 0; i < commands.size(); i++) { for (int i = 0; i < commands.size(); i++) {
try { try {
ScriptStep originStep = commands.get(i); ScriptStep originStep = commands.get(i);
// 通过流拷贝对象,可以重新创建对象,而不是赋值内存地址
step = ObjectUtil.cloneByStream(originStep); step = ObjectUtil.cloneByStream(originStep);
stepResult = createStepResult(step, commands, i + 1); // 脚本步骤 stepResult = createStepResult(step, commands, i + 1); // 脚本步骤
ret.addStepResult(stepResult); ret.addStepResult(stepResult);
@ -2054,4 +2059,35 @@ public class ScriptRuntimeExecutor implements ScriptExecutor {
} }
return StringUtils.abbreviate(object.toString(), 500); 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();
}
} }