package cn.reghao.devops.agent.task;

import cn.reghao.devops.common.docker.Docker;
import cn.reghao.devops.common.docker.po.ContainerInfo;
import cn.reghao.devops.common.docker.po.ImageInfo;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author reghao
 * @date 2024-07-29 16:17:47
 */
@Slf4j
public class ImageCleanTask implements Runnable {
    private final Docker docker;

    public ImageCleanTask(Docker docker) {
        this.docker = docker;
    }

    @Override
    public void run() {
        Map<String, ImageInfo> map = docker.images().stream().collect(Collectors.toMap(ImageInfo::getImageId, info -> info));
        List<ContainerInfo> containerInfos = docker.psAll();
        for (ContainerInfo containerInfo : containerInfos) {
            String imageId = containerInfo.getImageId();
            ImageInfo imageInfo = map.remove(imageId);
        }

        //map.keySet().forEach(docker::imageRm);
        log.info("定时扫描 docker 镜像和容器");
    }
}
