diff --git a/history/taskana-simplehistory-provider/src/main/java/pro/taskana/simplehistory/impl/jobs/HistoryCleanupJob.java b/history/taskana-simplehistory-provider/src/main/java/pro/taskana/simplehistory/impl/jobs/HistoryCleanupJob.java index 82441c46f..958f32f87 100644 --- a/history/taskana-simplehistory-provider/src/main/java/pro/taskana/simplehistory/impl/jobs/HistoryCleanupJob.java +++ b/history/taskana-simplehistory-provider/src/main/java/pro/taskana/simplehistory/impl/jobs/HistoryCleanupJob.java @@ -161,17 +161,23 @@ public class HistoryCleanupJob extends AbstractTaskanaJob { mapping(TaskHistoryEvent::getTaskId, toList())))); List taskIdsToDeleteHistoryEventsFor = new ArrayList<>(); + String createdKey = TaskHistoryEventType.CREATED.getName(); taskHistoryIdsByEventTypeByParentBusinessProcessId.forEach( (parentBusinessProcessId, taskHistoryIdsByEventType) -> { - if (taskHistoryIdsByEventType.get(TaskHistoryEventType.CREATED.getName()).size() + if (!taskHistoryIdsByEventType.containsKey(createdKey)) { + LOGGER.error( + "Issue during history cleanup tasks with enabled parent business process. " + + "No events for parent business process {} with type {} found." + + "Please clean up those history events manually", + parentBusinessProcessId, + createdKey); + } else if (taskHistoryIdsByEventType.get(createdKey).size() == taskHistoryIdsByEventType.entrySet().stream() - .filter( - not(entry -> entry.getKey().equals(TaskHistoryEventType.CREATED.getName()))) + .filter(not(entry -> entry.getKey().equals(createdKey))) .mapToInt(stringListEntry -> stringListEntry.getValue().size()) .sum()) { - taskIdsToDeleteHistoryEventsFor.addAll( - taskHistoryIdsByEventType.get(TaskHistoryEventType.CREATED.getName())); + taskIdsToDeleteHistoryEventsFor.addAll(taskHistoryIdsByEventType.get(createdKey)); } }); diff --git a/history/taskana-simplehistory-provider/src/test/java/acceptance/jobs/HistoryCleanupJobAccTest.java b/history/taskana-simplehistory-provider/src/test/java/acceptance/jobs/HistoryCleanupJobAccTest.java index 61f5d4e45..86635d768 100644 --- a/history/taskana-simplehistory-provider/src/test/java/acceptance/jobs/HistoryCleanupJobAccTest.java +++ b/history/taskana-simplehistory-provider/src/test/java/acceptance/jobs/HistoryCleanupJobAccTest.java @@ -374,6 +374,59 @@ class HistoryCleanupJobAccTest extends AbstractAccTest { assertThat(getHistoryService().createTaskHistoryQuery().count()).isEqualTo(16); } + @Test + @WithAccessId(user = "admin") + void should_NotCleanEvents_When_NoCreatedEventsForParentBusinessProcessIdExist() + throws Exception { + assertThat(getHistoryService().createTaskHistoryQuery().count()).isEqualTo(14); + + TaskHistoryEvent toBeIgnored1 = + createTaskHistoryEvent( + "wbKey1", + "taskId1", + TaskHistoryEventType.CANCELLED.getName(), + "wbKey2", + "someUserId", + "someDetails"); + toBeIgnored1.setCreated(Instant.now().minus(20, ChronoUnit.DAYS)); + toBeIgnored1.setParentBusinessProcessId("toBeIgnored1"); + + TaskHistoryEvent toBeIgnored2 = + createTaskHistoryEvent( + "wbKey1", + "taskId2", + TaskHistoryEventType.COMPLETED.getName(), + "wbKey2", + "someUserId", + "someDetails"); + toBeIgnored2.setCreated(Instant.now().minus(20, ChronoUnit.DAYS)); + toBeIgnored2.setParentBusinessProcessId("toBeIgnored2"); + + TaskHistoryEvent toBeIgnored3 = + createTaskHistoryEvent( + "wbKey1", + "taskId3", + TaskHistoryEventType.TERMINATED.getName(), + "wbKey2", + "someUserId", + "someDetails"); + toBeIgnored3.setCreated(Instant.now().minus(20, ChronoUnit.DAYS)); + toBeIgnored3.setParentBusinessProcessId("toBeIgnored3"); + + getHistoryService().create(toBeIgnored1); + getHistoryService().create(toBeIgnored2); + getHistoryService().create(toBeIgnored3); + + assertThat(getHistoryService().createTaskHistoryQuery().count()).isEqualTo(17); + + taskanaEngine.getConfiguration().setTaskCleanupJobAllCompletedSameParentBusiness(true); + + HistoryCleanupJob job = new HistoryCleanupJob(taskanaEngine, null, null); + job.run(); + + assertThat(getHistoryService().createTaskHistoryQuery().count()).isEqualTo(17); + } + @WithAccessId(user = "admin") @Test void should_DeleteOldHistoryCleanupJobs_When_InitializingSchedule() throws Exception {