Skip to content

Commit

Permalink
Indexing newly added feeds for users who need it.
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelclay committed Apr 23, 2014
1 parent de4e641 commit eac58cd
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
2 changes: 1 addition & 1 deletion apps/reader/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
35 changes: 33 additions & 2 deletions apps/search/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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):
Expand Down
7 changes: 7 additions & 0 deletions apps/search/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'},
},
}
Expand Down

0 comments on commit eac58cd

Please sign in to comment.