Skip to content

Commit

Permalink
logging debug
Browse files Browse the repository at this point in the history
  • Loading branch information
jacopocarlini committed Jul 5, 2024
1 parent e00fc2e commit deaf961
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 71 deletions.
2 changes: 1 addition & 1 deletion .java-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
11
17.0
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
# How to Run

```
mvn clean package
mvn azure-functions:run
```
42 changes: 21 additions & 21 deletions docker/run_docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ then
echo "No environment specified: local is used."
fi

pip3 install yq
#pip3 install yq

if [ "$ENV" = "local" ]; then
image="service-local:latest"
Expand Down Expand Up @@ -45,23 +45,23 @@ for line in $(echo $secret | jq -r '. | to_entries[] | select(.key) | "\(.key)=\
done


stack_name=$(cd .. && basename "$PWD")
docker compose -p "${stack_name}" up -d --remove-orphans --force-recreate --build
#docker build -t receipt-pdf-generator ../
# docker run -d -p 60486:80 --name="${stack_name}" receipt-pdf-generator

# waiting the containers
printf 'Waiting for the service'
attempt_counter=0
max_attempts=50
until [ $(curl -s -o /dev/null -w "%{http_code}" http://localhost:60486/info) -eq 200 ]; do
if [ ${attempt_counter} -eq ${max_attempts} ];then
echo "Max attempts reached"
exit 1
fi

printf '.'
attempt_counter=$((attempt_counter+1))
sleep 5
done
echo 'Service Started'
#stack_name=$(cd .. && basename "$PWD")
#docker compose -p "${stack_name}" up -d --remove-orphans --force-recreate --build
##docker build -t receipt-pdf-generator ../
## docker run -d -p 60486:80 --name="${stack_name}" receipt-pdf-generator
#
## waiting the containers
#printf 'Waiting for the service'
#attempt_counter=0
#max_attempts=50
#until [ $(curl -s -o /dev/null -w "%{http_code}" http://localhost:60486/info) -eq 200 ]; do
# if [ ${attempt_counter} -eq ${max_attempts} ];then
# echo "Max attempts reached"
# exit 1
# fi
#
# printf '.'
# attempt_counter=$((attempt_counter+1))
# sleep 5
#done
#echo 'Service Started'
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@

package it.gov.pagopa.print.payment.notice.functions;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.microsoft.azure.functions.ExecutionContext;
import com.microsoft.azure.functions.HttpStatus;
import com.microsoft.azure.functions.OutputBinding;
import com.microsoft.azure.functions.annotation.*;
import com.microsoft.azure.functions.annotation.Cardinality;
import com.microsoft.azure.functions.annotation.EventHubOutput;
import com.microsoft.azure.functions.annotation.EventHubTrigger;
import com.microsoft.azure.functions.annotation.FunctionName;
import it.gov.pagopa.print.payment.notice.functions.client.PaymentNoticeGenerationRequestErrorClient;
import it.gov.pagopa.print.payment.notice.functions.client.impl.PaymentNoticeGenerationRequestErrorClientImpl;
import it.gov.pagopa.print.payment.notice.functions.entity.PaymentGenerationRequestStatus;
Expand All @@ -24,7 +26,6 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* Azure Functions with Azure Queue trigger.
Expand All @@ -39,11 +40,11 @@ public class ManageNoticeErrors {

private final Integer maxRetriesOnErrors;

private String ERROR_STRING = "[{}] error recovering error data with id {}";
private final String ERROR_STRING = "[{}] error recovering error data with id {}";

public ManageNoticeErrors() {
this.noticeFolderService = new NoticeFolderServiceImpl();
this.maxRetriesOnErrors = Integer.parseInt(System.getenv("MAX_RETRIES_ON_ERRORS"));
this.maxRetriesOnErrors = Integer.parseInt(System.getenv("MAX_RETRIES_ON_ERRORS"));
this.paymentNoticeGenerationRequestErrorClient = PaymentNoticeGenerationRequestErrorClientImpl.getInstance();

}
Expand All @@ -58,13 +59,13 @@ public ManageNoticeErrors() {

/**
* This function will be invoked when a EH trigger occurs
*
* <p>
* The function will manage errors coming from the generation service, or compression function
*
* <p>
* In both cases it will be checked if the error does exist, recovering the latest attempt, and updating
* the number of attempts. Whenever the number of attempts exceeds the configured limit, no more actions
* will follow.
*
* <p>
* The retries will be sent on the dedicated channel for the specific type of error managed
*/
@FunctionName("ManageNoticeErrorsProcess")
Expand Down Expand Up @@ -98,36 +99,36 @@ public void processNoticeErrors(
paymentNoticeErrors.forEach(error -> {

PaymentNoticeGenerationRequestError paymentNoticeGenerationRequestError = null;
try {
if (error.getId() != null) {

paymentNoticeGenerationRequestError =
paymentNoticeGenerationRequestErrorClient.findOne(error.getId()).orElseThrow(() ->
new PaymentNoticeManagementException("Request error not found",
HttpStatus.INTERNAL_SERVER_ERROR.value()));
} else {
paymentNoticeGenerationRequestError = PaymentNoticeGenerationRequestError.builder()
.folderId(error.getFolderId())
.errorId(error.getErrorId())
.numberOfAttempts(error.getNumberOfAttempts())
.compressionError(error.isCompressionError())
.data(error.getData())
.errorCode(error.getErrorCode())
.errorDescription(error.getErrorDescription())
.build();
paymentNoticeGenerationRequestError.setId(
paymentNoticeGenerationRequestErrorClient.save(paymentNoticeGenerationRequestError));
}
} catch (Exception e) {
logger.error(ERROR_STRING,
context.getFunctionName(), error.getFolderId(), e);
try {
if(error.getId() != null) {

paymentNoticeGenerationRequestError =
paymentNoticeGenerationRequestErrorClient.findOne(error.getId()).orElseThrow(() ->
new PaymentNoticeManagementException("Request error not found",
HttpStatus.INTERNAL_SERVER_ERROR.value()));
} else {
paymentNoticeGenerationRequestError = PaymentNoticeGenerationRequestError.builder()
.folderId(error.getFolderId())
.errorId(error.getErrorId())
.numberOfAttempts(error.getNumberOfAttempts())
.compressionError(error.isCompressionError())
.data(error.getData())
.errorCode(error.getErrorCode())
.errorDescription(error.getErrorDescription())
.build();
paymentNoticeGenerationRequestError.setId(
paymentNoticeGenerationRequestErrorClient.save(paymentNoticeGenerationRequestError));
}
} catch (Exception e) {
logger.error(ERROR_STRING,
context.getFunctionName(), error.getFolderId(), e);
}


if (paymentNoticeGenerationRequestError != null &&
if(paymentNoticeGenerationRequestError != null &&
error.getNumberOfAttempts() < maxRetriesOnErrors) {

if (error.isCompressionError() &&
if(error.isCompressionError() &&
!"UNKNOWN".equals(paymentNoticeGenerationRequestError.getFolderId())) {
addRequestsToRetry(paymentNoticeGenerationRequestList, context, error, paymentNoticeGenerationRequestError);
} else {
Expand Down Expand Up @@ -160,20 +161,20 @@ private void addRequestsToRetry(List<it.gov.pagopa.print.payment.notice.function
try {
PaymentNoticeGenerationRequest paymentNoticeGenerationRequest =
noticeFolderService.findRequest(error.getId());
if (paymentNoticeGenerationRequest.getStatus().equals(
if(paymentNoticeGenerationRequest.getStatus().equals(
PaymentGenerationRequestStatus.COMPLETING)) {
completionToRetry.add(
it.gov.pagopa.print.payment.notice.functions.model.PaymentNoticeGenerationRequest
.builder()
.id(paymentNoticeGenerationRequest.getId())
.numberOfElementsTotal(paymentNoticeGenerationRequest
.getNumberOfElementsTotal())
.numberOfElementsFailed(paymentNoticeGenerationRequest
.getNumberOfElementsFailed())
.status(paymentNoticeGenerationRequest.getStatus())
.userId(paymentNoticeGenerationRequest.getUserId())
.items(paymentNoticeGenerationRequest.getItems())
.build()
.id(paymentNoticeGenerationRequest.getId())
.numberOfElementsTotal(paymentNoticeGenerationRequest
.getNumberOfElementsTotal())
.numberOfElementsFailed(paymentNoticeGenerationRequest
.getNumberOfElementsFailed())
.status(paymentNoticeGenerationRequest.getStatus())
.userId(paymentNoticeGenerationRequest.getUserId())
.items(paymentNoticeGenerationRequest.getItems())
.build()
);
}
} catch (RequestRecoveryException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@
*/
public class PaymentNoticeBlobClientImpl implements PaymentNoticeBlobClient {

private final Logger logger = LoggerFactory.getLogger(ManageNoticeErrors.class);

private static PaymentNoticeBlobClientImpl instance;

private final Logger logger = LoggerFactory.getLogger(ManageNoticeErrors.class);
private final String containerName = System.getenv("BLOB_STORAGE_CONTAINER_NAME");

private final BlobServiceClient blobServiceClient;
Expand All @@ -51,7 +49,7 @@ private PaymentNoticeBlobClientImpl() {
}

public static PaymentNoticeBlobClientImpl getInstance() {
if (instance == null) {
if(instance == null) {
instance = new PaymentNoticeBlobClientImpl();
}

Expand All @@ -61,11 +59,13 @@ public static PaymentNoticeBlobClientImpl getInstance() {
/**
* Using the provided id it will attempt to recover the folder data,
* compressing
*
* @param folderId
* @return
*/
@SneakyThrows
public BlobStorageResponse compressFolder(String folderId) {
logger.info("Create Zip file. Request {}", folderId);

BlobContainerClient blobContainerClient = blobServiceClient.getBlobContainerClient(containerName);

Expand All @@ -83,7 +83,8 @@ public BlobStorageResponse compressFolder(String folderId) {
blobContainerClient.listBlobsByHierarchy(delimiter, options, null)
.stream().forEach(blobItem -> {

if (!blobItem.isPrefix() && blobItem.getName().contains(".pdf")) {
if(!blobItem.isPrefix() && blobItem.getName().contains(".pdf")) {
logger.info("Get blob client. Request {}", folderId);
final BlobClient blobClient = blobContainerClient.getBlobClient(blobItem.getName());

final String[] splitName = blobItem.getName().split(delimiter, 2);
Expand All @@ -92,7 +93,7 @@ public BlobStorageResponse compressFolder(String folderId) {

CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
try (ByteArrayOutputStream fileOutputStream = new ByteArrayOutputStream()) {
if (blobClient.exists()) {
if(blobClient.exists()) {
blobClient.downloadStream(fileOutputStream);
zipStream.putNextEntry(new ZipEntry(finalSingleFileName));
zipStream.write(fileOutputStream.toByteArray());
Expand All @@ -103,6 +104,8 @@ public BlobStorageResponse compressFolder(String folderId) {
} catch (IOException e) {
throw new RuntimeException("Error processing file: " + finalSingleFileName, e);
}
logger.info("Download completed. Request {}", folderId);

});

futures.add(future);
Expand Down

0 comments on commit deaf961

Please sign in to comment.