-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Old messages are deleted after a configurable time (default is …
…after 180 days) with all associated data. - Integrates scheduled jobs which deletes the old messages. - Sets the default property values in application.properties. - Extends the repositories for the necessary methods. PR #773
- Loading branch information
1 parent
eadb139
commit d768632
Showing
5 changed files
with
147 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
iris-client-bff/src/main/java/iris/client_bff/iris_messages/IrisMessageDeleteJob.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package iris.client_bff.iris_messages; | ||
|
||
import iris.client_bff.iris_messages.IrisMessage.IrisMessageIdentifier; | ||
import lombok.Getter; | ||
import lombok.NonNull; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.time.Duration; | ||
import java.time.Instant; | ||
import java.util.stream.Collectors; | ||
|
||
import javax.transaction.Transactional; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.boot.context.properties.ConstructorBinding; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* This class collects all old events and deletes this. | ||
* | ||
* @author Jens Kutzsche | ||
*/ | ||
@Component | ||
@Slf4j | ||
@RequiredArgsConstructor | ||
class IrisMessageDeleteJob { | ||
|
||
private final @NonNull IrisMessageRepository messageRepo; | ||
private final @NonNull Properties properties; | ||
|
||
@Transactional | ||
@Scheduled(cron = "${iris.client.message.delete-cron:-}") | ||
void deleteMessages() { | ||
|
||
var refDate = Instant.now().minus(properties.getDeleteAfter()); | ||
|
||
var oldMessages = messageRepo.findByMetadataCreatedIsBefore(refDate).toList(); | ||
|
||
if (oldMessages.isEmpty()) { | ||
return; | ||
} | ||
|
||
log.debug("{} IRIS message(s) are deleted with period {} after their creation!", | ||
oldMessages.size(), | ||
properties.getDeleteAfter(), | ||
oldMessages.get(0).getCreatedAt()); | ||
|
||
messageRepo.deleteAll(oldMessages); | ||
|
||
log.info("{} IRIS message(s) (IDs: {}) were deleted with period {} after their creation at {}!", | ||
oldMessages.size(), | ||
oldMessages.stream() | ||
.map(IrisMessage::getId) | ||
.map(IrisMessageIdentifier::toString) | ||
.collect(Collectors.joining(", ")), | ||
properties.getDeleteAfter(), | ||
oldMessages.get(0).getCreatedAt()); | ||
} | ||
|
||
@ConstructorBinding | ||
@RequiredArgsConstructor | ||
@ConfigurationProperties("iris.client.message") | ||
@Getter | ||
public static class Properties { | ||
|
||
/** | ||
* Defines the {@link Duration} after that a IRIS message will be deleted starting from the creation date. | ||
*/ | ||
private final Duration deleteAfter; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...bff/src/test/java/iris/client_bff/iris_messages/IrisMessageDeleteJobIntegrationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package iris.client_bff.iris_messages; | ||
|
||
import static java.time.Duration.*; | ||
import static org.assertj.core.api.Assertions.*; | ||
|
||
import iris.client_bff.IrisWebIntegrationTest; | ||
import iris.client_bff.core.IrisDateTimeProvider; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* @author Jens Kutzsche | ||
*/ | ||
@IrisWebIntegrationTest | ||
@RequiredArgsConstructor | ||
class IrisMessageDeleteJobIntegrationTests { | ||
|
||
private final IrisMessageRepository messageRepo; | ||
private final IrisMessageDataRepository dataRepo; | ||
private final IrisMessageFolderRepository folderRepo; | ||
private final IrisDateTimeProvider dateTimeProvider; | ||
private final IrisMessageDeleteJob deleteJob; | ||
|
||
@Test | ||
void testDeleteMessages() { | ||
|
||
var messagesSize = messageRepo.findAll().size(); | ||
var dataSize = dataRepo.findAll().size(); | ||
|
||
// in time | ||
dateTimeProvider.setDelta(ofDays(-179)); | ||
|
||
createMessage(); | ||
|
||
// to old | ||
dateTimeProvider.setDelta(ofDays(-181)); | ||
|
||
createMessage(); | ||
|
||
dateTimeProvider.reset(); | ||
|
||
// extra element from data initialization | ||
assertThat(messageRepo.findAll()).hasSize(messagesSize + 2); | ||
assertThat(dataRepo.findAll()).hasSize(dataSize + 2); | ||
|
||
deleteJob.deleteMessages(); | ||
|
||
assertThat(messageRepo.findAll()).hasSize(messagesSize + 1); | ||
assertThat(dataRepo.findAll()).hasSize(dataSize + 1); | ||
} | ||
|
||
private void createMessage() { | ||
|
||
var testData = new IrisMessageTestData(); | ||
|
||
var folder = this.folderRepo.findFirstByContextAndParentFolderIsNull(IrisMessageContext.INBOX).get(); | ||
|
||
messageRepo.save(testData.getTestInboxMessage(folder)); | ||
} | ||
} |