package com.Modules.etl.handler;


import lombok.extern.slf4j.Slf4j;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class ETLRetryHandler {

    /**
     * 带重试机制的ETL任务执行
     */
    @Retryable(value = Exception.class, maxAttempts = 3, backoff = @Backoff(delay = 5000))
    public void executeWithRetry(Runnable etlTask) {
        try {
            etlTask.run();
        } catch (Exception e) {
            log.warn("ETL任务执行失败，准备重试", e);
            throw e;
        }
    }

    /**
     * 重试失败后的恢复处理
     */
    @Recover
    public void recover(Exception e, Runnable etlTask) {
        log.error("ETL任务重试多次后仍然失败", e);
        // 这里可以发送告警通知、记录失败状态等
        throw new RuntimeException("ETL任务最终执行失败", e);
    }
}