← Back to team overview

widelands-dev team mailing list archive

[Merge] lp:~widelands-dev/widelands-website/fix_online_users into lp:widelands-website

 

kaputtnik has proposed merging lp:~widelands-dev/widelands-website/fix_online_users into lp:widelands-website.

Commit message:
Fix the last server error (meanwhile i should know that getting a dicts key should be done inside a try statement ;-) )

The fix for the linked bug is a bit complicated:

1. online_users_middleware is working with a cache from django
2. Django has a default Cache called 'Local memory cache': https://docs.djangoproject.com/en/1.11/topics/cache/#local-memory-caching
This cache isn't meant to be used in production, something that i have overlooked. Not really sure, but i think the main reason why this cache do not work correctly is that each process has its own cache. Locally there is only one process running, but in production there are several processes running (gunicorn workers). Each time one hits F5 to refresh the page, a different process is responding and so each time a different cache is used, resulting in different 'online users'.

The solution is to define another cache than django's default in the settings. There are several caches available (see also the link before). I have chosen the database cache now, because tracking was also database related and is easy to use. I have added this in local_settings.py.sample with a short explanation. On the server we have to adjust local_settings py by hand.

The database cache solution was shortly tested on the alpha site, and i think the 'Currently online' box works correct now.

To get the database cache working:

1. Add the setting CACHES to local_settings.py (like in local_settings.py.sample)
2. Run 'python manage.py createcachetable'
3. Restart wlwebsite

Requested reviews:
  Widelands Developers (widelands-dev)
Related bugs:
  Bug #1769235 in Widelands Website: "Currently online  users do not work correct"
  https://bugs.launchpad.net/widelands-website/+bug/1769235

For more details, see:
https://code.launchpad.net/~widelands-dev/widelands-website/fix_online_users/+merge/345132
-- 
Your team Widelands Developers is requested to review the proposed merge of lp:~widelands-dev/widelands-website/fix_online_users into lp:widelands-website.
=== modified file 'local_settings.py.sample'
--- local_settings.py.sample	2018-04-12 20:56:17 +0000
+++ local_settings.py.sample	2018-05-05 19:43:41 +0000
@@ -37,7 +37,6 @@
    }
 }
 
-
 # The following are just dummy values, but needed defined
 # To use the registration you have to create an API key pair
 # See https://developers.google.com/recaptcha/docs/start
@@ -59,6 +58,19 @@
 ANTI_SPAM_PHONE_NR = re.compile('\d{8,16}')
 MAX_HIDDEN_POSTS = 5
 
+#######################
+#  Optional settings  #
+#######################
+
+# Set a Database cache. You won't need this for development or testing locally.
+# If you want to use this, run ./manage.py createcachetable after uncommenting.
+# CACHES = {
+#     'default': {
+#         'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
+#         'LOCATION': 'wl_cache',
+#     }
+# }
+
 # Uncomment 'LOGGING = {...}' for debugging purposes when you have set DEBUG=False.
 # Use then in the code:
 

=== modified file 'online_users_middleware.py'
--- online_users_middleware.py	2018-05-02 06:33:13 +0000
+++ online_users_middleware.py	2018-05-05 19:43:41 +0000
@@ -15,7 +15,10 @@
 
 @receiver(user_logged_out)
 def logout(sender, **kwargs):
-    cache.delete('online-%s' % kwargs['user'].id)
+    try:
+        cache.delete('online-%s' % kwargs['user'].id)
+    except KeyError:
+        pass
 
 
 class OnlineNowMiddleware(MiddlewareMixin):


Follow ups