Files
along_django/dingdan/tongzhi_tasks.py

43 lines
1.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# dingdan/tongzhi_tasks.py
import logging
from django.conf import settings
from utils.xcx_sys_config import wx_cfg
from a_long_dianjing.celery import app
from utils.weixin_broadcast import WeixinBroadcastSender
logger = logging.getLogger('weixin_broadcast')
@app.task(
bind=True,
name='dingdan.tongzhi_tasks.dingdan_guangbo',
max_retries=3,
default_retry_delay=30,
queue='broadcast',
soft_time_limit=240,
time_limit=300,
acks_late=True,
reject_on_worker_lost=True,
)
def dingdan_guangbo(self, order_info):
"""
Celery并发向所有关注服务号的用户推送订单模板消息
"""
if not wx_cfg.WEIXIN_BROADCAST_ENABLED:
return {'success': True, 'msg': '广播已关闭'}
order_id = order_info.get('dingdan_id')
try:
sender = WeixinBroadcastSender()
result = sender.broadcast_parallel(order_info)
if not result.get('success') and self.request.retries < self.max_retries:
raise self.retry(countdown=30)
return result
except Exception as e:
logger.error(f'广播任务异常 订单={order_id}: {e}', exc_info=True)
if self.request.retries < self.max_retries:
raise self.retry(exc=e)
return {'success': False, 'msg': str(e), 'order_id': order_id}