-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor reindex helpers in an importable way
- Loading branch information
Showing
3 changed files
with
135 additions
and
72 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,20 @@ | ||
from collective.solr.interfaces import ISolrConnectionManager | ||
from plone.registry.interfaces import IRegistry | ||
from kitconcept.solr.reindex_helpers import activate_and_reindex | ||
from Testing.makerequest import makerequest | ||
from zope.component import getUtility | ||
from zope.component import queryUtility | ||
from zope.site.hooks import setSite | ||
|
||
import logging | ||
import sys | ||
import transaction | ||
|
||
|
||
logger = logging.getLogger("kitconcept.solr") | ||
logger.setLevel(logging.DEBUG) | ||
if __name__ == "__main__": | ||
app = makerequest(app) # noQA | ||
|
||
indexer_logger = logging.getLogger("collective.solr.indexer") | ||
# Set site to Plone | ||
site_id = "Plone" | ||
portal = app.unrestrictedTraverse(site_id) | ||
setSite(portal) | ||
|
||
activate_and_reindex(portal, clear="--clear" in sys.argv) | ||
|
||
def solr_is_running(portal): | ||
manager = queryUtility(ISolrConnectionManager, context=portal) | ||
schema = manager.getSchema() | ||
return schema is not None | ||
|
||
|
||
def solr_must_be_running(portal): | ||
if not solr_is_running(portal): | ||
logger.fatal("*** Solr must be running! (make solr-start) ***") | ||
sys.exit(1) | ||
|
||
|
||
def activate(active=True): | ||
"""(de)activate the solr integration""" | ||
registry = getUtility(IRegistry) | ||
registry["collective.solr.active"] = active | ||
|
||
|
||
def silence_logger(): | ||
orig_logger_exception = indexer_logger.exception | ||
|
||
def new_logger_exception(msg): | ||
if msg != "Error occured while getting data for indexing!": | ||
orig_logger_exception(msg) | ||
|
||
indexer_logger.exception = new_logger_exception | ||
|
||
def reactivate_logger(): | ||
indexer_logger.exception = orig_logger_exception | ||
|
||
return reactivate_logger | ||
|
||
|
||
def reindex(portal): | ||
"""reindex the existing content in solr""" | ||
maintenance = portal.unrestrictedTraverse("@@solr-maintenance") | ||
if "--clear" in sys.argv: | ||
logger.info("Clearing solr...") | ||
maintenance.clear() | ||
# Avoid throwing a lot of errors which are actually not errors, | ||
# but the indexer keeps throwing them when it tries to traverse everything. | ||
reactivate_logger = silence_logger() | ||
logger.info("Reindexing solr...") | ||
maintenance.reindex() | ||
reactivate_logger() | ||
|
||
|
||
app = makerequest(app) # noQA | ||
|
||
# Set site to Plone | ||
site_id = "Plone" | ||
portal = app.unrestrictedTraverse(site_id) | ||
setSite(portal) | ||
|
||
# Activate before confirming solr is running, | ||
# because the confirmation only works if solr is enabled in the registry. | ||
# If solr isn't running, we'll exit | ||
# before committing the transaction with the activation. | ||
activate() | ||
solr_must_be_running(portal) | ||
reindex(portal) | ||
|
||
transaction.commit() | ||
app._p_jar.sync() | ||
transaction.commit() | ||
app._p_jar.sync() |
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,68 @@ | ||
from collective.solr.interfaces import ISolrConnectionManager | ||
from plone import api | ||
from zope.component import queryUtility | ||
|
||
import logging | ||
|
||
|
||
logger = logging.getLogger("kitconcept.solr") | ||
logger.setLevel(logging.DEBUG) | ||
|
||
indexer_logger = logging.getLogger("collective.solr.indexer") | ||
|
||
|
||
def solr_is_running(portal): | ||
manager = queryUtility(ISolrConnectionManager, context=portal) | ||
schema = manager.getSchema() | ||
return schema is not None | ||
|
||
|
||
def solr_must_be_running(portal): | ||
if not solr_is_running(portal): | ||
logger.fatal("*** Solr must be running! (make solr-start) ***") | ||
return False | ||
return True | ||
|
||
|
||
def activate(active=True): | ||
"""(de)activate the solr integration""" | ||
api.portal.set_registry_record("collective.solr.active", active) | ||
|
||
|
||
def silence_logger(): | ||
orig_logger_exception = indexer_logger.exception | ||
|
||
def new_logger_exception(msg): | ||
if msg != "Error occured while getting data for indexing!": | ||
orig_logger_exception(msg) | ||
|
||
indexer_logger.exception = new_logger_exception | ||
|
||
def reactivate_logger(): | ||
indexer_logger.exception = orig_logger_exception | ||
|
||
return reactivate_logger | ||
|
||
|
||
def reindex(portal, clear=False): | ||
"""reindex the existing content in solr""" | ||
maintenance = portal.unrestrictedTraverse("@@solr-maintenance") | ||
if clear: | ||
logger.info("Clearing solr...") | ||
maintenance.clear() | ||
# Avoid throwing a lot of errors which are actually not errors, | ||
# but the indexer keeps throwing them when it tries to traverse everything. | ||
reactivate_logger = silence_logger() | ||
logger.info("Reindexing solr...") | ||
maintenance.reindex() | ||
reactivate_logger() | ||
|
||
|
||
def activate_and_reindex(portal, clear=False): | ||
# Activate before confirming solr is running, | ||
# because the confirmation only works if solr is enabled in the registry. | ||
# If solr isn't running, we'll exit | ||
# before committing the transaction with the activation. | ||
activate() | ||
if solr_must_be_running(portal): | ||
reindex(portal, clear=clear) |