package cd.casic.module.execute.docker.callback;

import com.github.dockerjava.api.async.ResultCallback;
import com.github.dockerjava.api.model.Statistics;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.concurrent.CompletableFuture;

/**
 * @description: 镜像的状态监控
 * @author: mianbin
 * @date: 2025/5/28 10:45
 * @version: 1.0
 */
@Data
@Slf4j
public class MonitorCallback extends ResultCallback.Adapter<Statistics> {

    private Statistics statistics;
    private CompletableFuture<Statistics> future = new CompletableFuture<>();

    @Override
    public void onNext(Statistics statistics) {
        future.complete(statistics);
        super.onNext(statistics);
        log.info("状态使用情况：{}", statistics);
    }

    @Override
    public void onError(Throwable throwable) {
        super.onError(throwable);
        log.error("docker 拉取异常，{}", throwable.getMessage());
        future.completeExceptionally(throwable);
    }

    @Override
    public void onComplete() {
        super.onComplete();
        log.info("完成：{}", 1);
    }

    @Override
    public void close() throws IOException {
        super.close();
        log.info("关闭：{}", 1);
    }
}
