Lock periodic tasks by default
[cds-indico.git] / indico / modules / categories / suggestions.py
blob76298d0be69950d886f7836791f3023c0ae4028d
1 # This file is part of Indico.
2 # Copyright (C) 2002 - 2015 European Organization for Nuclear Research (CERN).
4 # Indico is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License as
6 # published by the Free Software Foundation; either version 3 of the
7 # License, or (at your option) any later version.
9 # Indico is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with Indico; if not, see <http://www.gnu.org/licenses/>.
17 from __future__ import unicode_literals
19 from celery.schedules import crontab
21 from indico.core.celery import celery
22 from indico.modules.categories import logger
23 from indico.modules.users import User
24 from indico.util.suggestions import get_category_scores
25 from indico.util.redis import write_client as redis_write_client
26 from indico.util.redis.suggestions import next_scheduled_check, suggest, unschedule_check
29 # Minimum score for a category to be suggested
30 SUGGESTION_MIN_SCORE = 0.25
33 @celery.periodic_task(name='update_category_suggestions', run_every=crontab(minute='0', hour='7'))
34 def update_category_suggestions():
35 if not redis_write_client:
36 return
37 while True:
38 user_id = next_scheduled_check()
39 if user_id is None:
40 break
41 user = User.get(int(user_id))
42 if user:
43 for category, score in get_category_scores(user).iteritems():
44 if score < SUGGESTION_MIN_SCORE:
45 continue
46 logger.debug('Suggesting {} with score {:.03} for {}'.format(category, score, user))
47 suggest(user, 'category', category.getId(), score)
48 unschedule_check(user_id)