|
@@ -0,0 +1,65 @@
|
|
|
|
|
+package com.etotem.cfc.task;
|
|
|
|
|
+
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
|
+import com.etotem.cfc.config.DatabaseInitializer;
|
|
|
|
|
+import com.etotem.cfc.entity.Task;
|
|
|
|
|
+import com.etotem.cfc.mapper.TaskMapper;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
|
+
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
|
+import java.util.Date;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 任务过期扫描器(迁移509)。
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>每分钟把"执行窗口已过但仍未完成"的任务标记过期:写 expired_at + status=overdue。
|
|
|
|
|
+ * overdue 不在 getTodayTasks 的白名单内 → 从今日列表隐藏,历史可查;
|
|
|
|
|
+ * 提醒调度器只捞 pending → 过期任务不再被提醒,避免持续推送。</p>
|
|
|
|
|
+ */
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+@Component
|
|
|
|
|
+public class TaskExpiryScheduledTask {
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private TaskMapper taskMapper;
|
|
|
|
|
+
|
|
|
|
|
+ @Scheduled(cron = "0 * * * * ?")
|
|
|
|
|
+ public void markExpiredTasks() {
|
|
|
|
|
+ try {
|
|
|
|
|
+ if (!DatabaseInitializer.isMigrationsComplete()) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ Date now = new Date();
|
|
|
|
|
+ // 过执行窗口:优先 latestEnd,未配置则退回 deadline;两者都无则不判定过期
|
|
|
|
|
+ List<Task> candidates = taskMapper.selectList(new LambdaQueryWrapper<Task>()
|
|
|
|
|
+ .eq(Task::getStatus, "pending")
|
|
|
|
|
+ .isNotNull(Task::getDeadline)
|
|
|
|
|
+ .le(Task::getDeadline, now)
|
|
|
|
|
+ .last("LIMIT 500"));
|
|
|
|
|
+ int marked = 0;
|
|
|
|
|
+ for (Task t : candidates) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ Date windowEnd = t.getLatestEnd() != null ? t.getLatestEnd() : t.getDeadline();
|
|
|
|
|
+ if (windowEnd == null || now.before(windowEnd)) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ t.setExpiredAt(now);
|
|
|
|
|
+ t.setStatus("overdue");
|
|
|
|
|
+ t.setUpdatedAt(now);
|
|
|
|
|
+ taskMapper.updateById(t);
|
|
|
|
|
+ marked++;
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.warn("标记任务过期失败 taskId={}: {}", t.getId(), e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if (marked > 0) {
|
|
|
|
|
+ log.info("任务过期扫描:标记{}条", marked);
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.warn("任务过期扫描异常: {}", e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|