protected String checkQueue()

in ignite-tc-helper-web/src/main/java/org/apache/ignite/ci/jobs/CheckQueueJob.java [199:284]


    protected String checkQueue(String srvCode, List<ITrackedChain> chains) {
        ITeamcityIgnited tcIgn = tcIgnitedProv.server(srvCode, creds);

        int queueSize = tcIgn.queueSize();

        if (queueSize > CHECK_MAX_QUEUE_SIZE) {
            String msg = MessageFormat.format("TeamCity queue is too big. No new builds will be triggered. Size of queue: {0}, TeamCity server: {1}", queueSize, srvCode);

            logger.info(msg);

            return msg;
        }

        List<Agent> agents = tcIgn.agents(true, true);

        int total = agents.size();
        int winAgents = 0;
        int running = 0;
        int winRunning = 0;

        for (Agent agent : agents) {
            //filter for windows agents
            if (agent.getProperties() != null &&
                    agent.getPool().getName().contains("Default") &&
                    agent.isEnabled() &&
                    agent.getProperties().getProperty().stream()
                    .filter(prop -> prop.getName().equals("teamcity.agent.jvm.os.name")).findAny().orElseGet(() -> {
                        Property emptyProp = new Property();

                        emptyProp.setValue("");

                        return emptyProp;
                    }).getValue().contains("Windows")) {
                winAgents++;

                if (agent.getBuild() != null)
                    winRunning++;
            }

            if (agent.getBuild() != null) //  || !STATE_RUNNING.equals(agent.getFatBuild().status)
                ++running;
        }

        int free = total == 0 ? -1 : (total - running) * 100 / total;

        String allAgentStatus = MessageFormat.format("{0}% of agents are free ({1} total, {2} running builds).", free, total, running);

        logger.info(allAgentStatus);

        if (free < CHECK_QUEUE_MIN_FREE_AGENTS_PERCENT)
            return "Min agent percent of free agents not met:" + allAgentStatus;

        logger.info("There are more than {}% free agents (total={}, free={}).", CHECK_QUEUE_MIN_FREE_AGENTS_PERCENT,
            total, total - running);

        int winFree = winAgents == 0 ? -1 : (winAgents - winRunning) * 100 / winAgents;

        String windowsAgentStatus = MessageFormat.format("{0}% of Windows agents are free ({1} total, {2} running builds).", winFree, winAgents, winRunning);

        logger.info(windowsAgentStatus);

        if (winAgents > 0 && winFree < CHECK_QUEUE_MIN_FREE_WINDOWS_AGENTS_PERCENT)
            return "Min agent percent of free Windows agents not met:" + windowsAgentStatus;

        logger.info("There are more than {}% free Windows agents (total={}, free={}).", CHECK_QUEUE_MIN_FREE_WINDOWS_AGENTS_PERCENT,
            winAgents, winAgents - winRunning);

        String selfLogin = creds.getUser(srvCode);

        tcIgn.actualizeRecentBuildRefs();

        StringBuilder res = new StringBuilder();

        for (ITrackedChain chain : chains) {
            if (!Objects.equals(chain.serverCode(), srvCode))
                continue;

            String agentStatus = allAgentStatus + " " + windowsAgentStatus;

            String chainRes = checkIfChainTriggerable(chain.serverCode(), chain.tcSuiteId(), chain.tcBranch(), tcIgn, selfLogin, chain, agentStatus);

            res.append(chainRes).append("; ");
        }

        return res.toString();
    }