Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix catalog logging counter duplicates #2048

Merged
merged 2 commits into from
Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Changelog
2.3.0 (unreleased)
------------------

- #2048 Fix catalog logging counter duplicates
- #2047 Make resultsinterpretation.pt to retrieve departments from viewlet
- #2045 Fix instrument types instruments view
- #2044 Skip Invoice for content exports
Expand Down
29 changes: 22 additions & 7 deletions src/senaite/core/catalog/base_catalog.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# -*- coding: utf-8 -*-

from threading import RLock

import transaction
from AccessControl import ClassSecurityInfo
from AccessControl.Permissions import \
Expand All @@ -19,6 +21,8 @@
CATALOG_ID = "senaite_catalog_base"
CATALOG_TITLE = "Senaite Base Catalog"

progress_rlock = RLock()

INDEXES = [
# id, indexed attribute, type
("allowedRolesAndUsers", "", "KeywordIndex"),
Expand Down Expand Up @@ -65,6 +69,7 @@ class BaseCatalog(CatalogTool):
def __init__(self, id, title="", **kw):
# CatalogTool does not take any parameters in __init__
ZCatalog.__init__(self, id, title=title, **kw)
self.progress_counter = 0

@property
def mapped_catalog_types(self):
Expand Down Expand Up @@ -104,6 +109,19 @@ def get_mapped_types(self):
mapped_at_types = self.get_mapped_at_types()
return mapped_catalog_types + mapped_at_types

def log_progress(self):
"""Log reindex progress
"""
with progress_rlock:
self.progress_counter += 1

if self.progress_counter % 100 == 0:
logger.info("Progress: {} objects have been cataloged for {}."
.format(self.progress_counter, self.id))

if self.progress_counter % 10000 == 0:
transaction.savepoint(optimistic=True)

@security.protected(ManageZCatalogEntries)
def clearFindAndRebuild(self):
"""Considers only mapped types when reindexing the whole catalog
Expand All @@ -130,28 +148,25 @@ def indexObject(obj, path):
# catalog, but does not take DX multiplexing into
# consideration.
self._reindexObject(obj, idxs=idxs) # bypass queue
self.counter += 1
self.log_progress()
elif api.is_dexterity_content(obj):
# NOTE: Catalog multiplexing is only available for DX types
# and stores the catalogs in a variable `_catalogs`.
multiplex_catalogs = getattr(obj, "_catalogs", [])
if self.id in multiplex_catalogs:
self._reindexObject(obj, idxs=idxs) # bypass queue
self.counter += 1
self.log_progress()
else:
return
except TypeError:
# Catalogs have 'indexObject' as well, but they
# take different args, and will fail
pass

if self.counter and self.counter % 100 == 0:
logger.info("Progress: {} objects have been cataloged for {}."
.format(self.counter, self.id))
transaction.savepoint(optimistic=True)
# reset the progress counter
self.progress_counter = 0

logger.info("Cleaning and rebuilding catalog '%s'..." % self.id)
self.counter = 0
self.manage_catalogClear()
portal = aq_parent(aq_inner(self))
portal.ZopeFindAndApply(
Expand Down