@ -208,13 +208,12 @@ public class ProcessService {
*
*
* @param logger logger
* @param logger logger
* @param host host
* @param host host
* @param validThreadNum validThreadNum
* @param command found command
* @param command found command
* @param processDefinitionCacheMaps
* @return process instance
* @return process instance
* /
* /
@Transactional ( rollbackFor = Exception . class )
public ProcessInstance handleCommand ( Logger logger , String host , Command command , HashMap < String , ProcessDefinition > processDefinitionCacheMaps ) {
public ProcessInstance handleCommand ( Logger logger , String host , int validThreadNum , Command command ) {
ProcessInstance processInstance = constructProcessInstance ( command , host , processDefinitionCacheMaps ) ;
ProcessInstance processInstance = constructProcessInstance ( command , host ) ;
// cannot construct process instance, return null
// cannot construct process instance, return null
if ( processInstance = = null ) {
if ( processInstance = = null ) {
logger . error ( "scan command, command parameter is error: {}" , command ) ;
logger . error ( "scan command, command parameter is error: {}" , command ) ;
@ -235,7 +234,6 @@ public class ProcessService {
* @param command command
* @param command command
* @param message message
* @param message message
* /
* /
@Transactional ( rollbackFor = Exception . class )
public void moveToErrorCommand ( Command command , String message ) {
public void moveToErrorCommand ( Command command , String message ) {
ErrorCommand errorCommand = new ErrorCommand ( command , message ) ;
ErrorCommand errorCommand = new ErrorCommand ( command , message ) ;
this . errorCommandMapper . insert ( errorCommand ) ;
this . errorCommandMapper . insert ( errorCommand ) ;
@ -286,15 +284,6 @@ public class ProcessService {
return result ;
return result ;
}
}
/ * *
* find one command from queue list
*
* @return command
* /
public Command findOneCommand ( ) {
return commandMapper . getOneToRun ( ) ;
}
/ * *
/ * *
* get command page
* get command page
*
*
@ -547,7 +536,9 @@ public class ProcessService {
processInstance . getWorkerGroup ( ) ,
processInstance . getWorkerGroup ( ) ,
processInstance . getEnvironmentCode ( ) ,
processInstance . getEnvironmentCode ( ) ,
processInstance . getProcessInstancePriority ( ) ,
processInstance . getProcessInstancePriority ( ) ,
processInstance . getDryRun ( )
processInstance . getDryRun ( ) ,
processInstance . getId ( ) ,
processInstance . getProcessDefinitionVersion ( )
) ;
) ;
saveCommand ( command ) ;
saveCommand ( command ) ;
return ;
return ;
@ -744,90 +735,74 @@ public class ProcessService {
/ * *
/ * *
* construct process instance according to one command .
* construct process instance according to one command .
*
*
* @param command command
* @param command command
* @param host host
* @param host host
* @param processDefinitionCacheMaps
* @return process instance
* @return process instance
* /
* /
private ProcessInstance constructProcessInstance ( Command command , String host ) {
private ProcessInstance constructProcessInstance ( Command command , String host , HashMap < String , ProcessDefinition > processDefinitionCacheMaps ) {
ProcessInstance processInstance ;
ProcessInstance processInstance ;
ProcessDefinition processDefinition ;
CommandType commandType = command . getCommandType ( ) ;
CommandType commandType = command . getCommandType ( ) ;
Map < String , String > cmdParam = JSONUtils . toMap ( command . getCommandParam ( ) ) ;
String key = String . format ( "%d-%d" , command . getProcessDefinitionCode ( ) , command . getProcessDefinitionVersion ( ) ) ;
if ( processDefinitionCacheMaps . containsKey ( key ) ) {
ProcessDefinition processDefinition = getProcessDefinitionByCommand ( command . getProcessDefinitionCode ( ) , cmdParam ) ;
processDefinition = processDefinitionCacheMaps . get ( key ) ;
} else {
processDefinition = this . findProcessDefinition ( command . getProcessDefinitionCode ( ) , command . getProcessDefinitionVersion ( ) ) ;
if ( processDefinition ! = null ) {
processDefinitionCacheMaps . put ( key , processDefinition ) ;
}
}
if ( processDefinition = = null ) {
if ( processDefinition = = null ) {
logger . error ( "cannot find the work process define! define code : {}" , command . getProcessDefinitionCode ( ) ) ;
logger . error ( "cannot find the work process define! define code : {}" , command . getProcessDefinitionCode ( ) ) ;
return null ;
return null ;
}
}
Map < String , String > cmdParam = JSONUtils . toMap ( command . getCommandParam ( ) ) ;
int processInstanceId = command . getProcessInstanceId ( ) ;
if ( processInstanceId = = 0 ) {
processInstance = generateNewProcessInstance ( processDefinition , command , cmdParam ) ;
} else {
processInstance = this . findProcessInstanceDetailById ( processInstanceId ) ;
if ( processInstance = = null ) {
return processInstance ;
}
}
if ( cmdParam ! = null ) {
if ( cmdParam ! = null ) {
int processInstanceId = 0 ;
CommandType commandTypeIfComplement = getCommandTypeIfComplement ( processInstance , command ) ;
// recover from failure or pause tasks
// reset global params while repeat running is needed by cmdParam
if ( cmdParam . containsKey ( Constants . CMD_PARAM_RECOVER_PROCESS_ID_STRING ) ) {
if ( commandTypeIfComplement = = CommandType . REPEAT_RUNNING ) {
String processId = cmdParam . get ( Constants . CMD_PARAM_RECOVER_PROCESS_ID_STRING ) ;
setGlobalParamIfCommanded ( processDefinition , cmdParam ) ;
processInstanceId = Integer . parseInt ( processId ) ;
if ( processInstanceId = = 0 ) {
logger . error ( "command parameter is error, [ ProcessInstanceId ] is 0" ) ;
return null ;
}
} else if ( cmdParam . containsKey ( Constants . CMD_PARAM_SUB_PROCESS ) ) {
// sub process map
String pId = cmdParam . get ( Constants . CMD_PARAM_SUB_PROCESS ) ;
processInstanceId = Integer . parseInt ( pId ) ;
} else if ( cmdParam . containsKey ( Constants . CMD_PARAM_RECOVERY_WAITING_THREAD ) ) {
// waiting thread command
String pId = cmdParam . get ( Constants . CMD_PARAM_RECOVERY_WAITING_THREAD ) ;
processInstanceId = Integer . parseInt ( pId ) ;
}
}
if ( processInstanceId = = 0 ) {
// Recalculate global parameters after rerun.
processInstance = generateNewProcessInstance ( processDefinition , command , cmdParam ) ;
processInstance . setGlobalParams ( ParameterUtils . curingGlobalParams (
} else {
processInstance = this . findProcessInstanceDetailById ( processInstanceId ) ;
if ( processInstance = = null ) {
return processInstance ;
}
CommandType commandTypeIfComplement = getCommandTypeIfComplement ( processInstance , command ) ;
// reset global params while repeat running is needed by cmdParam
if ( commandTypeIfComplement = = CommandType . REPEAT_RUNNING ) {
setGlobalParamIfCommanded ( processDefinition , cmdParam ) ;
}
// Recalculate global parameters after rerun.
processInstance . setGlobalParams ( ParameterUtils . curingGlobalParams (
processDefinition . getGlobalParamMap ( ) ,
processDefinition . getGlobalParamMap ( ) ,
processDefinition . getGlobalParamList ( ) ,
processDefinition . getGlobalParamList ( ) ,
commandTypeIfComplement ,
commandTypeIfComplement ,
processInstance . getScheduleTime ( ) ) ) ;
processInstance . getScheduleTime ( ) ) ) ;
processInstance . setProcessDefinition ( processDefinition ) ;
processInstance . setProcessDefinition ( processDefinition ) ;
}
}
//reset command parameter
//reset command parameter
if ( processInstance . getCommandParam ( ) ! = null ) {
if ( processInstance . getCommandParam ( ) ! = null ) {
Map < String , String > processCmdParam = JSONUtils . toMap ( processInstance . getCommandParam ( ) ) ;
Map < String , String > processCmdParam = JSONUtils . toMap ( processInstance . getCommandParam ( ) ) ;
for ( Map . Entry < String , String > entry : processCmdParam . entrySet ( ) ) {
for ( Map . Entry < String , String > entry : processCmdParam . entrySet ( ) ) {
if ( ! cmdParam . containsKey ( entry . getKey ( ) ) ) {
if ( ! cmdParam . containsKey ( entry . getKey ( ) ) ) {
cmdParam . put ( entry . getKey ( ) , entry . getValue ( ) ) ;
cmdParam . put ( entry . getKey ( ) , entry . getValue ( ) ) ;
}
}
}
}
}
// reset command parameter if sub process
}
if ( cmdParam . containsKey ( Constants . CMD_PARAM_SUB_PROCESS ) ) {
// reset command parameter if sub process
processInstance . setCommandParam ( command . getCommandParam ( ) ) ;
if ( cmdParam . containsKey ( Constants . CMD_PARAM_SUB_PROCESS ) ) {
}
processInstance . setCommandParam ( command . getCommandParam ( ) ) ;
} else {
// generate one new process instance
processInstance = generateNewProcessInstance ( processDefinition , command , cmdParam ) ;
}
}
if ( Boolean . FALSE . equals ( checkCmdParam ( command , cmdParam ) ) ) {
if ( Boolean . FALSE . equals ( checkCmdParam ( command , cmdParam ) ) ) {
logger . error ( "command parameter check failed!" ) ;
logger . error ( "command parameter check failed!" ) ;
return null ;
return null ;
}
}
if ( command . getScheduleTime ( ) ! = null ) {
if ( command . getScheduleTime ( ) ! = null ) {
processInstance . setScheduleTime ( command . getScheduleTime ( ) ) ;
processInstance . setScheduleTime ( command . getScheduleTime ( ) ) ;
}
}
processInstance . setHost ( host ) ;
processInstance . setHost ( host ) ;
ExecutionStatus runStatus = ExecutionStatus . RUNNING_EXECUTION ;
ExecutionStatus runStatus = ExecutionStatus . RUNNING_EXECUTION ;
int runTime = processInstance . getRunTimes ( ) ;
int runTime = processInstance . getRunTimes ( ) ;
switch ( commandType ) {
switch ( commandType ) {
@ -846,7 +821,7 @@ public class ProcessService {
initTaskInstance ( this . findTaskInstanceById ( taskId ) ) ;
initTaskInstance ( this . findTaskInstanceById ( taskId ) ) ;
}
}
cmdParam . put ( Constants . CMD_PARAM_RECOVERY_START_NODE_STRING ,
cmdParam . put ( Constants . CMD_PARAM_RECOVERY_START_NODE_STRING ,
String . join ( Constants . COMMA , convertIntListToString ( failedList ) ) ) ;
String . join ( Constants . COMMA , convertIntListToString ( failedList ) ) ) ;
processInstance . setCommandParam ( JSONUtils . toJsonString ( cmdParam ) ) ;
processInstance . setCommandParam ( JSONUtils . toJsonString ( cmdParam ) ) ;
processInstance . setRunTimes ( runTime + 1 ) ;
processInstance . setRunTimes ( runTime + 1 ) ;
break ;
break ;
@ -859,7 +834,7 @@ public class ProcessService {
cmdParam . remove ( Constants . CMD_PARAM_RECOVERY_START_NODE_STRING ) ;
cmdParam . remove ( Constants . CMD_PARAM_RECOVERY_START_NODE_STRING ) ;
List < Integer > suspendedNodeList = this . findTaskIdByInstanceState ( processInstance . getId ( ) , ExecutionStatus . PAUSE ) ;
List < Integer > suspendedNodeList = this . findTaskIdByInstanceState ( processInstance . getId ( ) , ExecutionStatus . PAUSE ) ;
List < Integer > stopNodeList = findTaskIdByInstanceState ( processInstance . getId ( ) ,
List < Integer > stopNodeList = findTaskIdByInstanceState ( processInstance . getId ( ) ,
ExecutionStatus . KILL ) ;
ExecutionStatus . KILL ) ;
suspendedNodeList . addAll ( stopNodeList ) ;
suspendedNodeList . addAll ( stopNodeList ) ;
for ( Integer taskId : suspendedNodeList ) {
for ( Integer taskId : suspendedNodeList ) {
// initialize the pause state
// initialize the pause state
@ -1274,7 +1249,7 @@ public class ProcessService {
}
}
}
}
String processParam = getSubWorkFlowParam ( instanceMap , parentProcessInstance , fatherParams ) ;
String processParam = getSubWorkFlowParam ( instanceMap , parentProcessInstance , fatherParams ) ;
int subProcessInstanceId = childInstance = = null ? 0 : childInstance . getId ( ) ;
return new Command (
return new Command (
commandType ,
commandType ,
TaskDependType . TASK_POST ,
TaskDependType . TASK_POST ,
@ -1288,7 +1263,9 @@ public class ProcessService {
task . getWorkerGroup ( ) ,
task . getWorkerGroup ( ) ,
task . getEnvironmentCode ( ) ,
task . getEnvironmentCode ( ) ,
parentProcessInstance . getProcessInstancePriority ( ) ,
parentProcessInstance . getProcessInstancePriority ( ) ,
parentProcessInstance . getDryRun ( )
parentProcessInstance . getDryRun ( ) ,
subProcessInstanceId ,
parentProcessInstance . getProcessDefinitionVersion ( )
) ;
) ;
}
}
@ -1886,6 +1863,8 @@ public class ProcessService {
//2 insert into recover command
//2 insert into recover command
Command cmd = new Command ( ) ;
Command cmd = new Command ( ) ;
cmd . setProcessDefinitionCode ( processDefinition . getCode ( ) ) ;
cmd . setProcessDefinitionCode ( processDefinition . getCode ( ) ) ;
cmd . setProcessDefinitionVersion ( processDefinition . getVersion ( ) ) ;
cmd . setProcessInstanceId ( processInstance . getId ( ) ) ;
cmd . setCommandParam ( String . format ( "{\"%s\":%d}" , Constants . CMD_PARAM_RECOVER_PROCESS_ID_STRING , processInstance . getId ( ) ) ) ;
cmd . setCommandParam ( String . format ( "{\"%s\":%d}" , Constants . CMD_PARAM_RECOVER_PROCESS_ID_STRING , processInstance . getId ( ) ) ) ;
cmd . setExecutorId ( processInstance . getExecutorId ( ) ) ;
cmd . setExecutorId ( processInstance . getExecutorId ( ) ) ;
cmd . setCommandType ( CommandType . RECOVER_TOLERANCE_FAULT_PROCESS ) ;
cmd . setCommandType ( CommandType . RECOVER_TOLERANCE_FAULT_PROCESS ) ;