定时推送账单添加执行日志;
This commit is contained in:
+19
-9
@@ -14,6 +14,7 @@ import java.time.LocalDateTime;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Component
|
||||
public class BillingScheduler {
|
||||
@@ -37,18 +38,27 @@ public class BillingScheduler {
|
||||
|
||||
List<ScheduledTask> tasks = scheduledTaskMapper.findPendingTasks(
|
||||
start, now, "PENDING", 100);
|
||||
|
||||
List<ScheduledTaskLog> taskLogs = scheduledTaskLogMapper.findTasks();
|
||||
for (ScheduledTask task : tasks) {
|
||||
try {
|
||||
// 乐观锁更新状态,防止重复执行
|
||||
int updated = scheduledTaskMapper.updateStatusIfPending(
|
||||
task.getId(), "PENDING", "PROCESSING");
|
||||
Long taskId = task.getId();
|
||||
List<ScheduledTaskLog> matchedLogs = taskLogs.stream()
|
||||
.filter(log -> taskId.equals(log.getTaskId()))
|
||||
.collect(Collectors.toList());
|
||||
if (matchedLogs.size() > 0) {
|
||||
//如果本月已经执行过跳过
|
||||
continue;
|
||||
} else {
|
||||
// 乐观锁更新状态,防止重复执行
|
||||
int updated = scheduledTaskMapper.updateStatusIfPending(
|
||||
task.getId(), "PENDING", "PROCESSING");
|
||||
|
||||
if (updated > 0) {
|
||||
// 异步执行具体业务
|
||||
CompletableFuture.runAsync(() -> {
|
||||
executeTask(task);
|
||||
});
|
||||
if (updated > 0) {
|
||||
// 异步执行具体业务
|
||||
CompletableFuture.runAsync(() -> {
|
||||
executeTask(task);
|
||||
});
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
scheduledTaskMapper.updateStatus(task.getId(), "FAILED");
|
||||
|
||||
+1
-1
@@ -10,5 +10,5 @@ import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
public interface ScheduledTaskLogMapper extends BaseMapper<ScheduledTaskLog> {
|
||||
|
||||
List<ScheduledTaskLog> findTasks();
|
||||
}
|
||||
Reference in New Issue
Block a user