|
1 | 1 | import datetime |
2 | 2 | import json |
3 | | -import logging |
4 | 3 | from math import ceil |
5 | 4 |
|
6 | 5 | from PyMatcha import celery |
|
20 | 19 |
|
21 | 20 | @celery.on_after_configure.connect |
22 | 21 | def setup_periodic_tasks(sender, **kwargs): |
23 | | - sender.add_periodic_task(60, update_offline_users.s(), name="Update online users every minute") |
| 22 | + sender.add_periodic_task(60, take_users_offline.s(), name="Update online users every minute") |
24 | 23 | sender.add_periodic_task(3600, update_heat_scores.s(), name="Update heat scores every hour") |
25 | 24 | sender.add_periodic_task(60, update_user_recommendations.s(), name="Update user recommendations every minute") |
| 25 | + sender.add_periodic_task(30, take_random_users_online.s(), name="Set 100 random users online every 30 seconds") |
26 | 26 | sender.add_periodic_task( |
27 | 27 | 600, calc_search_min_max.s(), name="Update Minimum and Maximum scores and ages for search every 10 minutes" |
28 | 28 | ) |
@@ -60,47 +60,19 @@ def update_heat_scores(): |
60 | 60 |
|
61 | 61 |
|
62 | 62 | @celery.task |
63 | | -def update_offline_users(): |
64 | | - logging.debug("Updating offline users") |
65 | | - # Get the last login deadline |
66 | | - login_deadline_timestamp = float(datetime.datetime.utcnow().timestamp()) - 120 |
67 | | - count = 0 |
68 | | - online_count = 0 |
69 | | - offline_count = 0 |
70 | | - # For all user keys |
71 | | - for key in redis.scan_iter("online_user:*"): |
72 | | - # Get the user id |
73 | | - user_id = str(key).split(":")[1] |
74 | | - date_lastseen = float(redis.get(key)) |
75 | | - # If the user has passed the deadline |
76 | | - if date_lastseen < login_deadline_timestamp: |
77 | | - u = User.get(id=user_id) |
78 | | - if u: |
79 | | - u.date_lastseen = datetime.datetime.fromtimestamp(date_lastseen) |
80 | | - u.is_online = False |
81 | | - u.save() |
82 | | - offline_count += 1 |
83 | | - # delete the key in redis, setting the user as offline in redis |
84 | | - redis.delete(key) |
85 | | - count += 1 |
| 63 | +def take_users_offline(): |
| 64 | + went_offline_count = 0 |
| 65 | + stayed_online_count = 0 |
| 66 | + for user in User.get_multis(is_online=True): |
| 67 | + if user.date_lastseen + datetime.timedelta(minutes=2) < datetime.datetime.utcnow(): |
| 68 | + user.is_online = False |
| 69 | + user.save() |
| 70 | + went_offline_count += 1 |
86 | 71 | else: |
87 | | - u = User.get(id=user_id) |
88 | | - if not u: |
89 | | - # Edge case where the user has been deleted from DB while he was still online |
90 | | - redis.delete(key) |
91 | | - else: |
92 | | - u.date_lastseen = datetime.datetime.fromtimestamp(date_lastseen) |
93 | | - u.is_online = True |
94 | | - u.save() |
95 | | - online_count += 1 |
96 | | - count += 1 |
97 | | - logging.debug( |
98 | | - "Updated online status for {} users. {} passed offline and {} passed or stayed online.".format( |
99 | | - count, offline_count, online_count |
100 | | - ) |
101 | | - ) |
102 | | - return "Updated online status for {} users. {} passed offline and {} passed or stayed online.".format( |
103 | | - count, offline_count, online_count |
| 72 | + stayed_online_count += 1 |
| 73 | + return ( |
| 74 | + f"{stayed_online_count} stayed online and " |
| 75 | + f"{went_offline_count} went offline for {went_offline_count + stayed_online_count} users" |
104 | 76 | ) |
105 | 77 |
|
106 | 78 |
|
@@ -143,3 +115,15 @@ def calc_search_min_max(): |
143 | 115 | minmax = {"min_score": min_score, "max_score": max_score, "min_age": min_age, "max_age": max_age} |
144 | 116 | redis.set("search_minmax", json.dumps(minmax)) |
145 | 117 | return "Successfully updated min and max ages and scores" |
| 118 | + |
| 119 | + |
| 120 | +@celery.task |
| 121 | +def take_random_users_online(): |
| 122 | + for user in User.select_random(100): |
| 123 | + if not user.skip_recommendations: |
| 124 | + # User isn't a bot, so skip him |
| 125 | + continue |
| 126 | + user.is_online = True |
| 127 | + user.date_lastseen = datetime.datetime.utcnow() |
| 128 | + user.save() |
| 129 | + return "Successfully set 100 users online" |
0 commit comments