<commit_before>import httplib
from time import sleep
from celery.decorators import task

from django.conf import settings
from django.core.urlresolvers import reverse

@task
def reload_cached_pdf(eadid):
    """Request the PDF of the finding aid (specified by eadid) from the configured
    proxy server, to trigger the proxy reloading and caching the latest version
    of that PDF (e.g., after updating or adding a new document in eXist)."""
    logger = reload_cached_pdf.get_logger()
    if hasattr(settings, 'PROXY_HOST') and hasattr(settings, 'SITE_BASE_URL'):
        sleep(3)    # may need to sleep for a few seconds so cache will recognized as modified (?)
        connection = httplib.HTTPConnection(settings.PROXY_HOST)
        url = "%s%s" % (settings.SITE_BASE_URL.rstrip('/'), reverse('fa:printable', kwargs={'id': eadid }))
        logger.info("Requesting PDF for %s from configured cache at %s" % (eadid, url))
        # set headers to force the cache to get a fresh copy
        refresh_cache = {
            # tell the cache to grab a fresh copy (implied: cache the fresh copy)
            'Cache-Control': 'max-age=0'
            # NOTE: using Cache-Control: no-cache clears the cache but does not populate
            }
        connection.request('GET', url, None, refresh_cache)     # no request body, cache header
        r = connection.getresponse()    # actually get the response to trigger PDF generation
        if r.status == 200:
            return True
        else:
            raise Exception("Got unexpected HTTP status code from response: %s" % r.status)
    else:
        raise Exception("PROXY_HOST and/or SITE_BASE_URL settings not available.  Failed to reload cached PDF.")<commit_msg>add debug logging with request/response headers for cache pdf task, to assist troubleshooting<commit_after>import httplib
from time import sleep
from celery.decorators import task

from django.conf import settings
from django.core.urlresolvers import reverse

@task
def reload_cached_pdf(eadid):
    """Request the PDF of the finding aid (specified by eadid) from the configured
    proxy server, to trigger the proxy reloading and caching the latest version
    of that PDF (e.g., after updating or adding a new document in eXist)."""
    logger = reload_cached_pdf.get_logger()
    if hasattr(settings, 'PROXY_HOST') and hasattr(settings, 'SITE_BASE_URL'):
        sleep(3)    # may need to sleep for a few seconds so cache will recognized as modified (?)
        connection = httplib.HTTPConnection(settings.PROXY_HOST)
        url = "%s%s" % (settings.SITE_BASE_URL.rstrip('/'), reverse('fa:printable', kwargs={'id': eadid }))
        logger.info("Requesting PDF for %s from configured cache at %s" % (eadid, url))
        # set headers to force the cache to get a fresh copy
        refresh_cache = {
            # tell the cache to grab a fresh copy (implied: cache the fresh copy)
            'Cache-Control': 'max-age=0'
            # NOTE: using Cache-Control: no-cache clears the cache but does not populate
            }
        logger.debug('Request headers: \n%s' % refresh_cache)
        connection.request('GET', url, None, refresh_cache)     # no request body, cache header
        r = connection.getresponse()    # actually get the response to trigger PDF generation
        logger.debug('Response headers: \n%s' % \
                     '\n'.join(['\t%s: %s' % header for header in r.getheaders()]))
        if r.status == 200:
            return True
        else:
            raise Exception("Got unexpected HTTP status code from response: %s" % r.status)
    else:
        raise Exception("PROXY_HOST and/or SITE_BASE_URL settings not available.  Failed to reload cached PDF.")
{'fraction_non_alphanumeric': 0.09005376344086022, 'fraction_numerical': 0.002688172043010753, 'mean_word_length': 2.992489270386266, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4125295', 'n_tokens_mistral': 1029, 'n_tokens_neox': 1012, 'n_words': 399}