2

I have a celery tasks in my application but when I try to query the database in the functions, some of them returns Does not exist or None


@shared_task
def email_on_assign_reviewer(submission, reviewerlist):
    """
    Send an email to the Reviewer when they are assigned to review
    """
    print(submission)   # prints normally
    print(reviewerlist) # this prints the list of id of reviewers perfectly  
    submission = Submission.objects.get(id=submission)
    print(submission)  # this prints the submission perfectly  
    
    context = {"some": "context"}
    
    for id in reviewerlist:
        reviewer = Reviewer.objects.filter(id=id).first()
        print(reviewer)  #this returns none 
        if reviewer:
            context['reviewer_name'] = reviewer.user.first_name
            utils.sendEmail(
                context,
                template="review_assign.html",
                to=[reviewer.user.email]
            )

MY VIEW

    def perform_create(self, serializer):
        submission = Submission.objects.filter(id=self.kwargs['submission_pk']).first()

        *SOME LOGIC*

        data = serializer.save()
        for i in reviewerlist:
            print(Reviewer.objects.filter(id=i).first())  # this prints the reviewers perfectly 
        
        tasks.email_on_assign_reviewer.delay(submission.id, reviewerlist)

MY SERIALIZER

def create(self, validated_data):
        submission_id = self.context['submission_id']
        user_ids = validated_data.pop('user_ids', [])
        existing_profile_ids = Somequery.objects.all()
        for user_id in user_ids:
                if user_id not in existing_profile_ids:
                    reviewer = Reviewer.objects.create(submission_id=submission_id, user_id=user_id, )
                    reviewers_to_create.append(reviewer.pk)
                    
        return reviewers_to_create

In some tasks, the submission returns DoesnotExist

5
  • Is there a specific use case or scenario in which you are getting DoesNotExists? Have you observed the similarities between them? It will not happen randomly. Commented Mar 4 at 5:03
  • I haven't been able to figure out this pattern. Could it be because I am using docker? I ran shell on the docker and queried my DB for the reviewer and it returned None. Now I wonder how it is being normal retrieved in the Views Commented Mar 4 at 6:08
  • So where do you schedule the celery tasks? Commented Mar 4 at 6:37
  • It sounds like you are using different database for view and celery task OR your changes are not saved to DB since you are getting the same result in shell. Try providing more logs or code examples Commented Mar 4 at 10:51
  • Do you have autocommit turned on in the settings? If not, there could be a race condition here, where the celery task starts before the Django transaction is committed. Commented Mar 8 at 19:18

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.