2

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 ?

4

1 回答 1

2

将文档的状态存储在其他地方,不要检查您的队列。要么为此创建一个单独的模型,要么例如。在您的文档模型上有一个状态,至少独立于您的队列。这应该有几个优点:

  • 检查队列可能很昂贵 - 也取决于后端。正如你所看到的,它也可能变得很困难。
  • 例如,您的队列可能不是持久的。您的服务器崩溃并使用 Redis 之类的东西,您会丢失此信息,因此在其他地方有一个日志以便能够重建队列是一件好事)
于 2013-06-03T10:23:50.333 回答