Algolia: Use Celery to push user updates in a background task.
This commit is contained in:
parent
ed4ee5228a
commit
e7d268bde6
@ -62,14 +62,9 @@ def before_replacing_user(request, lookup):
|
|||||||
def push_updated_user_to_algolia(user, original):
|
def push_updated_user_to_algolia(user, original):
|
||||||
"""Push an update to the Algolia index when a user item is updated"""
|
"""Push an update to the Algolia index when a user item is updated"""
|
||||||
|
|
||||||
from algoliasearch.helpers import AlgoliaException
|
from pillar.celery import algolia_tasks
|
||||||
from pillar.api.utils.algolia import algolia_index_user_save
|
|
||||||
|
|
||||||
try:
|
algolia_tasks.push_updated_user_to_algolia.delay(str(user['_id']))
|
||||||
algolia_index_user_save(user)
|
|
||||||
except AlgoliaException as ex:
|
|
||||||
log.warning('Unable to push user info to Algolia for user "%s", id=%s; %s',
|
|
||||||
user.get('username'), user.get('_id'), ex)
|
|
||||||
|
|
||||||
|
|
||||||
def send_blinker_signal_roles_changed(user, original):
|
def send_blinker_signal_roles_changed(user, original):
|
||||||
|
28
pillar/celery/algolia_tasks.py
Normal file
28
pillar/celery/algolia_tasks.py
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
import logging
|
||||||
|
|
||||||
|
import bson
|
||||||
|
|
||||||
|
from pillar import current_app
|
||||||
|
|
||||||
|
from .celery_cfg import celery_cfg
|
||||||
|
|
||||||
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
@celery_cfg.task(ignore_result=True)
|
||||||
|
def push_updated_user_to_algolia(user_id: str):
|
||||||
|
"""Push an update to the Algolia index when a user item is updated"""
|
||||||
|
|
||||||
|
from algoliasearch.helpers import AlgoliaException
|
||||||
|
from pillar.api.utils.algolia import algolia_index_user_save
|
||||||
|
|
||||||
|
user_oid = bson.ObjectId(user_id)
|
||||||
|
log.info('Retrieving user %s', user_oid)
|
||||||
|
users_coll = current_app.db('users')
|
||||||
|
user = users_coll.find_one({'_id': user_oid})
|
||||||
|
|
||||||
|
try:
|
||||||
|
algolia_index_user_save(user)
|
||||||
|
except AlgoliaException as ex:
|
||||||
|
log.warning('Unable to push user info to Algolia for user "%s", id=%s; %s',
|
||||||
|
user.get('username'), user.get('_id'), ex)
|
@ -2,6 +2,7 @@ from celery import Celery
|
|||||||
|
|
||||||
task_modules = [
|
task_modules = [
|
||||||
'pillar.celery.tasks',
|
'pillar.celery.tasks',
|
||||||
|
'pillar.celery.algolia_tasks',
|
||||||
]
|
]
|
||||||
|
|
||||||
celery_cfg = Celery('proj',
|
celery_cfg = Celery('proj',
|
||||||
|
Loading…
x
Reference in New Issue
Block a user