From eac58cdc22f4f42a86481d8f507fb8698616f7b0 Mon Sep 17 00:00:00 2001 From: Samuel Clay Date: Wed, 23 Apr 2014 16:12:30 -0700 Subject: [PATCH] Indexing newly added feeds for users who need it. --- apps/reader/views.py | 2 +- apps/search/models.py | 35 +++++++++++++++++++++++++++++++++-- apps/search/tasks.py | 7 +++++++ settings.py | 2 +- 4 files changed, 42 insertions(+), 4 deletions(-) diff --git a/apps/reader/views.py b/apps/reader/views.py index f83da9f8e3..dba49e758e 100644 --- a/apps/reader/views.py +++ b/apps/reader/views.py @@ -1460,7 +1460,7 @@ def add_url(request): if feed: r = redis.Redis(connection_pool=settings.REDIS_PUBSUB_POOL) r.publish(request.user.username, 'reload:%s' % feed.pk) - + MUserSearch.schedule_index_feeds_for_search(feed.pk, request.user.pk) return dict(code=code, message=message, feed=feed) diff --git a/apps/search/models.py b/apps/search/models.py index 78408affde..a4af165b5b 100644 --- a/apps/search/models.py +++ b/apps/search/models.py @@ -10,6 +10,7 @@ from django.contrib.auth.models import User from apps.search.tasks import IndexSubscriptionsForSearch from apps.search.tasks import IndexSubscriptionsChunkForSearch +from apps.search.tasks import IndexFeedsForSearch from utils import log as logging from utils.feed_functions import chunks @@ -28,12 +29,15 @@ class MUserSearch(mongo.Document): } @classmethod - def get_user(cls, user_id): + def get_user(cls, user_id, create=True): try: user_search = cls.objects.read_preference(pymongo.ReadPreference.PRIMARY)\ .get(user_id=user_id) except cls.DoesNotExist: - user_search = cls.objects.create(user_id=user_id) + if create: + user_search = cls.objects.create(user_id=user_id) + else: + user_search = None return user_search @@ -107,6 +111,33 @@ def index_subscriptions_chunk_for_search(self, feed_ids): r.publish(user.username, 'search_index_complete:feeds:%s' % ','.join([str(f) for f in feed_ids])) + + @classmethod + def schedule_index_feeds_for_search(cls, feed_ids, user_id): + user_search = cls.get_user(user_id, create=False) + if (not user_search or + not user_search.subscriptions_indexed or + user_search.subscriptions_indexing): + # User hasn't searched before. + return + + if not isinstance(feed_ids, list): + feed_ids = [feed_ids] + IndexFeedsForSearch.apply_async(kwargs=dict(feed_ids=feed_ids, user_id=user_id), + queue='search_indexer') + + @classmethod + def index_feeds_for_search(cls, feed_ids, user_id): + from apps.rss_feeds.models import Feed + user = User.objects.get(pk=user_id) + + logging.user(user, "~SB~FCIndexing %s~FC by request..." % feed_ids) + + for feed_id in feed_ids: + feed = Feed.get_by_id(feed_id) + if not feed: continue + + feed.index_stories_for_search() @classmethod def remove_all(cls): diff --git a/apps/search/tasks.py b/apps/search/tasks.py index b99e2f3ce7..7dfbbe8b2b 100644 --- a/apps/search/tasks.py +++ b/apps/search/tasks.py @@ -17,3 +17,10 @@ def run(self, feed_ids, user_id): user_search = MUserSearch.get_user(user_id) user_search.index_subscriptions_chunk_for_search(feed_ids) + +class IndexFeedsForSearch(Task): + + def run(self, feed_ids, user_id): + from apps.search.models import MUserSearch + + MUserSearch.index_feeds_for_search(feed_ids, user_id) \ No newline at end of file diff --git a/settings.py b/settings.py index d89e59d8b1..df9c46e8b6 100644 --- a/settings.py +++ b/settings.py @@ -430,7 +430,7 @@ }, 'activate-next-new-user': { 'task': 'activate-next-new-user', - 'schedule': datetime.timedelta(minutes=10), + 'schedule': datetime.timedelta(minutes=5), 'options': {'queue': 'beat_tasks'}, }, }