Introduce a random delay to each node to further reduce the chance of a race condition, until row locking can be implemented or is implemented by Django
This commit is contained in:
+7
-2
@@ -1,5 +1,7 @@
|
|||||||
from datetime import date, timedelta, datetime
|
from datetime import date, timedelta, datetime
|
||||||
import platform
|
import platform
|
||||||
|
import time
|
||||||
|
import random
|
||||||
|
|
||||||
from django.db.models import Q
|
from django.db.models import Q
|
||||||
|
|
||||||
@@ -20,8 +22,8 @@ from ocr.conf.settings import REPLICATION_DELAY
|
|||||||
@task
|
@task
|
||||||
def task_process_queue_document(queue_document_id):
|
def task_process_queue_document(queue_document_id):
|
||||||
queue_document = QueueDocument.objects.get(id=queue_document_id)
|
queue_document = QueueDocument.objects.get(id=queue_document_id)
|
||||||
if queue_document.node_name != platform.node():
|
if queue_document.state == QUEUEDOCUMENT_STATE_PROCESSING:
|
||||||
#Recheck to avoid race condition
|
#Recheck to avoid race condition
|
||||||
return
|
return
|
||||||
queue_document.state = QUEUEDOCUMENT_STATE_PROCESSING
|
queue_document.state = QUEUEDOCUMENT_STATE_PROCESSING
|
||||||
queue_document.node_name = platform.node()
|
queue_document.node_name = platform.node()
|
||||||
@@ -39,6 +41,9 @@ class DocumentQueueWatcher(PeriodicTask):
|
|||||||
run_every = timedelta(seconds=5)
|
run_every = timedelta(seconds=5)
|
||||||
|
|
||||||
def run(self, **kwargs):
|
def run(self, **kwargs):
|
||||||
|
#Introduce random 0 < t < 1 second delay to further reduce the
|
||||||
|
#chance of a race condition
|
||||||
|
time.sleep(random.random())
|
||||||
logger = self.get_logger(**kwargs)
|
logger = self.get_logger(**kwargs)
|
||||||
logger.info('Running queue watcher.')
|
logger.info('Running queue watcher.')
|
||||||
logger.debug('Active queues: %s' % DocumentQueue.objects.filter(state=DOCUMENTQUEUE_STATE_ACTIVE))
|
logger.debug('Active queues: %s' % DocumentQueue.objects.filter(state=DOCUMENTQUEUE_STATE_ACTIVE))
|
||||||
|
|||||||
Reference in New Issue
Block a user