from logging import getLogger
from plone.app.contenttypes import indexers as pac_indexers
from Products.CMFCore import indexing
from Products.CMFCore.indexing import getQueue
from Products.CMFCore.indexing import processing
from zope.globalrequest import getRequest

logger = getLogger(__name__)


def processQueue():
    """process the queue (for this thread) immediately"""
    queue = getQueue()
    processed = 0
    if queue.length() and queue not in processing:
        request = getRequest()
        try:
            queue_disabled = request.get("disable-indexing")
        except Exception:
            queue_disabled = False
        if not queue_disabled:
            try:
                processing.add(queue)
                processed = queue.process()
            finally:
                processing.remove(queue)
    return processed


indexing.processQueue = processQueue


# Patch the SearchableText file indexer because we use Solr for that
pac_indexers.SearchableText_file.callable = pac_indexers.SearchableText
