pythondjangodjango-rq

Django rq scheduler can not enqueue same task twice


I am using rq scheduler. I want to remind users to verify their email after 2 mins and 10 mins. So I use post_save signal to schedule these tasks. I have set up task like this:

from datetime import timedelta
import django_rq
def send_verification_email(user):
"""Remind signed up user to verify email."""
   
    if not user.is_email_verified:
        context = get_email_context(user)
        context['first_name'] = user.first_name
        context['url'] = django_settings.ACTIVATION_URL.format(**context)
        # below line sends email
        VerifyEmailReminderNotification(user.email, context=context).send()

@receiver(post_save)
def remind_to_verify_email(sender, created, instance, **kwargs):
    """Send verify email for the new user."""
    list_of_models = ('Person', 'Company')
    scheduler = django_rq.get_scheduler("default")
    if sender.__name__ in list_of_models:
        if created:
            scheduler.enqueue_in(timedelta(minutes=2), send_verification_email, instance)
            # if I move below enqueue to "send_verification_email" method it will go to recursion.
            scheduler.enqueue_in(timedelta(minutes=10), send_verification_email, instance)

Problem is: I am getting one mail after 2 mins but not second mail after 10 mins. Any help appreciated.


Solution

  • Run first task with delta 2 minutes and when it executes, it should run another one with delta 8 minutes. Hope that helps.