Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate CTS-related code into a separate app
This avoids extra dependencies and the crashing celery task on systems which do not use CTS.
- Loading branch information
Showing
16 changed files
with
133 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,18 @@ | ||
import logging | ||
|
||
from title_loader_tests import * | ||
from holding_loader_tests import * | ||
from title_pull_tests import * | ||
from api_tests import * | ||
from batch_loader_tests import * | ||
from browse_tests import * | ||
from ocr_extractor_tests import * | ||
from essay_loader_tests import * | ||
from holding_loader_tests import * | ||
from index_tests import * | ||
from jp2_tests import * | ||
from api_tests import * | ||
from json_tests import * | ||
from ocr_dump_tests import * | ||
from ocr_extractor_tests import * | ||
from rdf_tests import * | ||
from system_tests import * | ||
from cts_tests import * | ||
from ocr_dump_tests import * | ||
|
||
from title_loader_tests import * | ||
from title_pull_tests import * | ||
|
||
logging.basicConfig(filename="test.log", level=logging.DEBUG) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import json | ||
import urllib | ||
import logging | ||
import urllib | ||
import urlparse | ||
|
||
import requests | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from __future__ import absolute_import | ||
|
||
import logging | ||
|
||
from django.core.management.base import BaseCommand | ||
|
||
|
||
class LoggingCommand(BaseCommand): | ||
def execute(self, *args, **options): | ||
verbosity = options.get("verbosity", 0) | ||
|
||
if verbosity > 0: | ||
log_level = logging.DEBUG if verbosity > 1 else logging.INFO | ||
loggers = [logging.getLogger(), logging.getLogger("chronam")] | ||
for logger in loggers: | ||
logger.setLevel(log_level) | ||
for handler in logger.handlers: | ||
handler.setLevel(log_level) | ||
|
||
return super(LoggingCommand, self).execute(*args, **options) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import logging | ||
import os | ||
|
||
import minicts | ||
from celery.decorators import task | ||
from django.conf import settings | ||
|
||
from chronam.core import cts | ||
from chronam.core.models import Batch | ||
from chronam.core.tasks import load_batch, purge_batch | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
@task | ||
def poll_purge(): | ||
cts = minicts.CTS(settings.CTS_URL, settings.CTS_USERNAME, settings.CTS_PASSWORD) | ||
|
||
queue = settings.CTS_QUEUE | ||
purge_service_type = "purge.NdnpPurge.purge" | ||
while True: | ||
req = cts.next_service_request(queue, purge_service_type) | ||
if req is None: | ||
logger.info("no purge service requests") | ||
break | ||
|
||
logger.info('got purge service request: %s', req.url) | ||
bag_instance_key = req.data['requestParameters']['baginstancekey'] | ||
bag_instance = cts.get_bag_instance(bag_instance_key) | ||
batch_name = os.path.basename(bag_instance.data['filepath']) | ||
logger.info('purging %s', batch_name) | ||
|
||
# if the batch isn't there no need to purge | ||
try: | ||
if Batch.objects.filter(name=batch_name).count() == 0: | ||
logger.info('no need to purge %s ; it is not loaded', batch_name) | ||
logger.info('batch %s purged', batch_name) | ||
else: | ||
purge_batch(batch_name, req) | ||
except Exception as e: | ||
logger.exception("purge of %s failed", batch_name) | ||
req.fail("purge of %s failed: %s" % (batch_name, e)) | ||
|
||
|
||
@task | ||
def poll_cts(): | ||
if settings.MAX_BATCHES != 0 and Batch.objects.all().count() >= settings.MAX_BATCHES: | ||
logger.debug("not loading more than %s batches", settings.MAX_BATCHES) | ||
return None | ||
|
||
c = cts.CTS(settings.CTS_USERNAME, settings.CTS_PASSWORD, settings.CTS_URL) | ||
|
||
# 'ndnpstagingingestqueue', 'ingest.NdnpIngest.ingest' | ||
sr = c.next_service_request(settings.CTS_QUEUE, settings.CTS_SERVICE_TYPE) | ||
|
||
# no service request? whew, we're done. | ||
if not sr: | ||
logger.debug("no service requests") | ||
return | ||
|
||
# determine the location of the bag on the filesystem | ||
logger.info("got service request: %s", sr) | ||
bag_instance_id = sr.data['requestParameters']['baginstancekey'] | ||
bag = c.get_bag_instance(bag_instance_id) | ||
bag_dir = bag.data['filepath'] | ||
|
||
try: | ||
logger.info("loading %s", bag_dir) | ||
return load_batch.delay(bag_dir, sr) | ||
|
||
except Exception as e: | ||
logger.exception("loading batch failed!") | ||
sr.fail(str(e)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from cts_tests import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters