Skip to content

Commit fad232b

Browse files
authored
Merge pull request #14 from GREENRAT-K405/fix/garbage-collection-lockfiles
Add lightweight daemon thread to periodically cleanup garbage
2 parents e79aecf + d9fbfc0 commit fad232b

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

server/Server.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
from flask import jsonify
88
import shutil
99
from filelock import FileLock
10+
import threading
11+
import time
1012

1113
TRIMMED_LOGS = False
1214

@@ -25,6 +27,16 @@
2527
SEPARATOR = "/"
2628
WORKDIR = "userfiles/"
2729

30+
CLEANUP_PERIOD=3600
31+
#lightweight daemon to clear abandoned sessions and lockfiles
32+
def gc_sessions():
33+
while True:
34+
time.sleep(CLEANUP_PERIOD) #run periodically
35+
for directory in glob.glob(WORKDIR + "*"):
36+
if os.path.isdir(directory) and time.time() - os.path.getmtime(directory) > CLEANUP_PERIOD:
37+
shutil.rmtree(directory, ignore_errors=True)
38+
threading.Thread(target=gc_sessions, daemon=True).start()
39+
2840
@app.route('/')
2941
def hello_world():
3042
return render_template('home.html')

0 commit comments

Comments
 (0)