Amyth
Amyth

Reputation: 32959

get number of pending tasks for a specific user

In one of my applications i want to limit users to make a only a specific number of document conversion each calendar month and want to notify them of the conversions they've made and number of conversions they can still make in that calendar month.

So I do something like the following.

class CustomUser(models.Model):
    # user fields here

    def get_converted_docs(self):
       return self.document_set.filter(date__range=[start, end]).count()

    def remaining_docs(self):
        converted = self.get_converted_docs()
        return LIMIT - converted

Now, document conversion is done in the background using celery. So there may be a situation when a conversion task is pending, so in that case the above methods would let a user make an extra conversion, because the pending task is not being included in the count.

How can i get the number of tasks pending for a specific CustomUser object here ??

update

ok so i tried the following:

from celery.task.control import inspect

def get_scheduled_tasks():
    tasks = []
    scheduled = inspect().scheduled()
    for task in scheduled.values()
        tasks.extend(task)
    return tasks

This gives me a list of scheduled tasks but now all the values are unicode for the above mentioned task args look like this:

u'args': u'(<Document: test_document.doc>, <CustomUser: Test User>)'

is there a way these can be decoded back to original django objects so that i can filter them ?

Upvotes: 2

Views: 183

Answers (1)

Bernhard Vallant
Bernhard Vallant

Reputation: 50796

Store the state of your documents somewhere else, don't inspect your queue. Either create a seperate model for that, or eg. have a state on your document model, at least independently from your queue. This should have several advantages:

  • Inspecting the queue might be expensive - also depending on the backend for that. And as you see it can also turn out to be difficult.
  • Your queue might not be persistent, if eg. your server crashes and use something like Redis you would loose this information, so it's a good thing to have a log somewhere else to be able to reconstruct the queue)

Upvotes: 2

Related Questions