aboutsummaryrefslogtreecommitdiff
path: root/api/game/cleanup.py
blob: 3c285e11b64a95a923b7eb78ec08df985520a749 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from db import cursor, connection
import threading
import time

def cleanup():
    now = int( time.time() * 1000 )
    old_games = cursor.execute("select game_id from games where (status = \"wait_for_opponent\" or status = \"in_progress\") and last_activity < ?", [now - 5 * 60 * 1e3]).fetchall()
    for game_id in old_games:
        cursor.execute("delete from games where game_id = ?", [game_id[0]])
    connection.commit()

def set_interval(func, sec): # https://stackoverflow.com/questions/2697039/python-equivalent-of-setinterval
    def func_wrapper():
        set_interval(func, sec)
        func()
    t = threading.Timer(sec, func_wrapper)
    t.start()
    return t

set_interval(cleanup, 5 * 60)