Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jacopocarlini committed Aug 1, 2024
1 parent 9ce8065 commit be0dd56
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 32 deletions.
19 changes: 7 additions & 12 deletions docker/run_docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,30 @@ if test -f "$FILE"; then
rm .env
fi
config=$(yq -r '."microservice-chart".envConfig' ../helm/values-$ENV.yaml)
for line in $(echo $config | jq -r '. | to_entries[] | select(.key) | "\(.key)=\(.value)"'); do
echo $line >> .env
IFS=$'\n'
for line in $(echo "$config" | jq -r '. | to_entries[] | select(.key) | "\(.key)=\(.value)"'); do
echo "$line" >> .env
done

keyvault=$(yq -r '."microservice-chart".keyvault.name' ../helm/values-$ENV.yaml)
secret=$(yq -r '."microservice-chart".envSecret' ../helm/values-$ENV.yaml)
for line in $(echo $secret | jq -r '. | to_entries[] | select(.key) | "\(.key)=\(.value)"'); do
for line in $(echo "$secret" | jq -r '. | to_entries[] | select(.key) | "\(.key)=\(.value)"'); do
IFS='=' read -r -a array <<< "$line"
response=$(az keyvault secret show --vault-name $keyvault --name "${array[1]}")
value=$(echo $response | jq -r '.value')
value=$(echo "$response" | jq -r '.value')
echo "${array[0]}=$value" >> .env
# if [ "${array[0]}" = "AFM_SA_CONNECTION_STRING" ];then
# echo "Set secret env ${array[0]}"
# echo "::add-mask::$value"
# echo AFM_SA_CONNECTION_STRING=$value >> $GITHUB_ENV
# fi
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
#until $(curl --output /dev/null --silent --head --fail http://localhost:8080/actuator/info); do
# if [ ${attempt_counter} -eq ${max_attempts} ];then
# echo "Max attempts reached"
# exit 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

