Added job_processors app to abstract background job processing
This commit is contained in:
@@ -44,7 +44,7 @@ class ConverterClass(ConverterBase):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
while 1:
|
while 1:
|
||||||
im.seek(im.tell()+1)
|
im.seek(im.tell() + 1)
|
||||||
page_count += 1
|
page_count += 1
|
||||||
# do something to im
|
# do something to im
|
||||||
except EOFError:
|
except EOFError:
|
||||||
|
|||||||
0
apps/job_processor/__init__.py
Normal file
0
apps/job_processor/__init__.py
Normal file
16
apps/job_processor/api.py
Normal file
16
apps/job_processor/api.py
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
from celery.decorators import task, periodic_task
|
||||||
|
from celery.task.control import inspect
|
||||||
|
|
||||||
|
from job_processor.conf.settings import BACKEND
|
||||||
|
|
||||||
|
|
||||||
|
@task
|
||||||
|
def celery_task(func, *args, **kwargs):
|
||||||
|
return func(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
def process_job(func, *args, **kwargs):
|
||||||
|
if BACKEND == 'celery':
|
||||||
|
return celery_task.delay(func, *args, **kwargs)
|
||||||
|
elif BACKEND is None:
|
||||||
|
return func(*args, **kwargs)
|
||||||
0
apps/job_processor/conf/__init__.py
Normal file
0
apps/job_processor/conf/__init__.py
Normal file
12
apps/job_processor/conf/settings.py
Normal file
12
apps/job_processor/conf/settings.py
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
"""Configuration options for the job_processing app"""
|
||||||
|
from django.utils.translation import ugettext_lazy as _
|
||||||
|
|
||||||
|
from smart_settings.api import register_settings
|
||||||
|
|
||||||
|
register_settings(
|
||||||
|
namespace=u'job_processor',
|
||||||
|
module=u'job_processor.conf.settings',
|
||||||
|
settings=[
|
||||||
|
{'name': u'BACKEND', 'global_name': u'JOB_PROCESSING_BACKEND', 'default': None, 'description': _('Specified which job processing library to use, option are: None and celery.')},
|
||||||
|
]
|
||||||
|
)
|
||||||
3
apps/job_processor/models.py
Normal file
3
apps/job_processor/models.py
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
from django.db import models
|
||||||
|
|
||||||
|
# Create your models here.
|
||||||
23
apps/job_processor/tests.py
Normal file
23
apps/job_processor/tests.py
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
"""
|
||||||
|
This file demonstrates two different styles of tests (one doctest and one
|
||||||
|
unittest). These will both pass when you run "manage.py test".
|
||||||
|
|
||||||
|
Replace these with more appropriate tests for your application.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from django.test import TestCase
|
||||||
|
|
||||||
|
class SimpleTest(TestCase):
|
||||||
|
def test_basic_addition(self):
|
||||||
|
"""
|
||||||
|
Tests that 1 + 1 always equals 2.
|
||||||
|
"""
|
||||||
|
self.failUnlessEqual(1 + 1, 2)
|
||||||
|
|
||||||
|
__test__ = {"doctest": """
|
||||||
|
Another way to test that 1 + 1 is equal to 2.
|
||||||
|
|
||||||
|
>>> 1 + 1 == 2
|
||||||
|
True
|
||||||
|
"""}
|
||||||
|
|
||||||
1
apps/job_processor/views.py
Normal file
1
apps/job_processor/views.py
Normal file
@@ -0,0 +1 @@
|
|||||||
|
# Create your views here.
|
||||||
@@ -7,8 +7,7 @@ from django.db.models import Q
|
|||||||
from django.utils.translation import ugettext as _
|
from django.utils.translation import ugettext as _
|
||||||
from django.core.cache import get_cache
|
from django.core.cache import get_cache
|
||||||
|
|
||||||
from celery.decorators import task, periodic_task
|
from job_processor.api import process_job
|
||||||
from celery.task.control import inspect
|
|
||||||
|
|
||||||
from ocr.api import do_document_ocr
|
from ocr.api import do_document_ocr
|
||||||
from ocr.literals import QUEUEDOCUMENT_STATE_PENDING, \
|
from ocr.literals import QUEUEDOCUMENT_STATE_PENDING, \
|
||||||
@@ -46,7 +45,6 @@ else:
|
|||||||
release_lock = lambda lock_id: True
|
release_lock = lambda lock_id: True
|
||||||
|
|
||||||
|
|
||||||
#@task
|
|
||||||
def task_process_queue_document(queue_document_id):
|
def task_process_queue_document(queue_document_id):
|
||||||
lock_id = u'%s-lock-%d' % (u'task_process_queue_document', queue_document_id)
|
lock_id = u'%s-lock-%d' % (u'task_process_queue_document', queue_document_id)
|
||||||
if acquire_lock(lock_id):
|
if acquire_lock(lock_id):
|
||||||
@@ -112,6 +110,7 @@ def task_process_document_queues():
|
|||||||
if oldest_queued_document_qs:
|
if oldest_queued_document_qs:
|
||||||
oldest_queued_document = oldest_queued_document_qs.order_by('datetime_submitted')[0]
|
oldest_queued_document = oldest_queued_document_qs.order_by('datetime_submitted')[0]
|
||||||
#task_process_queue_document.delay(oldest_queued_document.pk)
|
#task_process_queue_document.delay(oldest_queued_document.pk)
|
||||||
task_process_queue_document(oldest_queued_document.pk)
|
#task_process_queue_document(oldest_queued_document.pk)
|
||||||
|
process_job(task_process_queue_document, oldest_queued_document.pk)
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
print 'DocumentQueueWatcher exception: %s' % e
|
print 'DocumentQueueWatcher exception: %s' % e
|
||||||
|
|||||||
@@ -152,6 +152,7 @@ INSTALLED_APPS = (
|
|||||||
'sources',
|
'sources',
|
||||||
'mimetype',
|
'mimetype',
|
||||||
'scheduler',
|
'scheduler',
|
||||||
|
'job_processor',
|
||||||
)
|
)
|
||||||
|
|
||||||
TEMPLATE_CONTEXT_PROCESSORS = (
|
TEMPLATE_CONTEXT_PROCESSORS = (
|
||||||
|
|||||||
Reference in New Issue
Block a user