WebhookRetryTask.java 1.43 KB
package com.diligrp.rider.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.diligrp.rider.entity.WebhookLog;
import com.diligrp.rider.mapper.WebhookLogMapper;
import com.diligrp.rider.service.WebhookService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
@RequiredArgsConstructor
public class WebhookRetryTask {

    private final WebhookLogMapper webhookLogMapper;
    private final WebhookService webhookService;

    @XxlJob("webhookRetryHandler")
    public void retryFailedWebhooks() {
        try {
            List<WebhookLog> logs = webhookLogMapper.selectList(new LambdaQueryWrapper<WebhookLog>()
                    .eq(WebhookLog::getStatus, 0)
                    .lt(WebhookLog::getRetryCount, 5)
                    .orderByAsc(WebhookLog::getCreateTime)
                    .last("LIMIT 20"));
            for (WebhookLog webhookLog : logs) {
                try {
                    webhookService.retry(webhookLog.getId());
                } catch (Exception e) {
                    log.warn("Webhook 自动重试失败 logId={}", webhookLog.getId(), e);
                }
            }
        } catch (Exception e) {
            log.error("Webhook 重试任务异常", e);
            throw new RuntimeException(e);
        }
    }
}