@Configuration
@Slf4j
public class RetryConsumerConfig {
public class ErrorConsumerConfig {
@Bean
public Consumer<String> noticeError(RetryService retryService) {
return retryService::retryError;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
@AllArgsConstructor
@EqualsAndHashCode(of = "errorId")
@ToString
public class RetryEvent {
public class ErrorEvent {

private String id;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package it.gov.pagopa.print.payment.notice.functions.events.producer;

import it.gov.pagopa.print.payment.notice.functions.events.model.RetryEvent;
import it.gov.pagopa.print.payment.notice.functions.events.model.ErrorEvent;

/**
* Interface to use when required to execute sending of a notice generation request through
Expand All @@ -14,6 +14,6 @@ public interface NoticeRequestErrorProducer {
* @param paymentNoticeGenerationRequestError data to send
* @return boolean referring if the insertion on the sending channel was successfully
*/
boolean sendErrorEvent(RetryEvent paymentNoticeGenerationRequestError);
boolean sendErrorEvent(ErrorEvent paymentNoticeGenerationRequestError);

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package it.gov.pagopa.print.payment.notice.functions.events.producer;

import it.gov.pagopa.print.payment.notice.functions.events.model.RetryEvent;
import it.gov.pagopa.print.payment.notice.functions.events.model.ErrorEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.context.annotation.Bean;
Expand All @@ -22,13 +22,13 @@ public NoticeRequestErrorProducerImpl(StreamBridge streamBridge) {
this.streamBridge = streamBridge;
}

public static Message<RetryEvent> buildMessage(RetryEvent paymentNoticeGenerationRequestError) {
public static Message<ErrorEvent> buildMessage(ErrorEvent paymentNoticeGenerationRequestError) {
return MessageBuilder.withPayload(paymentNoticeGenerationRequestError)
.build();
}

@Override
public boolean sendErrorEvent(RetryEvent paymentNoticeGenerationRequestError) {
public boolean sendErrorEvent(ErrorEvent paymentNoticeGenerationRequestError) {
return streamBridge.send("noticeError-out-0",
buildMessage(paymentNoticeGenerationRequestError));
}
Expand All @@ -41,7 +41,7 @@ public boolean sendErrorEvent(RetryEvent paymentNoticeGenerationRequestError) {
static class NoticeGenerationRequestErrorConfig {

@Bean
public Supplier<Flux<Message<RetryEvent>>> sendErrorEvent() {
public Supplier<Flux<Message<ErrorEvent>>> sendErrorEvent() {
return Flux::empty;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import it.gov.pagopa.print.payment.notice.functions.entity.PaymentGenerationRequestStatus;
import it.gov.pagopa.print.payment.notice.functions.entity.PaymentNoticeGenerationRequest;
import it.gov.pagopa.print.payment.notice.functions.events.model.CompressionEvent;
import it.gov.pagopa.print.payment.notice.functions.events.model.RetryEvent;
import it.gov.pagopa.print.payment.notice.functions.events.model.ErrorEvent;
import it.gov.pagopa.print.payment.notice.functions.events.producer.NoticeRequestCompleteProducer;
import it.gov.pagopa.print.payment.notice.functions.events.producer.NoticeRequestErrorProducer;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -49,7 +49,7 @@ public void compressFolder(String message) {
.status(compressionMessage.getStatus())
.build());
} catch (Exception e) {
var errorMsg = RetryEvent.builder()
var errorMsg = ErrorEvent.builder()
.folderId(compressionMessage.getId())
.errorId(compressionMessage.getId())
.numberOfAttempts(0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import it.gov.pagopa.print.payment.notice.functions.repository.PaymentGenerationRequestRepository;
import it.gov.pagopa.print.payment.notice.functions.storage.NoticeStorageClient;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -48,6 +49,7 @@ public void manageFolder(PaymentNoticeGenerationRequest paymentNoticeGenerationR
PaymentGenerationRequestStatus.PROCESSED);
paymentGenerationRequestRepository.save(paymentNoticeGenerationRequest);
paymentGenerationRequestErrorRepository.deleteById(paymentNoticeGenerationRequest.getId());
MDC.put("massiveStatus", paymentNoticeGenerationRequest.getStatus().toString());
log.info("Massive Request {} [user {}]", paymentNoticeGenerationRequest.getStatus(), paymentNoticeGenerationRequest.getUserId());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import it.gov.pagopa.print.payment.notice.functions.entity.PaymentNoticeGenerationRequest;
import it.gov.pagopa.print.payment.notice.functions.entity.PaymentNoticeGenerationRequestError;
import it.gov.pagopa.print.payment.notice.functions.events.model.CompressionEvent;
import it.gov.pagopa.print.payment.notice.functions.events.model.ErrorEvent;
import it.gov.pagopa.print.payment.notice.functions.events.model.GenerationEvent;
import it.gov.pagopa.print.payment.notice.functions.events.model.RetryEvent;
import it.gov.pagopa.print.payment.notice.functions.events.producer.NoticeGenerationRequestProducer;
import it.gov.pagopa.print.payment.notice.functions.events.producer.NoticeRequestCompleteProducer;
import it.gov.pagopa.print.payment.notice.functions.exception.Aes256Exception;
Expand Down Expand Up @@ -42,13 +42,16 @@ public class RetryService {
@Autowired
private NoticeRequestCompleteProducer noticeRequestCompleteProducer;

@Autowired
private CompressionService compressionService;

@Autowired
private PaymentGenerationRequestErrorRepository paymentGenerationRequestErrorRepository;

public void retryError(String message) {

try {
var retryMessage = objectMapper.readValue(message, RetryEvent.class);
var retryMessage = objectMapper.readValue(message, ErrorEvent.class);

MDC.put("folderId", retryMessage.getFolderId());
log.info("Starting Retry Function {}", retryMessage);
Expand All @@ -68,6 +71,8 @@ public void retryError(String message) {
} else {
GenerationEvent generationEvent = buildNoticeRetry(retryMessage);
noticeGenerationRequestProducer.sendGenerationEvent(generationEvent);
// TODO verify if it works instead send new event
// compressionService.compressFolder(new ObjectMapper().writeValueAsString(generationEvent));
log.debug("Sent a new generation event");
}
}
Expand All @@ -88,11 +93,11 @@ private void updateNumberOfAttempts(PaymentNoticeGenerationRequestError paymentN
log.debug("Updated Number Of Attempts");
}

private boolean isCompressionError(RetryEvent retryMessage, PaymentNoticeGenerationRequestError paymentNoticeGenerationRequestError) {
private boolean isCompressionError(ErrorEvent retryMessage, PaymentNoticeGenerationRequestError paymentNoticeGenerationRequestError) {
return retryMessage.isCompressionError() && !"UNKNOWN".equals(paymentNoticeGenerationRequestError.getFolderId());
}

private PaymentNoticeGenerationRequestError findErrorOrCreate(RetryEvent retryMessage) throws PaymentNoticeManagementException {
private PaymentNoticeGenerationRequestError findErrorOrCreate(ErrorEvent retryMessage) throws PaymentNoticeManagementException {
PaymentNoticeGenerationRequestError paymentNoticeGenerationRequestError;
if (retryMessage.getId() != null) {
paymentNoticeGenerationRequestError = paymentGenerationRequestErrorRepository.findById(retryMessage.getId())
Expand All @@ -113,7 +118,7 @@ private PaymentNoticeGenerationRequestError findErrorOrCreate(RetryEvent retryMe
return paymentNoticeGenerationRequestError;
}

private CompressionEvent buildCompressionError(RetryEvent error) throws RequestRecoveryException {
private CompressionEvent buildCompressionError(ErrorEvent error) throws RequestRecoveryException {

PaymentNoticeGenerationRequest paymentNoticeGenerationRequest = noticeFolderService.findRequest(error.getId());
if (paymentNoticeGenerationRequest.getStatus().equals(PaymentGenerationRequestStatus.COMPLETING)) {
Expand All @@ -133,7 +138,7 @@ private CompressionEvent buildCompressionError(RetryEvent error) throws RequestR

}

private GenerationEvent buildNoticeRetry(RetryEvent error) throws Aes256Exception, JsonProcessingException {
private GenerationEvent buildNoticeRetry(ErrorEvent error) throws Aes256Exception, JsonProcessingException {
String plainRequestData = Aes256Utils.decrypt(error.getData());
GenerationEvent noticeRequestEH = ObjectMapperUtils.mapString(plainRequestData, GenerationEvent.class);
noticeRequestEH.setErrorId(error.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.azure.storage.blob.models.ListBlobsOptions;
import it.gov.pagopa.print.payment.notice.functions.model.response.BlobStorageResponse;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
Expand Down Expand Up @@ -63,13 +64,16 @@ public BlobStorageResponse compressFolder(String folderId) throws IOException {
.forEach(blobItem -> {

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

final String[] splitName = blobItem.getName().split(delimiter, 2);
final String finalSingleFileName = splitName.length > 1 ? splitName[1] : splitName[0];
final String finalSingleFilepath = blobItem.getName();

MDC.put("itemId", finalSingleFilepath);
log.info("Get info file {} from blob. Request {}", blobItem.getName(), folderId);
final BlobClient blobClient = blobContainerClient.getBlobClient(blobItem.getName());


// CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
try (ByteArrayOutputStream fileOutputStream = new ByteArrayOutputStream()) {
if (blobClient.exists()) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ spring.cloud.azure.storage.blob.notices.containerName=${NOTICE_STORAGE_CONTAINER
spring.cloud.function.definition=noticeComplete;noticeError

spring.cloud.stream.bindings.noticeComplete-in-0.destination=${KAFKA_NOTICE_COMPLETE_TOPIC:pagopa-printit-complete-evh}
spring.cloud.stream.bindings.noticeComplete-in-0.group=$Default
#spring.cloud.stream.bindings.noticeComplete-in-0.group=$Default
spring.cloud.stream.bindings.noticeComplete-in-0.content-type=${KAFKA_CONTENT_TYPE:application/json}
spring.cloud.stream.bindings.noticeComplete-in-0.binder=complete-consumer
spring.cloud.stream.binders.complete-consumer.type=kafka
Expand All @@ -75,7 +75,7 @@ spring.cloud.stream.binders.complete-producer.environment.spring.cloud.stream.ka


spring.cloud.stream.bindings.noticeError-in-0.destination=${KAFKA_NOTICE_ERROR_TOPIC:pagopa-printit-errors-evh}
spring.cloud.stream.bindings.noticeError-in-0.group=$Default
#spring.cloud.stream.bindings.noticeError-in-0.group=$Default
spring.cloud.stream.bindings.noticeError-in-0.content-type=${KAFKA_CONTENT_TYPE:application/json}
spring.cloud.stream.bindings.noticeError-in-0.binder=error-consumer
spring.cloud.stream.binders.error-consumer.type=kafka
Expand Down

0 comments on commit be0dd56

Please sign in to comment.