Skip to content

Commit

Permalink
DMP-4065 UnstructuredToArmDataStore leaves record stuck in 'Arm Inges…
Browse files Browse the repository at this point in the history
…tion'

Added new shutdown hook
  • Loading branch information
karen-hedges committed Jan 23, 2025
1 parent f0c3614 commit ba69e0f
Showing 1 changed file with 26 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import javax.annotation.PreDestroy;
import javax.annotation.PostConstruct;

import static java.util.Objects.nonNull;
import static uk.gov.hmcts.darts.common.util.EodHelper.equalsAnyStatus;
import static uk.gov.hmcts.darts.common.util.EodHelper.isEqual;

Expand All @@ -50,6 +51,11 @@ public class UnstructuredToArmBatchProcessorImpl implements UnstructuredToArmBat
private List<Integer> eodsForTransfer;
private UserAccountEntity userAccount;

@PostConstruct
public void init() {
Runtime.getRuntime().addShutdownHook(new Thread(this::resetEodStatusOnShutdown));
}

@Override
@SuppressWarnings({"PMD.AvoidInstantiatingObjectsInLoops", "PMD.CognitiveComplexity", "PMD.CyclomaticComplexity"})
public void processUnstructuredToArm(int taskBatchSize) {
Expand All @@ -65,7 +71,7 @@ public void processUnstructuredToArm(int taskBatchSize) {
taskBatchSize);

log.info("Found {} pending entities to process from source '{}'", eodsForTransfer.size(), eodSourceLocation.getDescription());
if (!eodsForTransfer.isEmpty()) {
if (CollectionUtils.isNotEmpty(eodsForTransfer)) {
//ARM has a max batch size for manifest items, so lets loop through the big list creating lots of individual batches for ARM to process separately
List<List<Integer>> batchesForArm = ListUtils.partition(eodsForTransfer, unstructuredToArmProcessorConfiguration.getMaxArmManifestItems());
AtomicInteger batchCounter = new AtomicInteger(1);
Expand Down Expand Up @@ -191,9 +197,8 @@ private boolean shouldAddEntryToManifestFile(ArmBatchItem batchItem) {
return equalsAnyStatus(batchItem.getPreviousStatus(), EodHelper.failedArmManifestFileStatus(), EodHelper.armResponseManifestFailedStatus());
}

@SuppressWarnings({"PMD.ConfusingTernary"})
private void recoverByUpdatingEodToFailedArmStatus(ArmBatchItem batchItem, UserAccountEntity userAccount) {
if (batchItem.getArmEod() != null) {
if (nonNull(batchItem.getArmEod())) {
logApi.armPushFailed(batchItem.getArmEod().getId());
batchItem.undoManifestFileChange();
if (!batchItem.isRawFilePushNotNeededOrSuccessfulWhenNeeded()) {
Expand All @@ -205,25 +210,26 @@ private void recoverByUpdatingEodToFailedArmStatus(ArmBatchItem batchItem, UserA
}
}

@PreDestroy
//@PreDestroy
public void destroy() {
System.out.println("UnstructuredToArmBatchProcessorImpl shutting down.");
resetEodStatusOnShutdown();
}

private void resetEodStatusOnShutdown() {
log.info("UnstructuredToArmBatchProcessorImpl shutting down.");
if (CollectionUtils.isNotEmpty(eodsForTransfer)) {
System.out.println("Reverting EODs to failed status for potentially EODs " + eodsForTransfer.size());
String eodsIds = eodsForTransfer
.stream()
.map(eodId -> String.valueOf(eodId))
.collect(Collectors.joining(","));
System.out.println("EODs " + eodsIds + " will be reverted to pod recycled status");

unstructuredToArmHelper.updateEodByIdAndStatus(eodsForTransfer,
EodHelper.armIngestionStatus(),
EodHelper.armPushPodRecycledStatus(),
userAccount);
eodsForTransfer.forEach(eodId -> System.out.println("EOD ID: " + eodId + " has been reverted to pod recycled status"));
log.info("Reverting EODs to failed status for potentially EODs {}", eodsForTransfer.size());
String eodsIds = eodsForTransfer.stream().map(String::valueOf).collect(Collectors.joining(","));
log.info("EODs {} will be reverted to pod recycled status", eodsIds);

externalObjectDirectoryRepository.updateEodByIdAndStatus(
eodsForTransfer, EodHelper.armPushPodRecycledStatus(), EodHelper.armIngestionStatus(), userAccount);
log.error("Updated eods from {} to {}", EodHelper.armIngestionStatus().getDescription(), EodHelper.armPushPodRecycledStatus().getDescription());

eodsForTransfer.forEach(eodId -> log.info("EOD ID: {} has been reverted to pod recycled status", eodId));
} else {
System.out.println("No EODs to revert to failed status");
log.info("No EODs to revert to failed status");
}
System.out.println("UnstructuredToArmBatchProcessorImpl has shut down.");
log.info("UnstructuredToArmBatchProcessorImpl has shut down.");
}
}

0 comments on commit ba69e0f

Please sign in to comment.