Delete CLOSED posts only a week after closing; track closedAt
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -25,6 +25,8 @@ public class PostResponse {
|
||||
private PostType type;
|
||||
private PostStatus status;
|
||||
private LocalDateTime createdAt;
|
||||
/** Когда пост закрыт (status = CLOSED); фронт показывает дату «найдено». */
|
||||
private LocalDateTime closedAt;
|
||||
/** Логин автора. */
|
||||
private String userLogin;
|
||||
private String phone;
|
||||
|
||||
@@ -58,6 +58,11 @@ public class Post {
|
||||
@Enumerated(EnumType.STRING)
|
||||
private PostStatus status;
|
||||
|
||||
// Когда пост перешёл в CLOSED; по нему SchedulerService удаляет закрытые
|
||||
// больше недели назад. Ведётся в setStatus — менять напрямую не нужно.
|
||||
@Setter(lombok.AccessLevel.NONE)
|
||||
private LocalDateTime closedAt;
|
||||
|
||||
@ManyToOne
|
||||
@JsonIgnore
|
||||
private User user;
|
||||
@@ -67,4 +72,19 @@ public class Post {
|
||||
@OrderBy("id ASC")
|
||||
private List<Image> images = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* Меняет статус и ведёт closedAt: переход в CLOSED ставит время закрытия,
|
||||
* уход из CLOSED (переоткрытие, правка с повторной модерацией) его сбрасывает.
|
||||
* Повторный CLOSED у уже закрытого поста время не сдвигает.
|
||||
*/
|
||||
public void setStatus(PostStatus status) {
|
||||
if (status == PostStatus.CLOSED) {
|
||||
if (this.status != PostStatus.CLOSED || closedAt == null) {
|
||||
closedAt = LocalDateTime.now();
|
||||
}
|
||||
} else {
|
||||
closedAt = null;
|
||||
}
|
||||
this.status = status;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ public interface PostRepository extends JpaRepository<Post, Long>, JpaSpecificat
|
||||
|
||||
Page<Post> findByUserIdAndStatusIn(Long userId, Collection<PostStatus> statuses, Pageable pageable);
|
||||
|
||||
List<Post> findByStatus(PostStatus status);
|
||||
List<Post> findByStatusAndClosedAtBefore(PostStatus status, LocalDateTime closedBefore);
|
||||
|
||||
List<Post> findByCreatedAtBefore(LocalDateTime date);
|
||||
|
||||
|
||||
@@ -27,14 +27,17 @@ public class SchedulerService {
|
||||
/** Меньше lockAtMostFor задачи модерации (10 минут). */
|
||||
private static final Duration MODERATION_RUN_BUDGET = Duration.ofMinutes(8);
|
||||
|
||||
/** Сколько закрытый пост остаётся в ленте, прежде чем его удалит ночная задача. */
|
||||
static final Duration CLOSED_POST_TTL = Duration.ofDays(7);
|
||||
|
||||
private final PostRepository postRepository;
|
||||
private final PostModerationExecutorService postModerationExecutorService;
|
||||
private final PostDeletionService postDeletionService;
|
||||
|
||||
/**
|
||||
* Runs every day at 00:01 to delete:
|
||||
* 1. Posts with CLOSED status
|
||||
* 2. Posts older than 6 months
|
||||
* 1. Posts with CLOSED status closed more than a week ago (by closedAt)
|
||||
* 2. Posts older than 6 months (any status)
|
||||
*
|
||||
* Удаление идёт через PostDeletionService: вместе с постами чистятся избранное,
|
||||
* ссылки из чатов и файлы в MinIO (раньше файлы оставались мусором в бакете).
|
||||
@@ -45,9 +48,11 @@ public class SchedulerService {
|
||||
public void deleteExpiredPosts() {
|
||||
log.info("Starting scheduled cleanup of expired posts...");
|
||||
|
||||
LocalDateTime sixMonthsAgo = LocalDateTime.now().minusMonths(6);
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
LocalDateTime sixMonthsAgo = now.minusMonths(6);
|
||||
LocalDateTime closedBefore = now.minus(CLOSED_POST_TTL);
|
||||
|
||||
List<Post> closedPosts = postRepository.findByStatus(PostStatus.CLOSED);
|
||||
List<Post> closedPosts = postRepository.findByStatusAndClosedAtBefore(PostStatus.CLOSED, closedBefore);
|
||||
List<Post> oldPosts = postRepository.findByCreatedAtBefore(sixMonthsAgo);
|
||||
|
||||
// Закрытый пост бывает и старым — удаляем каждый один раз
|
||||
@@ -55,7 +60,7 @@ public class SchedulerService {
|
||||
.collect(Collectors.toMap(Post::getId, Function.identity(), (a, b) -> a, LinkedHashMap::new));
|
||||
|
||||
postDeletionService.delete(new ArrayList<>(expired.values()));
|
||||
log.info("Scheduled cleanup completed: closed={}, older than 6 months={}, deleted={}",
|
||||
log.info("Scheduled cleanup completed: closed more than a week ago={}, older than 6 months={}, deleted={}",
|
||||
closedPosts.size(), oldPosts.size(), expired.size());
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
databaseChangeLog:
|
||||
- changeSet:
|
||||
id: 022-add-closed-at-posts
|
||||
author: krylov
|
||||
comment: >
|
||||
Время перехода поста в CLOSED. Ночная задача удаляет закрытые посты,
|
||||
у которых closed_at старше недели (раньше удалялись все CLOSED сразу).
|
||||
preConditions:
|
||||
onFail: MARK_RAN
|
||||
not:
|
||||
columnExists:
|
||||
tableName: posts
|
||||
columnName: closed_at
|
||||
changes:
|
||||
- addColumn:
|
||||
tableName: posts
|
||||
columns:
|
||||
- column: { name: closed_at, type: TIMESTAMP }
|
||||
@@ -34,3 +34,4 @@ databaseChangeLog:
|
||||
- include: { file: db/changelog/search/019-posts-full-text-search.yaml }
|
||||
- include: { file: db/changelog/search/020-search-words.yaml }
|
||||
- include: { file: db/changelog/add/021-ad-stats-indexes.yaml }
|
||||
- include: { file: db/changelog/add/022-add-closed-at-posts.yaml }
|
||||
|
||||
Reference in New Issue
Block a user