diff --git a/app.py b/app.py index b4d97d2..9d7f2c9 100644 --- a/app.py +++ b/app.py @@ -1,3 +1,4 @@ +import datetime from time import sleep from flask import Flask, flash, make_response, redirect, send_file, jsonify, request, render_template, url_for import db @@ -19,21 +20,30 @@ ssh_thread = threading.Thread(target=ssh_thread_function) ssh_thread.start() class PingThread(threading.Thread): - def __init__(self, ip): + def __init__(self, ip, id): super(PingThread, self).__init__() self.Ip = ip + self.Id = id def run(self): - utils.ping_client(self.ip) + if utils.ping_client(self.Ip) == False: + date = db.get_image_allocation_time_id(self.Id) + if date is None: + return + delta = datetime.datetime.now() - date + if delta.total_seconds() > 30: + db.del_image_allocation_id(self.Id) + else: + db.update_image_allocation_time(self.Id) def check_allocation_thread_function(): while True: ids = db.get_image_allocation_all() for x in ids: ip = db.get_image_allocation_clientip_id(x) - ping_thread = PingThread(ip) + ping_thread = PingThread(ip, x) ping_thread.start() - sleep(15) + sleep(10) allocation_thread = threading.Thread(target=check_allocation_thread_function) allocation_thread.start() @@ -156,7 +166,21 @@ def add_image(): @app.route("/api/getconf") def get_image(): - filename = db.get_conf_image(request.headers['token']) + try: + filename = db.get_conf_image(request.headers['token']) + except: + pass + try: + date = db.get_image_allocation_time(request.headers['token']) + if date is not None: + delta = datetime.datetime.now() - date + if delta.total_seconds() > 30: + db.del_image_allocation_token(request.headers['token']) + else: + filename = None + except: + pass + if filename is None or filename == "": filename = "default.squashfs" diff --git a/db.py b/db.py index caf257f..04ce969 100644 --- a/db.py +++ b/db.py @@ -183,6 +183,17 @@ def get_image_allocation_time_imageid(image_id): except: return None +def get_image_allocation_time_id(id): + connect() + with get_cur() as cur: + cur.execute(""" + SELECT last_access_time FROM image_allocation WHERE id = %s + """,(id,)) + try: + return cur.fetchone()[0] + except: + return None + def get_image_allocation_clientip(token): id_image = get_conf_id(token) if id_image is None: @@ -221,4 +232,44 @@ def set_image_allocation(token, client_ip): VALUES (%s, %s, NOW()) """,(id_image,client_ip,)) conn.commit() - return token \ No newline at end of file + return token + +def del_image_allocation_token(token): + id_image = get_conf_id(token) + if id_image is None: + return None + + connect() + with get_cur() as cur: + cur.execute(""" + DELETE FROM image_allocation WHERE id_image = %s + """,(id_image,)) + try: + conn.commit() + return True + except: + return None + +def del_image_allocation_id(id): + connect() + with get_cur() as cur: + cur.execute(""" + DELETE FROM image_allocation WHERE id = %s + """,(id,)) + try: + conn.commit() + return True + except: + return None + +def update_image_allocation_time(id): + connect() + with get_cur() as cur: + cur.execute(""" + UPDATE SET last_access_time = NOW() FROM image_allocation WHERE id = %s + """,(id,)) + try: + conn.commit() + return True + except: + return None \ No newline at end of file