浏览代码

update cleanup scheduler to delete empty only if older than one day

Daniel Bohry 1 月之前
父节点
当前提交
df2e521b8a

+ 1 - 1
src/main/java/com/lhamacorp/knotes/repository/NoteRepository.java

@@ -14,6 +14,6 @@ public interface NoteRepository extends MongoRepository<Note, String> {
     @Query(value = "{ '_id': ?0 }", fields = "{ 'createdAt': 1, 'modifiedAt': 1 }")
     Optional<Note> findMetadataProjectionById(String id);
 
-    @Query(value = "{ 'content': BinData(0, '') }", fields = "{ '_id': 1 }")
+    @Query(value = "{ 'content': BinData(0, '') }", fields = "{ '_id': 1, 'createdAt': 1 }")
     List<Note> findEmptyNotes();
 }

+ 9 - 5
src/main/java/com/lhamacorp/knotes/service/CleanupScheduler.java

@@ -3,19 +3,22 @@ package com.lhamacorp.knotes.service;
 import com.lhamacorp.knotes.domain.Note;
 import com.lhamacorp.knotes.repository.NoteRepository;
 import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Component;
 
 import java.util.List;
 
+import static java.time.Instant.now;
+import static java.time.temporal.ChronoUnit.DAYS;
+import static org.slf4j.LoggerFactory.getLogger;
+
 @Component
 public class CleanupScheduler {
 
     private final NoteRepository repository;
 
-    private static final String ONCE_PER_DAY_AT_2AM = "0 0 2 * * *";
-    private static final Logger log = LoggerFactory.getLogger(CleanupScheduler.class);
+    private static final String ONCE_PER_DAY_AT_2AM = "* * * * * *";
+    private static final Logger log = getLogger(CleanupScheduler.class);
 
     public CleanupScheduler(NoteRepository repository) {
         this.repository = repository;
@@ -23,8 +26,9 @@ public class CleanupScheduler {
 
     @Scheduled(cron = ONCE_PER_DAY_AT_2AM)
     public void cleanup() {
-        List<Note> emptyNotes = repository.findEmptyNotes();
-        List<String> ids = emptyNotes.stream()
+        List<String> ids = repository.findEmptyNotes()
+            .stream()
+            .filter(note -> note.createdAt().isBefore(now().minus(1, DAYS)))
             .map(Note::id)
             .toList();
 

+ 8 - 10
src/main/java/com/lhamacorp/knotes/service/NoteService.java

@@ -7,19 +7,21 @@ import com.lhamacorp.knotes.domain.Note;
 import com.lhamacorp.knotes.exception.NotFoundException;
 import com.lhamacorp.knotes.repository.NoteRepository;
 import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 import org.springframework.cache.annotation.CacheEvict;
 import org.springframework.cache.annotation.Cacheable;
 import org.springframework.stereotype.Service;
 
 import java.time.Instant;
 
+import static java.time.Instant.now;
+import static org.slf4j.LoggerFactory.getLogger;
+
 @Service
 public class NoteService {
 
     private final NoteRepository repository;
 
-    private static final Logger log = LoggerFactory.getLogger(NoteService.class);
+    private static final Logger log = getLogger(NoteService.class);
 
     public NoteService(NoteRepository repository) {
         this.repository = repository;
@@ -46,13 +48,10 @@ public class NoteService {
     public Note save(String content) {
         Ulid id = UlidCreator.getUlid();
 
-        log.info("Saving new note [{}]", id);
-
-        Instant now = Instant.now();
-        Note savedNote = repository.save(new Note(id.toString(), content, now, now));
+        log.info("Saving note [{}]", id);
 
-        log.debug("Evicting caches for new note [{}]", savedNote.id());
-        return savedNote;
+        Instant now = now();
+        return repository.save(new Note(id.toString(), content, now, now));
     }
 
     @CacheEvict(value = {"contentCache", "metadataCache"}, key = "#id")
@@ -62,8 +61,7 @@ public class NoteService {
 
         log.info("Updating note [{}]", id);
 
-        Instant now = Instant.now();
-        return repository.save(new Note(id, content, note.createdAt(), now));
+        return repository.save(new Note(id, content, note.createdAt(), now()));
     }
 
 }