Skip to content

Commit

Permalink
[JBPM-10192] SLA timers are not migrated after migrating process
Browse files Browse the repository at this point in the history
instances
  • Loading branch information
gmunozfe committed Sep 1, 2023
1 parent a52a4c7 commit 31ebc4a
Show file tree
Hide file tree
Showing 6 changed files with 829 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -587,8 +587,18 @@ public Map<Long, List<TimerInstance>> execute(Context context) {
WorkflowProcessInstanceImpl processInstance = (WorkflowProcessInstanceImpl) kieSession.getProcessInstance(migrationSpec.getProcessInstanceId());

Collection<org.jbpm.workflow.instance.NodeInstance> activeInstances = processInstance.getNodeInstances(true);

TimerInstance processSlaTimer = cancelSla(processInstance.getSlaTimerId(), timerManager, processInstance.getId());
if (processSlaTimer != null) {
result.put(-1L, Arrays.asList(processSlaTimer)); //-1 as key is for process, not node
}

for (org.jbpm.workflow.instance.NodeInstance active : activeInstances) {
TimerInstance nodeSlaTimer = cancelSla(((NodeInstanceImpl) active).getSlaTimerId(), timerManager, processInstance.getId());
if (nodeSlaTimer != null) {
result.put(active.getId(), Arrays.asList(nodeSlaTimer));
break;
}
if (active instanceof TimerNodeInstance) {
TimerInstance timerInstance = timerManager.getTimerMap().get(((TimerNodeInstance) active).getTimerId());

Expand Down Expand Up @@ -623,6 +633,16 @@ public Map<Long, List<TimerInstance>> execute(Context context) {
}
}

protected TimerInstance cancelSla(Long slaTimerId, TimerManager tm, long pid) {
if (slaTimerId != null && slaTimerId != -1L) {
TimerInstance timer = tm.getTimerMap().get(slaTimerId);
logger.debug("cancelling SLA timer, "+timer);
tm.cancelTimer(pid, timer.getId());
return timer;
}
return null;
}

protected void rescheduleTimersAfterMigration(RuntimeManager manager, Map<Long, List<TimerInstance>> timerMigrated) {
RuntimeEngine engine = manager.getRuntimeEngine(ProcessInstanceIdContext.get(migrationSpec.getProcessInstanceId()));
try {
Expand All @@ -639,45 +659,67 @@ public Void execute(Context context) {

for (Entry<Long, List<TimerInstance>> entry : timerMigrated.entrySet()) {

if (entry.getKey()==-1L) { //Process SLA are stored with key -1
setSlaProcessTimer(timerManager, processInstance, entry.getValue().get(0));
break;
}

org.jbpm.workflow.instance.NodeInstance active = processInstance.getNodeInstance(entry.getKey(), true);

if (((NodeInstanceImpl) active).getSlaTimerId() != -1L) { //Process SLA for nodes
setSlaNodeTimer(timerManager, processInstance, entry.getValue().get(0), (NodeInstanceImpl) active);
break;
}

if (active instanceof TimerNodeInstance) {
TimerInstance timerInstance = entry.getValue().get(0);

long delay = timerInstance.getDelay() - (System.currentTimeMillis() - timerInstance.getActivated().getTime());
timerInstance.setDelay(delay);

updateBasedOnTrigger(timerInstance);

timerManager.registerTimer(timerInstance, processInstance);
registerTimer(timerManager, processInstance, timerInstance);
((TimerNodeInstance) active).internalSetTimerId(timerInstance.getId());
} else if (active instanceof StateBasedNodeInstance) {

List<TimerInstance> timerInstances = entry.getValue();
List<Long> timers = new ArrayList<>();
for (TimerInstance timerInstance : timerInstances) {
long delay = timerInstance.getDelay() - (System.currentTimeMillis() - timerInstance.getActivated().getTime());
timerInstance.setDelay(delay);

updateBasedOnTrigger(timerInstance);

timerManager.registerTimer(timerInstance, processInstance);
registerTimer(timerManager, processInstance, timerInstance);
timers.add(timerInstance.getId());
}
((StateBasedNodeInstance) active).internalSetTimerInstances(timers);

}
}

return null;
}

});
} finally {

manager.disposeRuntimeEngine(engine);
}
}

protected void setSlaProcessTimer(TimerManager timerManager, WorkflowProcessInstanceImpl processInstance, TimerInstance timerInstance) {
registerTimer(timerManager, processInstance, timerInstance);
processInstance.internalSetSlaTimerId(timerInstance.getId());
processInstance.internalSetSlaDueDate(new Date(System.currentTimeMillis() + getDelay(timerInstance)));
processInstance.internalSetSlaCompliance(ProcessInstance.SLA_PENDING);
logger.debug("Setting new process SLA timer: "+timerInstance);
}

protected void setSlaNodeTimer(TimerManager timerManager, WorkflowProcessInstanceImpl processInstance, TimerInstance timerInstance, NodeInstanceImpl node) {
registerTimer(timerManager, processInstance, timerInstance);
node.internalSetSlaTimerId(timerInstance.getId());
node.internalSetSlaDueDate(new Date(System.currentTimeMillis() + timerInstance.getDelay()));
node.internalSetSlaCompliance(ProcessInstance.SLA_PENDING);
logger.debug("Setting new node SLA timer: "+timerInstance);
}

protected void registerTimer(TimerManager timerManager, WorkflowProcessInstanceImpl processInstance, TimerInstance timerInstance) {
timerInstance.setDelay(getDelay(timerInstance));
updateBasedOnTrigger(timerInstance);
timerManager.registerTimer(timerInstance, processInstance);
}

protected long getDelay(TimerInstance timerInstance) {
return timerInstance.getDelay() - (System.currentTimeMillis() - timerInstance.getActivated().getTime());
}

protected void updateBasedOnTrigger(TimerInstance timerInstance) {
Trigger trigger = ((DefaultJobHandle)timerInstance.getJobHandle()).getTimerJobInstance().getTrigger();
if (trigger instanceof IntervalTrigger && timerInstance.getPeriod() > 0) {
Expand All @@ -696,5 +738,5 @@ protected void updateBasedOnTrigger(TimerInstance timerInstance) {
timerInstance.setDelay(delay);
}
}

}
Loading

0 comments on commit 31ebc4a

Please sign in to comment.