package net.educoder.ecsonar.task;

import net.educoder.ecsonar.constant.Constant;
import net.educoder.ecsonar.services.DbOperateService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Author: youys
 * @Date: 2022/7/5
 * @Description:
 */
public class CloudBrainQueryResultRunnable implements Runnable {

    private final Logger LOGGER = LoggerFactory.getLogger(CloudBrainQueryResultRunnable.class);


    private String projectName;
    private String detailId;
    private DbOperateService dbOperateService;

    public CloudBrainQueryResultRunnable(String projectName, String detailId, DbOperateService dbOperateService) {
        this.projectName = projectName;
        this.detailId = detailId;
        this.dbOperateService = dbOperateService;
    }

    @Override
    public void run() {

        int count = 0;
        String status;
        while ((status = dbOperateService.queryCeActivityStatus(projectName)) == null && count++ <= 60) {
            LOGGER.info("CloudBrainQueryResultRunnable#detailId:[{}],sonar还未执行完,次数:{}", detailId, count);
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
            }
        }

        // 查询
        LOGGER.info("CloudBrainQueryResultRunnable#detailId:[{}], 查询sonar执行状态[{}]", detailId, status);
        dbOperateService.updateCloudTaskInfoDetail(detailId, Constant.SUCCESS.equals(status) ? 1 : -1);
    }
}
