import time
import logging

from ark.exc import BizExc


logger = logging.getLogger(__name__)


def handle_message(payload, msg):
    try:
        logger.info('amqp handle, payload:{} msg:{}'.format(payload, msg))
        # 幂等、失败重试、全部成功后再ack
    except BizExc as exc:
        # 业务异常不重试(如:并发锁)
        logger.error('handle_message, exc:{}'.format(repr(exc)))
        msg.ack()
    except Exception as exc:
        logger.error('handle_message, exc:{}'.format(repr(exc)))
        msg.requeue()
        time.sleep(1)
    except:  # noqa
        logger.error('handle_message, exc:unknown')
        msg.requeue()
        time.sleep(1)
    else:
        msg.ack()
