1
0
mirror of https://github.com/ansible/awx.git synced 2024-11-01 08:21:15 +03:00

Avoid unnecessary user get expiring session memberships

This commit is contained in:
AlanCoding 2019-05-16 08:23:22 -04:00
parent ab1e45d6c4
commit 1223148116
No known key found for this signature in database
GPG Key ID: FD2C3C012A72926B
2 changed files with 11 additions and 12 deletions

View File

@ -177,14 +177,14 @@ class UserSessionMembership(BaseModel):
created = models.DateTimeField(default=None, editable=False) created = models.DateTimeField(default=None, editable=False)
@staticmethod @staticmethod
def get_memberships_over_limit(user, now=None): def get_memberships_over_limit(user_id, now=None):
if settings.SESSIONS_PER_USER == -1: if settings.SESSIONS_PER_USER == -1:
return [] return []
if now is None: if now is None:
now = tz_now() now = tz_now()
query_set = UserSessionMembership.objects\ query_set = UserSessionMembership.objects\
.select_related('session')\ .select_related('session')\
.filter(user=user)\ .filter(user_id=user_id)\
.order_by('-created') .order_by('-created')
non_expire_memberships = [x for x in query_set if x.session.expire_date > now] non_expire_memberships = [x for x in query_set if x.session.expire_date > now]
return non_expire_memberships[settings.SESSIONS_PER_USER:] return non_expire_memberships[settings.SESSIONS_PER_USER:]

View File

@ -652,20 +652,19 @@ def save_user_session_membership(sender, **kwargs):
return return
if not session: if not session:
return return
user = session.get_decoded().get(SESSION_KEY, None) user_id = session.get_decoded().get(SESSION_KEY, None)
if not user: if not user_id:
return return
user = User.objects.get(pk=user) if UserSessionMembership.objects.filter(user=user_id, session=session).exists():
if UserSessionMembership.objects.filter(user=user, session=session).exists():
return return
UserSessionMembership(user=user, session=session, created=timezone.now()).save() UserSessionMembership(user_id=user_id, session=session, created=timezone.now()).save()
expired = UserSessionMembership.get_memberships_over_limit(user) expired = UserSessionMembership.get_memberships_over_limit(user_id)
for membership in expired: for membership in expired:
Session.objects.filter(session_key__in=[membership.session_id]).delete() Session.objects.filter(session_key__in=[membership.session_id]).delete()
membership.delete() membership.delete()
if len(expired): if len(expired):
consumers.emit_channel_notification( consumers.emit_channel_notification(
'control-limit_reached_{}'.format(user.pk), 'control-limit_reached_{}'.format(user_id),
dict(group_name='control', reason='limit_reached') dict(group_name='control', reason='limit_reached')
) )