diff --git a/server/invites-manager/invites-routine.py b/server/invites-manager/invites-routine.py index 27eb9c3..fb9dd36 100644 --- a/server/invites-manager/invites-routine.py +++ b/server/invites-manager/invites-routine.py @@ -1,4 +1,46 @@ # Chronjob which is meant to remove old/unwanted invites from our database +import mysql.connector as mysql +from os import getenv +import time, sys +code_name = 'INVITES_MANAGER' + +def remove_old(config): + global code_name + ''' + Removes invites that are out of date + ''' + now = int(time.time()) + query = ('DELETE FROM invites WHERE expires < ?') + + conn = mysql.connect(**config) + cursor = conn.cursor(prepared=True) + + cursor.execute(query, (now,)) + + cursor.close() + conn.close() + print(f'[ {code_name} ] : Removed old invites with no errors') + + +if __name__ == '__main__': + fail = False + config = { + 'database': getenv('DATABASE_NAME'), + 'password': getenv('DATABASE_PASS'), + 'user': getenv('DATABASE_USER'), + 'host': getenv('DATABASE_HOST'), + 'port': int(getenv('DATABASE_PORT')), + } + for k in config: + if config[k] is None: + print(f'[ {code_name} ] : {k} not set', file=sys.stderr) + fail = True + + + if not fail: + remove_old(config) + else: + exit(1) diff --git a/server/invites-manager/readme.md b/server/invites-manager/readme.md index e595a01..494ef27 100644 --- a/server/invites-manager/readme.md +++ b/server/invites-manager/readme.md @@ -1,4 +1,6 @@ # Invites Manager -Chron-job which runs every few minutes to clean out invites table from \ No newline at end of file +Chron-job which runs every few minutes to clean out invites table from our invites table + + diff --git a/server/invites-manager/tests/clean.sh b/server/invites-manager/tests/clean.sh new file mode 100644 index 0000000..e429141 --- /dev/null +++ b/server/invites-manager/tests/clean.sh @@ -0,0 +1,17 @@ +#!/bin/sh + +# Simple test to make sure we can actually remove stuff from mysql +# without the whole thing complaining about everything + + +. ./bin/activate + +export DATABASE_URL=mysql://freechat_dev:password@localhost:3306/freechat +export DATABASE_NAME=freechat +export DATABASE_PASS=password +export DATABASE_USER=freechat_dev +export DATABASE_HOST=localhost +export DATABASE_PORT=3306 + +python invites-routine.py +