Merge branch 'production'
[wolnelektury.git] / apps / waiter / models.py
index 59eeea6..10f9289 100644 (file)
@@ -1,14 +1,14 @@
 from os.path import join, isfile
 from django.core.urlresolvers import reverse
 from django.db import models
-from djcelery.models import TaskMeta
-from waiter.settings import WAITER_URL
+from waiter.settings import WAITER_URL, WAITER_MAX_QUEUE
 from waiter.utils import check_abspath
 from picklefield import PickledObjectField
 
 
 class WaitedFile(models.Model):
     path = models.CharField(max_length=255, unique=True, db_index=True)
+    task_id = models.CharField(max_length=128, db_index=True, null=True, blank=True)
     task = PickledObjectField(null=True, editable=False)
     description = models.CharField(max_length=255, null=True, blank=True)
 
@@ -28,6 +28,13 @@ class WaitedFile(models.Model):
         else:
             return False
 
+    @classmethod
+    def can_order(cls, path):
+        return (cls.objects.filter(path=path).exists() or
+                cls.exists(path) or
+                cls.objects.count() < WAITER_MAX_QUEUE
+                )
+
     def is_stale(self):
         if self.task is None:
             # Race; just let the other task roll. 
@@ -51,6 +58,7 @@ class WaitedFile(models.Model):
             waited, created = cls.objects.get_or_create(path=path)
             if created or waited.is_stale():
                 waited.task = task_creator(check_abspath(path))
+                waited.task_id = waited.task.task_id
                 waited.description = description
                 waited.save()
             return reverse("waiter", args=[path])