I'm using Django 2.2 and Celery for periodic tasks
I have the following task configuration
@celery.task(name='payments.recheck_payment_status')
def recheck_payment_status(payment_id):
"""
Recheck payment status
:param payment_id: Payment id for which recheck the status
:return:
"""
logger.info('Checking status for payment %s' % payment_id)
payment = Payment.objects.get(id=payment_id)
if timezone.now() > payment.created + timedelta(days=1):
logger.info('Payment done is more than 1 day. Sending email to the admin')
send_incomplete_payment_email.apply_async(args=(payment_id,))
return
if not payment.completed:
logger.info('Payment status is incomplete. Checking payment status')
payment_ = payment.recheck_payment()
if payment_.completed:
order = payment.order
order.external_reference = payment.external_reference
order.save()
if not payment_.completed:
logger.info('Payment %s is not completed yet.' % payment_id)
recheck_payment_status.apply_async(
args=(payment.id,),
countdown=1800
)
And the task is called by
recheck_payment_status.apply_async(args=(payment_id,), countdown=300)
For a few payments, which failed to check after 300 seconds, are requeued by 1800 seconds again and again.
But the past queue is not cleared and the task send_incomplete_payment_email
is executed multiple times due to the previously scheduled tasks.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…