|
@@ -1,8 +1,13 @@
|
|
package com.smppw.modaq.application.task;
|
|
package com.smppw.modaq.application.task;
|
|
|
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
|
+import cn.hutool.core.exceptions.ExceptionUtil;
|
|
import com.smppw.modaq.application.service.EmailParseApiService;
|
|
import com.smppw.modaq.application.service.EmailParseApiService;
|
|
|
|
+import com.smppw.modaq.domain.entity.TaskRecordDO;
|
|
|
|
+import com.smppw.modaq.domain.service.TaskRecordService;
|
|
import jakarta.annotation.PostConstruct;
|
|
import jakarta.annotation.PostConstruct;
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
@@ -12,10 +17,14 @@ import java.util.Date;
|
|
@Component
|
|
@Component
|
|
@EnableScheduling
|
|
@EnableScheduling
|
|
public class ParseSchedulerTask {
|
|
public class ParseSchedulerTask {
|
|
|
|
+ private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
|
|
|
+
|
|
private final EmailParseApiService emailParseApiService;
|
|
private final EmailParseApiService emailParseApiService;
|
|
|
|
+ private final TaskRecordService taskRecordService;
|
|
|
|
|
|
- public ParseSchedulerTask(EmailParseApiService emailParseApiService) {
|
|
|
|
|
|
+ public ParseSchedulerTask(EmailParseApiService emailParseApiService, TaskRecordService taskRecordService) {
|
|
this.emailParseApiService = emailParseApiService;
|
|
this.emailParseApiService = emailParseApiService;
|
|
|
|
+ this.taskRecordService = taskRecordService;
|
|
}
|
|
}
|
|
|
|
|
|
@PostConstruct
|
|
@PostConstruct
|
|
@@ -26,9 +35,29 @@ public class ParseSchedulerTask {
|
|
/**
|
|
/**
|
|
* 定时任务每2小时执行一次
|
|
* 定时任务每2小时执行一次
|
|
*/
|
|
*/
|
|
- @Scheduled(cron = "0 0 */2 * * ?")
|
|
|
|
|
|
+ @Scheduled(cron = "0 0 */1 * * ?")
|
|
public void run() {
|
|
public void run() {
|
|
- Date preDay = DateUtil.offsetMinute(new Date(), -121);
|
|
|
|
- this.emailParseApiService.parseEmail(preDay, new Date());
|
|
|
|
|
|
+ String taskKye = "mo_email_parser_task";
|
|
|
|
+ TaskRecordDO task = this.taskRecordService.getTaskRecord(taskKye);
|
|
|
|
+ if (task == null) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
|
+ Date now = new Date();
|
|
|
|
+ try {
|
|
|
|
+ Date startTime = DateUtil.offsetMinute(task.getStartTime(), -2);
|
|
|
|
+ this.emailParseApiService.parseEmail(startTime, now);
|
|
|
|
+ task.setStatus(1);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ task.setStatus(2);
|
|
|
|
+ task.setErrMsg(ExceptionUtil.stacktraceToString(e));
|
|
|
|
+ this.logger.error("任务{} 执行错误:{}", taskKye, ExceptionUtil.stacktraceToString(e));
|
|
|
|
+ } finally {
|
|
|
|
+ task.setEndTime(now);
|
|
|
|
+ this.taskRecordService.updateStatus(task);
|
|
|
|
+ if (this.logger.isInfoEnabled()) {
|
|
|
|
+ this.logger.info("任务{} 执行完成,耗时:{}ms", taskKye, System.currentTimeMillis() - start);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|