Skip to content

Commit

Permalink
feat: add scheduler for purging expired records
Browse files Browse the repository at this point in the history
  • Loading branch information
neumanf committed Mar 4, 2024
1 parent ee2a2f2 commit 75b3859
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
package com.mally.api.pastebin.repositories;

import com.mally.api.pastebin.entities.Paste;
import jakarta.transaction.Transactional;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.ZonedDateTime;
import java.util.Optional;

@Repository
public interface PastebinRepository extends CrudRepository<Paste, Long> {
Optional<Paste> findBySlug(String slug);

@Transactional
@Modifying
@Query("DELETE FROM Paste p WHERE p.expiresAt < :now")
void deleteExpiredPastes(@Param("now") ZonedDateTime now);
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,8 @@ public Paste create(CreatePasteDTO dto) {

return pastebinRepository.save(paste);
}

public void deleteExpiredPastes() {
pastebinRepository.deleteExpiredPastes(ZonedDateTime.now());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.mally.api.shared.schedulers;


import com.mally.api.pastebin.services.PastebinService;
import com.mally.api.urlshortener.services.UrlShortenerService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@EnableScheduling
@AllArgsConstructor
@Slf4j
@Component
public class ExpiredRecordsPurger {
static private final String AT_MIDNIGHT_CRON = "0 0 * * * *";

private final UrlShortenerService urlShortenerService;

private final PastebinService pastebinService;

@Scheduled(cron = AT_MIDNIGHT_CRON)
public void purgeExpiredRecords() {
deleteExpiredURLs();
deleteExpiredPastes();

log.info("Expired records purged.");
}

private void deleteExpiredURLs() {
urlShortenerService.deleteExpiredURLs();
}

private void deleteExpiredPastes() {
pastebinService.deleteExpiredPastes();
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
package com.mally.api.urlshortener.repositories;

import com.mally.api.urlshortener.entities.Url;
import jakarta.transaction.Transactional;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.ZonedDateTime;
import java.util.Optional;

@Repository
public interface UrlShortenerRepository extends CrudRepository<Url, Long> {
Optional<Url> findBySlug(String slug);

@Transactional
@Modifying
@Query("DELETE FROM Url u WHERE u.expiresAt < :now")
void deleteExpiredURLs(@Param("now") ZonedDateTime now);
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,8 @@ public Url save(ShortenUrlDTO dto) {

return urlShortenerRepository.save(url);
}

public void deleteExpiredURLs() {
urlShortenerRepository.deleteExpiredURLs(ZonedDateTime.now());
}
}

0 comments on commit 75b3859

Please sign in to comment.