Skip to content

Commit

Permalink
Fix s3 rollback
Browse files Browse the repository at this point in the history
  • Loading branch information
EmanueleValentini1 committed Sep 30, 2024
1 parent c69dab2 commit f946b4b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,11 @@
import org.eclipse.microprofile.rest.client.inject.RestClient;

import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.*;

import static it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorCodeEnum.ATMLM_500;
import static it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorCodeEnum.DATABASE_SAVE_FILE_ERROR;
import static it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorCodeEnum.OBJECT_STORE_SAVE_FILE_ERROR;
import static it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorCodeEnum.RESOURCES_CREATION_ERROR;
import static it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorCodeEnum.RESOURCE_DOES_NOT_EXIST;
import static it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorCodeEnum.RESOURCE_WITH_SAME_SHA256_ALREADY_EXISTS;
import static it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorCodeEnum.*;
import static it.gov.pagopa.atmlayer.service.model.utils.FileUtilities.calculateSha256;

@ApplicationScoped
Expand All @@ -69,7 +58,13 @@ public Uni<List<ResourceEntity>> getAll() {
@Override
@WithTransaction
public Uni<ResourceEntity> save(ResourceEntity resourceEntity) {
return resourceEntityRepository.persist(resourceEntity);
return resourceEntityRepository.persist(resourceEntity)
.onFailure().recoverWithUni(dbException -> {
log.info(dbException.getMessage());
return Uni.createFrom().failure(new AtmLayerException(
String.format("Errore nel salvataggio della risorsa. %s", dbException.getMessage()),
Response.Status.INTERNAL_SERVER_ERROR, DATABASE_SAVE_FILE_ERROR));
});
}

@Override
Expand Down Expand Up @@ -107,11 +102,7 @@ public Uni<ResourceEntity> saveAndUpload(ResourceEntity resourceEntity, File fil
.onItem().transformToUni(putObjectResponse -> {
log.info("Completed Resource Entity Creation");
return Uni.createFrom().item(element);
}))
.onFailure().recoverWithUni(dbException -> Uni.createFrom().failure(new AtmLayerException(
String.format("Errore nel salvataggio della risorsa. %s", dbException.getMessage()),
Response.Status.INTERNAL_SERVER_ERROR, DATABASE_SAVE_FILE_ERROR))
);
}));
}

@Override
Expand Down Expand Up @@ -212,11 +203,11 @@ public Uni<List<String>> deleteResourcesFromStorage(List<String> storageKeys, Li
.onItem().transformToUniAndConcatenate(uploadedStorageKey -> resourceEntityStorageService.delete(uploadedStorageKey)
.onItem().transform(objectStoreResponse -> String.format("Deleted %s", objectStoreResponse.getStorageKey())))
.collect().asList()
.onItem().transform(deletedKeys -> {
.onItem().transform(Unchecked.function(deletedKeys -> {
FileUtilities.cleanDecodedFilesDirectory();
throw new AtmLayerException("Errore nel caricamento dovuto ai seguenti file: " + String.join(", ", errorMessages),
Response.Status.BAD_REQUEST, RESOURCES_CREATION_ERROR);
});
}));
}

@Override
Expand Down Expand Up @@ -277,6 +268,7 @@ public Uni<PageInfo<ResourceEntity>> findResourceFiltered(int pageIndex, int pag
@Override
@WithTransaction
public Uni<Void> disable(UUID uuid) {
String errorMessage = "Errore durante la cancellazione della risorsa: Controllare i log per i dettagli";
return resourceEntityRepository.findById(uuid)
.onItem()
.transformToUni(resourceEntity -> {
Expand All @@ -295,23 +287,18 @@ public Uni<Void> disable(UUID uuid) {
.onFailure()
.recoverWithUni(deleteException -> resourceEntityStorageService.delete(resourceFileUpdated.getStorageKey())
.onItem()
.transform(deletedCopy -> {
.transform(Unchecked.function(deletedCopy -> {
throw new AtmLayerException(
deleteException.getMessage(),
Response.Status.INTERNAL_SERVER_ERROR, ATMLM_500);
}))
)
.onFailure().recoverWithUni(failure ->
Uni.createFrom().failure(new AtmLayerException(
failure.getMessage(),
Response.Status.INTERNAL_SERVER_ERROR, ATMLM_500))
)
.onFailure().recoverWithUni(failure ->
Uni.createFrom().failure(new AtmLayerException(
failure.getMessage(),
Response.Status.INTERNAL_SERVER_ERROR, ATMLM_500))
errorMessage,
Response.Status.INTERNAL_SERVER_ERROR, OBJECT_STORE_COPY_FILE_ERROR);})))
)
));
)
.onFailure().recoverWithUni(failure ->
Uni.createFrom().failure(new AtmLayerException(
errorMessage,
Response.Status.INTERNAL_SERVER_ERROR, DATABASE_SAVE_FILE_ERROR))
)
);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public void testUpdateStorageKey_ResourceNotFound() {
resourceFileService.updateStorageKey(resourceEntity)
.subscribe()
.withSubscriber(UniAssertSubscriber.create())
.assertFailedWith(AtmLayerException.class, "La risorsa di riferimento non esiste: impossibile aggiornare la chiave di archiviazione");
.assertFailedWith(AtmLayerException.class, "Impossibile eliminare la risorsa. Aggiornamento della storage key interrotto");

verify(resourceFileRepository, times(1)).findByStorageKey(storageKey);
}
Expand Down

0 comments on commit f946b4b

Please sign in to comment.