From 88bb72e983356eba9cc3c3464ff18ed788443c06 Mon Sep 17 00:00:00 2001 From: Nicolas Setton Date: Sat, 21 Jul 2018 04:02:09 -0400 Subject: [PATCH] Clear up database sessions that are more than 1 min old --- compile_server/app/process_handling.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/compile_server/app/process_handling.py b/compile_server/app/process_handling.py index a0f0402..2d1ffd3 100644 --- a/compile_server/app/process_handling.py +++ b/compile_server/app/process_handling.py @@ -23,6 +23,9 @@ from compile_server.app.models import ProgramRun TIMEOUT_SECONDS = 30 # Number of seconds to allow to a process +MAX_SESSION_AGE = 60 +# Number of seconds after which to remove a session from memory + class SeparateProcess(object): @@ -168,9 +171,17 @@ class ProcessReader(object): def cleanup_old_processes(): """Cleanup the list of running processes""" for a in ProgramRun.objects.all(): - print a.timestamp, a.working_dir + # Uncomment this to print the running sessions + # print "running:", a.timestamp, a.working_dir + # Remove from the database all the processes where the working dir does # no longer exist - if not os.path.exists(a.working_dir): - print "deleting because dir has been cleared", a.working_dir + now = time.time() + if os.path.exists(a.working_dir): + if now - os.path.getctime(a.working_dir) > MAX_SESSION_AGE: + print "deleting dir because it is too old:", a.working_dir + shutil.rmtree(a.working_dir) + a.delete() + else: + print "deleting because dir has been cleared:", a.working_dir a.delete()