Update the OCR app to use Celery, remove OCR config options OCR_REPLICATION_DELAY, OCR_NODE_CONCURRENT_EXECUTION, OCR_QUEUE_PROCESSING_INTERVAL
This commit is contained in:
+24
-47
@@ -10,37 +10,34 @@ from django.conf import settings
|
||||
from django.db.models import Q
|
||||
from django.utils.timezone import now
|
||||
|
||||
from documents.models import Document
|
||||
from job_processor.api import process_job
|
||||
from lock_manager import Lock, LockError
|
||||
from mayan.celery import app
|
||||
|
||||
from .api import do_document_ocr
|
||||
from .literals import (DOCUMENTQUEUE_STATE_ACTIVE, QUEUEDOCUMENT_STATE_ERROR,
|
||||
QUEUEDOCUMENT_STATE_PENDING,
|
||||
QUEUEDOCUMENT_STATE_PROCESSING)
|
||||
from .models import DocumentQueue, QueueDocument
|
||||
from .settings import NODE_CONCURRENT_EXECUTION, REPLICATION_DELAY
|
||||
|
||||
LOCK_EXPIRE = 60 * 2 # Lock expires in 2 minutes
|
||||
# TODO: Tie LOCK_EXPIRATION with hard task timeout
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
LOCK_EXPIRE = 60 * 10 # Adjust to worst case scenario
|
||||
|
||||
|
||||
def task_process_queue_document(queue_document_id):
|
||||
lock_id = u'task_proc_queue_doc-%d' % queue_document_id
|
||||
@app.task
|
||||
def task_do_ocr(document_pk):
|
||||
lock_id = u'task_do_ocr_doc-%d' % document_pk
|
||||
try:
|
||||
logger.debug('trying to acquire lock: %s' % lock_id)
|
||||
lock = Lock.acquire_lock(lock_id, LOCK_EXPIRE)
|
||||
logger.debug('acquired lock: %s' % lock_id)
|
||||
queue_document = QueueDocument.objects.get(pk=queue_document_id)
|
||||
queue_document.state = QUEUEDOCUMENT_STATE_PROCESSING
|
||||
queue_document.node_name = platform.node()
|
||||
queue_document.save()
|
||||
try:
|
||||
do_document_ocr(queue_document)
|
||||
queue_document.delete()
|
||||
logger.info('Starting document OCR for document: %d' % document_pk)
|
||||
document = Document.objects.get(pk=document_pk)
|
||||
do_document_ocr(document)
|
||||
except Exception as exception:
|
||||
queue_document.state = QUEUEDOCUMENT_STATE_ERROR
|
||||
logger.error('OCR error for document: %d; %s' % (document_pk, exception))
|
||||
document_queue = DocumentQueue.objects.get(name='default')
|
||||
queue_document, created = document_queue.documents.get_or_create(document=document)
|
||||
queue_document.node_name = platform.node()
|
||||
|
||||
if settings.DEBUG:
|
||||
result = []
|
||||
@@ -52,37 +49,17 @@ def task_process_queue_document(queue_document_id):
|
||||
queue_document.result = exception
|
||||
|
||||
queue_document.save()
|
||||
|
||||
lock.release()
|
||||
else:
|
||||
logger.info('OCR for document: %d ended' % document_pk)
|
||||
document_queue = DocumentQueue.objects.get(name='default')
|
||||
try:
|
||||
queue_document = document_queue.documents.get(document=document)
|
||||
except QueueDocument.DoesNotExist:
|
||||
pass
|
||||
else:
|
||||
queue_document.delete()
|
||||
finally:
|
||||
lock.release()
|
||||
except LockError:
|
||||
logger.debug('unable to obtain lock')
|
||||
pass
|
||||
|
||||
|
||||
def task_process_document_queues():
|
||||
logger.debug('executed')
|
||||
# TODO: reset_orphans()
|
||||
q_pending = Q(state=QUEUEDOCUMENT_STATE_PENDING)
|
||||
q_delayed = Q(delay=True)
|
||||
q_delay_interval = Q(datetime_submitted__lt=now() - timedelta(seconds=REPLICATION_DELAY))
|
||||
for document_queue in DocumentQueue.objects.filter(state=DOCUMENTQUEUE_STATE_ACTIVE):
|
||||
current_local_processing_count = QueueDocument.objects.filter(
|
||||
state=QUEUEDOCUMENT_STATE_PROCESSING).filter(
|
||||
node_name=platform.node()).count()
|
||||
if current_local_processing_count < NODE_CONCURRENT_EXECUTION:
|
||||
try:
|
||||
oldest_queued_document_qs = document_queue.queuedocument_set.filter(
|
||||
(q_pending & ~q_delayed) | (q_pending & q_delayed & q_delay_interval))
|
||||
|
||||
if oldest_queued_document_qs:
|
||||
oldest_queued_document = oldest_queued_document_qs.order_by('datetime_submitted')[0]
|
||||
process_job(task_process_queue_document, oldest_queued_document.pk)
|
||||
except Exception as exception:
|
||||
logger.error('unhandled exception: %s' % exception)
|
||||
finally:
|
||||
# Don't process anymore from this queryset, might be stale
|
||||
break
|
||||
else:
|
||||
logger.debug('already processing maximum')
|
||||
else:
|
||||
logger.debug('nothing to process')
|
||||
|
||||
Reference in New Issue
Block a user