<commit_before>/*
 * Copyright 2017 flow.ci
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.flow.platform.cc.consumer;

import com.flow.platform.cc.config.QueueConfig;
import com.flow.platform.cc.domain.CmdQueueItem;
import com.flow.platform.cc.exception.AgentErr;
import com.flow.platform.cc.service.AgentService;
import com.flow.platform.cc.service.CmdDispatchService;
import com.flow.platform.cc.service.CmdService;
import com.flow.platform.core.exception.IllegalParameterException;
import com.flow.platform.core.exception.IllegalStatusException;
import com.flow.platform.core.queue.PlatformQueue;
import com.flow.platform.core.queue.QueueListener;
import com.flow.platform.domain.Cmd;
import com.flow.platform.domain.CmdStatus;
import com.flow.platform.util.Logger;
import com.flow.platform.util.zk.ZkException;
import java.time.Duration;
import java.time.Instant;
import javax.annotation.PostConstruct;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * Cmd queue consumer to handle cmd from RabbitMQ
 *
 * @author <EMAIL>
 */
@Component
public class CmdQueueConsumer implements QueueListener<Message> {

    private final static Logger LOGGER = new Logger(CmdQueueConsumer.class);

    @Value("${queue.cmd.idle_agent.period}")
    private Integer idleAgentPeriod; // period for check idle agent in seconds

    @Value("${queue.cmd.idle_agent.timeout}")
    private Integer idleAgentTimeout; // timeout if no idle agent in seconds

    @Autowired
    private CmdService cmdService;

    @Autowired
    private CmdDispatchService cmdDispatchService;

    @Autowired
    private AgentService agentService;

    @Autowired
    private PlatformQueue<Message> cmdQueue;

    @PostConstruct
    public void init() {
        cmdQueue.register(this);
    }

    @Override
    public void onQueueItem(Message message) {
        CmdQueueItem item = CmdQueueItem.parse(message.getBody(), CmdQueueItem.class);
        String cmdId = item.getCmdId();
        LOGGER.trace("Receive a cmd queue item: %s", item);

        try {
            cmdDispatchService.dispatch(cmdId, false);
        } catch (IllegalParameterException e) {
            LOGGER.warn("Illegal cmd id: %s", e.getMessage());
        } catch (IllegalStatusException e) {
            LOGGER.warn("Illegal cmd status: %s", e.getMessage());
        } catch (AgentErr.NotAvailableException | AgentErr.NotFoundException | ZkException.NotExitException e) {
            if (item.getRetry() <= 0) {
                return;
            }

            boolean isTimeout = waitForIdleAgent(cmdId, idleAgentPeriod, idleAgentTimeout);
            if (isTimeout) {
                LOGGER.trace("wait for idle agent time out %s seconds for cmd %s", idleAgentTimeout, cmdId);
            }

            // reset cmd status to pending, record num of retry
            int retry = item.getRetry() - 1;
            Cmd cmd = cmdService.find(cmdId);
            if (cmd.getStatus() == CmdStatus.STOPPED || cmd.getStatus() == CmdStatus.KILLED) {
                return;
            }

            cmd.setStatus(CmdStatus.PENDING);
            cmd.setRetry(retry);
            cmdService.save(cmd);

            // re-enqueue
            resend(item, retry);
<commit_msg>add comments on cmd queue reenequue logic<commit_after>/*
 * Copyright 2017 flow.ci
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.flow.platform.cc.consumer;

import com.flow.platform.cc.config.QueueConfig;
import com.flow.platform.cc.domain.CmdQueueItem;
import com.flow.platform.cc.exception.AgentErr;
import com.flow.platform.cc.service.AgentService;
import com.flow.platform.cc.service.CmdDispatchService;
import com.flow.platform.cc.service.CmdService;
import com.flow.platform.core.exception.IllegalParameterException;
import com.flow.platform.core.exception.IllegalStatusException;
import com.flow.platform.core.queue.PlatformQueue;
import com.flow.platform.core.queue.QueueListener;
import com.flow.platform.domain.Cmd;
import com.flow.platform.domain.CmdStatus;
import com.flow.platform.util.Logger;
import com.flow.platform.util.zk.ZkException;
import java.time.Duration;
import java.time.Instant;
import javax.annotation.PostConstruct;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * Cmd queue consumer to handle cmd from RabbitMQ
 *
 * @author <EMAIL>
 */
@Component
public class CmdQueueConsumer implements QueueListener<Message> {

    private final static Logger LOGGER = new Logger(CmdQueueConsumer.class);

    @Value("${queue.cmd.idle_agent.period}")
    private Integer idleAgentPeriod; // period for check idle agent in seconds

    @Value("${queue.cmd.idle_agent.timeout}")
    private Integer idleAgentTimeout; // timeout if no idle agent in seconds

    @Autowired
    private CmdService cmdService;

    @Autowired
    private CmdDispatchService cmdDispatchService;

    @Autowired
    private AgentService agentService;

    @Autowired
    private PlatformQueue<Message> cmdQueue;

    @PostConstruct
    public void init() {
        cmdQueue.register(this);
    }

    @Override
    public void onQueueItem(Message message) {
        CmdQueueItem item = CmdQueueItem.parse(message.getBody(), CmdQueueItem.class);
        String cmdId = item.getCmdId();
        LOGGER.trace("Receive a cmd queue item: %s", item);

        try {
            cmdDispatchService.dispatch(cmdId, false);
        } catch (IllegalParameterException e) {
            LOGGER.warn("Illegal cmd id: %s", e.getMessage());
        } catch (IllegalStatusException e) {
            LOGGER.warn("Illegal cmd status: %s", e.getMessage());
        } catch (AgentErr.NotAvailableException | AgentErr.NotFoundException | ZkException.NotExitException e) {
            if (item.getRetry() <= 0) {
                return;
            }

            boolean isTimeout = waitForIdleAgent(cmdId, idleAgentPeriod, idleAgentTimeout);
            if (isTimeout) {
                LOGGER.trace("wait for idle agent time out %s seconds for cmd %s", idleAgentTimeout, cmdId);
            }

            Cmd cmd = cmdService.find(cmdId);

            // do not re-enqueue if cmd been stopped or killed
            if (cmd.getStatus() == CmdStatus.STOPPED || cmd.getStatus() == CmdStatus.KILLED) {
                return;
            }

            // reset cmd status to pending, record num of retry
            int retry = item.getRetry() - 1;
            cmd.setStatus(CmdStatus.PENDING);
            cmd.setRetry(retry);
            cmdService.save(cmd);

            // re-enqueue
            resend(item, retry);

{'fraction_non_alphanumeric': 0.09782345828295043, 'fraction_numerical': 0.0024183796856106408, 'mean_word_length': 3.270005162622612, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3638201', 'n_tokens_mistral': 2280, 'n_tokens_neox': 2260, 'n_words': 601}