Changed retention
This commit is contained in:
@@ -11,7 +11,6 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
|
|||||||
* @author Rafał Żukowicz
|
* @author Rafał Żukowicz
|
||||||
*/
|
*/
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
@EnableConfigurationProperties(RetentionConfigurationProperties.class)
|
|
||||||
public class KlausApplication {
|
public class KlausApplication {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|||||||
@@ -1,34 +1,58 @@
|
|||||||
package com.r11.tools.configuration;
|
package com.r11.tools.configuration;
|
||||||
|
|
||||||
import ch.qos.logback.core.util.FixedDelay;
|
import com.r11.tools.repository.MockedResponseRepository;
|
||||||
import lombok.AllArgsConstructor;
|
import com.r11.tools.repository.RequestHistoryRepository;
|
||||||
|
import org.apache.logging.log4j.LogManager;
|
||||||
|
import org.apache.logging.log4j.Logger;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.scheduling.annotation.Async;
|
import org.springframework.scheduling.annotation.Async;
|
||||||
import org.springframework.scheduling.annotation.EnableAsync;
|
import org.springframework.scheduling.annotation.EnableAsync;
|
||||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||||
import org.springframework.scheduling.annotation.Scheduled;
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
@EnableScheduling
|
@EnableScheduling
|
||||||
@EnableAsync
|
@EnableAsync
|
||||||
@Configuration
|
@Configuration
|
||||||
public class RetentionConfiguration {
|
public class RetentionConfiguration {
|
||||||
|
|
||||||
@Async
|
private final MockedResponseRepository responseRepository;
|
||||||
@Scheduled(fixedDelayString = "#{${retention.minutes-to-delete-message} * 60000}")
|
private final RequestHistoryRepository historyRepository;
|
||||||
public void deleteMessage(){
|
private final RetentionConfigurationProperties retentionProperties;
|
||||||
System.out.println("Message deleting");
|
private final Logger log = LogManager.getRootLogger();
|
||||||
}
|
|
||||||
|
|
||||||
@Async
|
|
||||||
@Scheduled(fixedDelayString = "#{${retention.minutes-to-delete-history-record} * 60000}")
|
|
||||||
public void deleteHistoryRecord(){
|
|
||||||
System.out.println("History deleting");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
public RetentionConfiguration(MockedResponseRepository responseRepository,
|
||||||
|
RequestHistoryRepository historyRepository,
|
||||||
|
RetentionConfigurationProperties retentionProperties){
|
||||||
|
this.historyRepository = historyRepository;
|
||||||
|
this.responseRepository = responseRepository;
|
||||||
|
this.retentionProperties = retentionProperties;
|
||||||
|
}
|
||||||
|
@Scheduled(fixedDelayString = "#{${retention.retention-cooldown} * 60000 }")
|
||||||
@Async
|
@Async
|
||||||
@Scheduled(fixedDelayString = "#{${retention.minutes-to-delete-unused-message} * 60000}")
|
public void deleteMessagesAndHistoryRecords(){
|
||||||
public void deleteUnusedMessages(){
|
log.info("OUTDATED MESSAGES AND HISTORY RECORDS DELETED!");
|
||||||
System.out.println("Unused messages deleting");
|
responseRepository
|
||||||
|
.findAll()
|
||||||
|
.iterator()
|
||||||
|
.forEachRemaining( mockedMessage -> {
|
||||||
|
if (mockedMessage.getCreatedAt().plusMinutes(retentionProperties.getMinutesToDeleteMessage()).isBefore(LocalDateTime.now())){
|
||||||
|
responseRepository.delete(mockedMessage);
|
||||||
|
}
|
||||||
|
} );
|
||||||
|
|
||||||
|
historyRepository
|
||||||
|
.findAll()
|
||||||
|
.iterator()
|
||||||
|
.forEachRemaining(
|
||||||
|
historyRecord -> {
|
||||||
|
if (historyRecord.getDateTimeStamp().plusMinutes(retentionProperties.getMinutesToDeleteHistoryRecord()).isBefore(LocalDateTime.now())){
|
||||||
|
historyRepository.delete(historyRecord);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,5 +21,5 @@ public class RetentionConfigurationProperties {
|
|||||||
@Positive
|
@Positive
|
||||||
private Integer minutesToDeleteHistoryRecord;
|
private Integer minutesToDeleteHistoryRecord;
|
||||||
@Positive
|
@Positive
|
||||||
private Integer minutesToDeleteUnusedMessage;
|
private Integer retentionCooldown;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ import java.util.*;
|
|||||||
@RestController
|
@RestController
|
||||||
@RequestMapping(path = "/api/mock")
|
@RequestMapping(path = "/api/mock")
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
|
@CrossOrigin(origins = "*")
|
||||||
public class MockController {
|
public class MockController {
|
||||||
private final KlausService klausService;
|
private final KlausService klausService;
|
||||||
private final Logger log = LogManager.getRootLogger();
|
private final Logger log = LogManager.getRootLogger();
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import org.springframework.http.HttpStatus;
|
|||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.validation.BindException;
|
import org.springframework.validation.BindException;
|
||||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||||
|
import org.springframework.web.bind.annotation.CrossOrigin;
|
||||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
|
|
||||||
|
|
||||||
@@ -15,6 +16,7 @@ import org.springframework.web.bind.annotation.ExceptionHandler;
|
|||||||
* @author Rafał Żukowicz
|
* @author Rafał Żukowicz
|
||||||
*/
|
*/
|
||||||
@ControllerAdvice
|
@ControllerAdvice
|
||||||
|
@CrossOrigin(origins = "*")
|
||||||
public class MvcExceptionHandler {
|
public class MvcExceptionHandler {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ import java.util.stream.Collectors;
|
|||||||
@RestController
|
@RestController
|
||||||
@RequestMapping(path = "/api/event")
|
@RequestMapping(path = "/api/event")
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
|
@CrossOrigin(origins = "*")
|
||||||
public class RequestHistoryController {
|
public class RequestHistoryController {
|
||||||
private final RequestHistoryService service;
|
private final RequestHistoryService service;
|
||||||
private final RequestHistoryMapper mapper;
|
private final RequestHistoryMapper mapper;
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import org.mapstruct.*;
|
|||||||
public interface MockedMessageMapper {
|
public interface MockedMessageMapper {
|
||||||
@Mapping( target = "compositePrimaryKey", expression = "java(mockedMessageDto.getClientUUID() + \"_\"" +
|
@Mapping( target = "compositePrimaryKey", expression = "java(mockedMessageDto.getClientUUID() + \"_\"" +
|
||||||
" + mockedMessageDto.getMockedResponseId())")
|
" + mockedMessageDto.getMockedResponseId())")
|
||||||
|
@Mapping( target = "createdAt" , expression = "java(java.time.LocalDateTime.now())")
|
||||||
MockedMessage mockedMessageDtoToMockedMessage(MockedMessageDto mockedMessageDto);
|
MockedMessage mockedMessageDtoToMockedMessage(MockedMessageDto mockedMessageDto);
|
||||||
MockedMessageDto mockedMessageToMockedMessageDto(MockedMessage mockedMessage);
|
MockedMessageDto mockedMessageToMockedMessageDto(MockedMessage mockedMessage);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package com.r11.tools.model;
|
|||||||
|
|
||||||
import com.r11.tools.model.constraints.HttpCode;
|
import com.r11.tools.model.constraints.HttpCode;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import javax.validation.constraints.Positive;
|
import javax.validation.constraints.Positive;
|
||||||
@@ -34,6 +35,7 @@ public class MockedMessage implements Serializable {
|
|||||||
private Map<String, String> httpHeaders;
|
private Map<String, String> httpHeaders;
|
||||||
@HttpCode
|
@HttpCode
|
||||||
private Integer httpStatus;
|
private Integer httpStatus;
|
||||||
|
private LocalDateTime createdAt;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,6 +11,6 @@ spring.redis.host=redis
|
|||||||
spring.redis.port=6379
|
spring.redis.port=6379
|
||||||
|
|
||||||
#retention
|
#retention
|
||||||
retention.minutes-to-delete-message=1
|
retention.minutes-to-delete-message=120
|
||||||
retention.minutes-to-delete-history-record=2
|
retention.minutes-to-delete-history-record=1440
|
||||||
retention.minutes-to-delete-unused-message=3
|
retention.retention-cooldown=1440
|
||||||
|
|||||||
Reference in New Issue
Block a user