56 lines
1.7 KiB
Python
56 lines
1.7 KiB
Python
|
#!./venv/bin/python3
|
||
|
|
||
|
# This script clears expired sessions in a regular interval
|
||
|
|
||
|
import os
|
||
|
|
||
|
from argparse import ArgumentParser
|
||
|
from atexit import register as register_exithandler
|
||
|
from pathlib import Path
|
||
|
from subprocess import Popen
|
||
|
from time import sleep
|
||
|
from datetime import datetime
|
||
|
|
||
|
|
||
|
current_proc = None
|
||
|
|
||
|
|
||
|
def exithandler():
|
||
|
if current_proc is not None:
|
||
|
seconds_waited = 0
|
||
|
while current_proc.poll() is None:
|
||
|
# wait for 10 seconds to quit session cleaner
|
||
|
if seconds_waited >= 10:
|
||
|
current_proc.terminate()
|
||
|
break
|
||
|
# is still running
|
||
|
sleep(1)
|
||
|
seconds_waited += 1
|
||
|
print("Stopped session-autocleaner.")
|
||
|
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
try:
|
||
|
argp = ArgumentParser()
|
||
|
argp.add_argument("interval", help="The interval in minutes", type=int)
|
||
|
args = argp.parse_args()
|
||
|
os.chdir(str(Path(__file__).parent.parent))
|
||
|
print(f"Started session-autocleaner with an interval of {args.interval} minute(s)")
|
||
|
interval = args.interval * 60
|
||
|
# register exithandler that cleans up stuff
|
||
|
register_exithandler(exithandler)
|
||
|
# main loop
|
||
|
while True:
|
||
|
if current_proc is not None:
|
||
|
# wait for last iteration
|
||
|
while current_proc.poll() is None:
|
||
|
# is still running
|
||
|
print("Last cleanup is still running, waiting before clearing sessions...")
|
||
|
sleep(1)
|
||
|
print(f"Clearing expired sessions at {datetime.now()}...")
|
||
|
current_proc = Popen(
|
||
|
["./manage.py", "clearsessions"])
|
||
|
sleep(interval)
|
||
|
except KeyboardInterrupt:
|
||
|
exit()
|