TimeTaskService.java 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package com.fdkankan.fusion.task;
  2. import cn.hutool.core.date.DateUnit;
  3. import cn.hutool.core.date.DateUtil;
  4. import com.fdkankan.fyun.face.FYunFileServiceInterface;
  5. import com.fdkankan.fyun.util.FileInfoVo;
  6. import lombok.extern.slf4j.Slf4j;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.beans.factory.annotation.Value;
  9. import org.springframework.scheduling.annotation.Scheduled;
  10. import org.springframework.stereotype.Service;
  11. import java.util.Date;
  12. import java.util.List;
  13. @Service
  14. @Slf4j
  15. public class TimeTaskService {
  16. @Autowired
  17. FYunFileServiceInterface fYunFileServiceInterface;
  18. @Value("${spring.profiles.active}")
  19. private String environment;
  20. @Scheduled(cron = "0 0 0 * * ?")
  21. public void scheduledTask() {
  22. delOssOffline();
  23. }
  24. /**
  25. * 定时删除oss中离线包文件
  26. */
  27. private void delOssOffline() {
  28. String path = "fusion/" +environment+ "/offline";
  29. log.info("开始执行清除离线包:{}",path);
  30. try {
  31. List<String> list = fYunFileServiceInterface.listRemoteFiles(path);
  32. for (String key : list) {
  33. FileInfoVo fileInfo = fYunFileServiceInterface.getFileInfo(key);
  34. Long lastModified = fileInfo.getLastModified();
  35. long between = DateUtil.between(new Date(lastModified), new Date(), DateUnit.DAY);
  36. if(between >7){
  37. log.info("删除文件->{},info:{}",key,lastModified);
  38. fYunFileServiceInterface.deleteFile(key);
  39. }
  40. }
  41. }catch (Exception e){
  42. log.info("del-oss-offline-error:{}",e);
  43. }
  44. }
  45. }