Skip to content
This repository has been archived by the owner on Apr 9, 2023. It is now read-only.

Commit

Permalink
Moving catalog reindex to an async task
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramon Navarro Bosch committed Jan 17, 2017
1 parent 7462d92 commit f771bf5
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 17 deletions.
8 changes: 0 additions & 8 deletions src/plone.server/plone/server/api/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,6 @@ async def __call__(self):
for behavior in behaviors or ():
self.context.add_behavior(behavior)

if '__name__' in data:
# Special case we need to change the id of the content we
# also need to delete the caches of the paths
if hasattr(self.context, '_v_physical_path'):
del self.context._v_physical_path
if hasattr(self.context, '_v_get_content_path'):
del self.context._v_get_content_path

deserializer = queryMultiAdapter((self.context, self.request),
IResourceDeserializeFromJson)
if deserializer is None:
Expand Down
19 changes: 16 additions & 3 deletions src/plone.server/plone/server/api/search.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# -*- coding: utf-8 -*-
from plone.server.configure import service
from plone.server.api.service import Service
from plone.server.interfaces import ICatalogUtility
from plone.server.interfaces import IResource
from zope.component import queryUtility
from plone.server.utils import get_content_path
from plone.server.async import IQueueUtility


@service(context=IResource, method='GET', permission='plone.SearchContent',
Expand Down Expand Up @@ -39,9 +41,20 @@ async def search_post(context, request):

@service(context=IResource, method='POST', permission='plone.ReindexContent',
name='@catalog-reindex')
async def catalog_reindex(context, request):
search = queryUtility(ICatalogUtility)
await search.reindex_all_content(context)
class CatalogReindex(Service):

async def __call__(self):
search = queryUtility(ICatalogUtility)
await search.reindex_all_content(self.context)
return {}


@service(context=IResource, method='POST', permission='plone.ReindexContent',
name='@async-catalog-reindex')
async def async_catalog_reindex(context, request):
util = queryUtility(IQueueUtility)
import pdb; pdb.set_trace()
await util.add(CatalogReindex(context, request))
return {}


Expand Down
16 changes: 14 additions & 2 deletions src/plone.server/plone/server/async.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from zope.i18nmessageid import MessageFactory
from zope.interface import Interface
from zope.security.interfaces import Unauthorized
from plone.server.interfaces import SHARED_CONNECTION

import asyncio
import logging
Expand Down Expand Up @@ -46,6 +47,17 @@ async def initialize(self, app=None):
try:
priority, view = await self._queue.get()
got_obj = True
if view.request.conn.transaction_manager is None:
# Connection was closed
# Open DB
db = view.request.application[view.request._db_id]
if SHARED_CONNECTION:
view.request.conn = db.conn
else:
# Create a new conection
view.request.conn = db.open()
view.context = view.request.conn.get(view.context._p_oid)

txn = view.request.conn.transaction_manager.begin(view.request)
try:
view_result = await view()
Expand All @@ -71,9 +83,9 @@ async def initialize(self, app=None):
except KeyboardInterrupt or MemoryError or SystemExit or asyncio.CancelledError:
self._exceptions = True
raise
except: # noqa
except Exception as e: # noqa
self._exceptions = True
logger.error('Worker call failed')
logger.error('Worker call failed', exc_info=e)
finally:
if got_obj:
self._queue.task_done()
Expand Down
5 changes: 1 addition & 4 deletions src/plone.server/plone/server/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,13 @@


def get_physical_path(context):
if hasattr(context, '_v_physical_path'):
return context._v_physical_path
parts = [context.__name__]
parent = context.__parent__
while parent is not None and parent.__name__ is not None:
parts.append(parent.__name__)
parent = parent.__parent__
parts.append('')
context._v_physical_path = [x for x in reversed(parts)]
return context._v_physical_path
return [x for x in reversed(parts)]


@adapter(IResource, IRequest)
Expand Down

0 comments on commit f771bf5

Please sign in to comment.