ParseSchedulerTask.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package com.smppw.modaq.application.task;
  2. import cn.hutool.core.collection.ListUtil;
  3. import cn.hutool.core.date.DateUtil;
  4. import cn.hutool.core.exceptions.ExceptionUtil;
  5. import com.smppw.modaq.application.service.EmailParseApiService;
  6. import com.smppw.modaq.common.conts.EmailTypeConst;
  7. import com.smppw.modaq.domain.entity.TaskRecordDO;
  8. import com.smppw.modaq.domain.service.TaskRecordService;
  9. import jakarta.annotation.PostConstruct;
  10. import org.slf4j.Logger;
  11. import org.slf4j.LoggerFactory;
  12. import org.springframework.scheduling.annotation.EnableScheduling;
  13. import org.springframework.scheduling.annotation.Scheduled;
  14. import org.springframework.stereotype.Component;
  15. import java.util.Date;
  16. @Component
  17. @EnableScheduling
  18. public class ParseSchedulerTask {
  19. private final Logger logger = LoggerFactory.getLogger(this.getClass());
  20. private final EmailParseApiService emailParseApiService;
  21. private final TaskRecordService taskRecordService;
  22. public ParseSchedulerTask(EmailParseApiService emailParseApiService, TaskRecordService taskRecordService) {
  23. this.emailParseApiService = emailParseApiService;
  24. this.taskRecordService = taskRecordService;
  25. }
  26. @PostConstruct
  27. public void executeOnStartup() {
  28. try {
  29. // 定期报告从 我的文件夹.报告公告 文件夹获取邮件
  30. this.emailParseApiService.parseEmail(
  31. DateUtil.parseDateTime("2025-05-12 11:00:00"),
  32. DateUtil.parseDateTime("2025-05-12 11:03:00"),
  33. ListUtil.of("其他文件夹/报告公告"), EmailTypeConst.REPORT_EMAIL_TYPES);
  34. } catch (Exception e) {
  35. logger.error(ExceptionUtil.getMessage(e));
  36. }
  37. }
  38. /**
  39. * 定时任务每11分钟执行一次
  40. */
  41. @Scheduled(cron = "0 */11 * * * ?")
  42. public void letter() {
  43. String taskKey = "mo_email_parse_letter_task";
  44. TaskRecordDO task = this.taskRecordService.getTaskRecord(taskKey, 11 * 2 * 60);
  45. if (task == null) {
  46. return;
  47. }
  48. long start = System.currentTimeMillis();
  49. Date now = new Date();
  50. try {
  51. // 尽可能往前找11分钟覆盖可能遗漏的邮件
  52. Date startTime = DateUtil.offsetMinute(task.getStartTime(), -11);
  53. // 确认单从 INBOX 默认文件夹获取邮件
  54. this.emailParseApiService.parseEmail(startTime, now,
  55. null, ListUtil.of(EmailTypeConst.REPORT_LETTER_EMAIL_TYPE));
  56. task.setStatus(1);
  57. } catch (Exception e) {
  58. task.setStatus(2);
  59. task.setErrMsg(ExceptionUtil.stacktraceToString(e));
  60. this.logger.error("任务{} 执行错误:{}", taskKey, ExceptionUtil.stacktraceToString(e));
  61. } finally {
  62. task.setEndTime(now);
  63. this.taskRecordService.updateStatus(task);
  64. if (this.logger.isInfoEnabled()) {
  65. this.logger.info("任务{} 执行完成,耗时:{}ms", taskKey, System.currentTimeMillis() - start);
  66. }
  67. }
  68. }
  69. /**
  70. * 定时任务每60分钟执行一次
  71. */
  72. @Scheduled(cron = "0 15 */1 * * ?")
  73. public void report() {
  74. String taskKey = "mo_email_parser_report_task";
  75. TaskRecordDO task = this.taskRecordService.getTaskRecord(taskKey, 60 * 2 * 60);
  76. if (task == null) {
  77. return;
  78. }
  79. long start = System.currentTimeMillis();
  80. Date now = new Date();
  81. try {
  82. // 尽可能往前找50分钟覆盖可能遗漏的邮件
  83. Date startTime = DateUtil.offsetMinute(task.getStartTime(), -50);
  84. // 定期报告从 我的文件夹.报告公告 文件夹获取邮件
  85. this.emailParseApiService.parseEmail(startTime, now,
  86. ListUtil.of("其他文件夹/报告公告"), EmailTypeConst.REPORT_EMAIL_TYPES);
  87. task.setStatus(1);
  88. } catch (Exception e) {
  89. task.setStatus(2);
  90. task.setErrMsg(ExceptionUtil.stacktraceToString(e));
  91. this.logger.error("定期报告解析任务{} 执行错误:{}", taskKey, ExceptionUtil.stacktraceToString(e));
  92. } finally {
  93. task.setEndTime(now);
  94. this.taskRecordService.updateStatus(task);
  95. if (this.logger.isInfoEnabled()) {
  96. this.logger.info("定期报告解析任务{} 执行完成,耗时:{}ms", taskKey, System.currentTimeMillis() - start);
  97. }
  98. }
  99. }
  100. }