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 に答える