add celery with task clearing task

This commit is contained in:
prance 2022-01-30 16:32:09 +01:00
parent 0d5201a575
commit 14a2e7d185
4 changed files with 28 additions and 6 deletions

View File

@ -25,7 +25,7 @@ SECRET_KEY = 'django-insecure-t52#vo-k9ty*$@u9bf75hrkd#^o_)gadrz9$7w%xnkb-0#y!bi
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ['django']
ALLOWED_HOSTS = ['django', 'localhost']
# Application definition
@ -139,4 +139,14 @@ AUTH_USER_MODEL = "MUOR.Profile"
AVAILABLE_PORTS_RANGE = (6000, 7000)
SESSION_EXPIRE_AT_BROWSER_CLOSE = True
SESSION_COOKIE_AGE = 60 * 60
CELERY_BROKER_URL = 'redis+socket:///var/run/redis/redis-server.sock'
CELERY_TIMEZONE = "Europe/Warsaw"
CELERY_BEAT_SCHEDULE = {
'daily_report': {
'task': 'clear_sessions',
'schedule': 60,
}
}

View File

@ -1,10 +1,11 @@
from django.db.models.signals import post_save
from django.db.models.signals import post_save, pre_delete
from django.dispatch import receiver
from .DockerManager import DockerManager
from .NGINXConfigurator import NGINXConfigurator
from .models import Profile, Session
from django.contrib.auth.signals import user_logged_in, user_logged_out
from django.conf import settings
from django.contrib.sessions.models import Session as DjangoSession
def get_sessions_data():
@ -57,11 +58,11 @@ def profile_start_up(sender, user, request, **kwargs):
NGINXConfigurator.refresh_config(get_sessions_data())
@receiver(user_logged_out)
def profile_logged_out(sender, user, request, **kwargs):
@receiver(pre_delete, sender=DjangoSession)
def close_session(sender, instance, **kwargs):
# Obtaining container_id
try:
session = Session.objects.get(user=user)
session = Session.objects.get(sessionid=instance.session_key)
container_id = session.container_id
# Turn off and delete container
@ -70,6 +71,7 @@ def profile_logged_out(sender, user, request, **kwargs):
# Delete session
session.delete()
print("Closed session!")
except Session.DoesNotExist:
pass
NGINXConfigurator.refresh_config(get_sessions_data())

7
MUOR/MUOR/tasks.py Normal file
View File

@ -0,0 +1,7 @@
from celery import shared_task
from django.core import management
@shared_task(name="clear_sessions")
def clear_sessions():
management.call_command('clearsessions')

View File

@ -1,3 +1,6 @@
Django
django-crispy-forms
docker
celery
redis
django-redis