Skip to content

Commit

Permalink
feat: support automatic cleaning recycled posts. halo-dev#1031 (halo-…
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiteDG authored Oct 25, 2020
1 parent b5f7425 commit ff734ed
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/main/java/run/halo/app/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import run.halo.app.repository.base.BaseRepositoryImpl;

/**
Expand All @@ -17,6 +18,7 @@
*/
@SpringBootApplication(exclude = { MultipartAutoConfiguration.class })
@EnableAsync
@EnableScheduling
@EnableJpaRepositories(basePackages = "run.halo.app.repository", repositoryBaseClass = BaseRepositoryImpl.class)
public class Application extends SpringBootServletInitializer {

Expand Down
35 changes: 35 additions & 0 deletions src/main/java/run/halo/app/model/enums/TimeUnit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package run.halo.app.model.enums;

/**
* @author Wh1te
* @date 2020-10-19
*/
public enum TimeUnit implements ValueEnum<Integer> {

/**
* 天
*/
DAY(0),

/**
* 小时
*/
HOUR(1);

private final Integer value;

TimeUnit(Integer value) {
this.value = value;
}

/**
* Get enum value.
*
* @return enum value
*/
@Override
public Integer getValue() {
return value;
}
}

19 changes: 18 additions & 1 deletion src/main/java/run/halo/app/model/properties/PostProperties.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package run.halo.app.model.properties;

import run.halo.app.model.enums.TimeUnit;

/**
* Post properties.
*
Expand Down Expand Up @@ -37,7 +39,22 @@ public enum PostProperties implements PropertyEnum {
/**
* Post index sort.
*/
INDEX_SORT("post_index_sort", String.class, "createTime");
INDEX_SORT("post_index_sort", String.class, "createTime"),

/**
* Enable auto cleaning recycled post.
*/
RECYCLED_POST_CLEANING_ENABLED("recycled_post_cleaning_enabled", Boolean.class, "false"),

/**
* Recycled post retention time
*/
RECYCLED_POST_RETENTION_TIME("recycled_post_retention_time", Integer.class, "30"),

/**
* Recycled post retention time unit.
*/
RECYCLED_POST_RETENTION_TIMEUNIT("recycled_post_retention_timeunit", TimeUnit.class, TimeUnit.DAY.name());

private final String value;

Expand Down
82 changes: 82 additions & 0 deletions src/main/java/run/halo/app/task/RecycledPostCleaningTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package run.halo.app.task;

import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import run.halo.app.model.entity.BasePost;
import run.halo.app.model.entity.Post;
import run.halo.app.model.enums.PostStatus;
import run.halo.app.model.enums.TimeUnit;
import run.halo.app.model.properties.PostProperties;
import run.halo.app.service.OptionService;
import run.halo.app.service.PostService;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* @author Wh1te
* @date 2020-10-19
*/
@Slf4j
@Component
public class RecycledPostCleaningTask {

private final OptionService optionService;

private final PostService postService;

public RecycledPostCleaningTask(OptionService optionService, PostService postService) {
this.optionService = optionService;
this.postService = postService;
}

/**
* Clean recycled posts if RECYCLED_POST_CLEANING_ENABLED is true
*/
@Scheduled(cron = "0 0 */1 * * *")
public synchronized void run() {
Boolean recycledPostCleaningEnabled = optionService.getByPropertyOrDefault(PostProperties.RECYCLED_POST_CLEANING_ENABLED, Boolean.class, false);
log.debug("{} = {}", PostProperties.RECYCLED_POST_CLEANING_ENABLED.getValue(), recycledPostCleaningEnabled);
if (!recycledPostCleaningEnabled) {
return;
}

Integer recycledPostRetentionTime = optionService.getByPropertyOrDefault(PostProperties.RECYCLED_POST_RETENTION_TIME, Integer.class, PostProperties.RECYCLED_POST_RETENTION_TIME.defaultValue(Integer.class));
TimeUnit timeUnit = optionService.getEnumByPropertyOrDefault(PostProperties.RECYCLED_POST_RETENTION_TIMEUNIT, TimeUnit.class, TimeUnit.DAY);
log.debug("{} = {}", PostProperties.RECYCLED_POST_RETENTION_TIME.getValue(), recycledPostRetentionTime);
log.debug("{} = {}", PostProperties.RECYCLED_POST_RETENTION_TIMEUNIT.getValue(), Objects.requireNonNull(timeUnit).name());

long expiredIn;
switch (timeUnit) {
case HOUR:
expiredIn = recycledPostRetentionTime;
break;
case DAY:
default:
expiredIn = recycledPostRetentionTime * 24;
break;
}
List<Post> recyclePost = postService.listAllBy(PostStatus.RECYCLE);
LocalDateTime now = LocalDateTime.now();
List<Integer> ids = recyclePost.stream().filter(post -> {
LocalDateTime updateTime = DateUtil.toLocalDateTime(post.getUpdateTime());
long until = updateTime.until(now, ChronoUnit.HOURS);
return until >= expiredIn;
}).map(BasePost::getId).collect(Collectors.toList());

if (CollectionUtils.isEmpty(ids)) {
return;
}

log.info("Start cleaning recycled posts");
List<Post> posts = postService.removeByIds(ids);
log.info("Recycled posts cleaning has been completed, {} posts has been permanently deleted", posts.size());
}

}

0 comments on commit ff734ed

Please sign in to comment.