Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ GONETTE_SMTP_PASSWORD= pass

MAIL_RECIPIENT=test@example.com

DATABASE_RETENTION_PAYMENT_DAY=7

# must be 'true' or 'false'
PAYMENT_CYCLOS_ENABLED=true
PAYMENT_AUTOMATIC_ENABLED=false
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.lagonette.hellos.service;

import io.github.cdimascio.dotenv.Dotenv;
import org.lagonette.hellos.repository.PaymentRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -16,19 +16,19 @@ public class PurgeDatabaseService {
private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());

private final PaymentRepository paymentRepository;
private final Dotenv dotenv;

@Value("${database.retention.payment.days}")
private int nbOfDaysForPayments;

public PurgeDatabaseService(PaymentRepository paymentRepository) {
public PurgeDatabaseService(PaymentRepository paymentRepository, Dotenv dotenv) {
this.paymentRepository = paymentRepository;
this.dotenv = dotenv;
}

@Transactional
@Scheduled(cron = "${database.purge.cron}")
public void purgeDatabase() {
LOGGER.info("Begin of the database purge");

long nbOfDaysForPayments = Long.parseLong(dotenv.get("DATABASE_RETENTION_PAYMENT_DAY"));

@Marshall71 Marshall71 Apr 16, 2022

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jymaire parseLong génère une exception NumberFormatException si la valeur peut pas être convertie.
Comment veux tu qu'elle soit gérée ? Un try catch avec un log d'erreur en cas d'execption ?
Comment se passe le throws de l'exception si elle pope, ça kill l'application ?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

c'est de la purge donc ça doit pas couper le process métier. Try catch et mail d'erreur (dans les logs c'est nécessaire mais pas suffisant)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ce qui pourrait être bien aussi, c'est de mettre une valeur par défaut (à 7 jours) si jamais la propriété n'est pas remplie (et un message de log en INFO pour prévenir, idéalement une seule fois au démarrage et pas chaque nuit)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

là je sèche parce que à part faire un compteur pour savoir si c'est la première fois je vois pas d'autre solution.
Je peux pas déclarer la variable nbOfDaysForPayments en static (variable de classe), car au moment ou j'initialiserai ma varialbe dans mon bloc static{} ma dotenv variable sera null (le constructeur n'aura pas encore été appelé) :'(

@jymaire jymaire Apr 16, 2022

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tu as des events spring que tu peux attraper pour dire "execute moi ce boot de code une fois l'init terminée" (exemple ici : https://stackoverflow.com/questions/27405713/running-code-after-spring-boot-starts ) à voir si ça fonctionne comme tu veux :)
Edit : doc officielle avec les events dispo https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#features.spring-application.application-events-and-listeners

// Payment purge
List<Integer> paymentsToDelete = paymentRepository.findIdsByInsertionDateBefore(LocalDateTime.now().minusDays(nbOfDaysForPayments));
int paymentsSize = paymentsToDelete.size();
Expand Down
1 change: 0 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
###################
# Database config #
###################
database.retention.payment.days=7
database.purge.cron=0 0 0 * * *
# To use when application is behind a proxy responsible for HTTPS management
server.use-forward-headers=true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.lagonette.hellos.service;

import io.github.cdimascio.dotenv.Dotenv;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.lagonette.hellos.repository.PaymentRepository;
Expand All @@ -19,9 +21,18 @@ class PurgeDatabaseServiceTest {
@Mock
private PaymentRepository paymentRepository;

@Mock
private Dotenv dotenv;


@InjectMocks
private PurgeDatabaseService purgeDatabaseService;

@BeforeEach
void before(){
when(dotenv.get("DATABASE_RETENTION_PAYMENT_DAY")).thenReturn("7");
}

@Test
void purgeDatabase() {
// GIVEN
Expand Down