diff --git a/rxsmserver/__init__.py b/rxsmserver/__init__.py index 56c79f0..73a5174 100644 --- a/rxsmserver/__init__.py +++ b/rxsmserver/__init__.py @@ -19,6 +19,12 @@ def get_or_create_connection(): database_connections[thread_id] = dblib.connect(database_path) return database_connections[thread_id] +def close_connection(): + thread_id = get_ident() + if thread_id in database_connections: + database_connections[thread_id].close() + del(database_connections[thread_id]) + @app.route('/', methods=['GET']) def index(): return render_template('index.html') @@ -51,6 +57,7 @@ def create_or_modify_release(): cursor.execute('UPDATE releases SET url=?, created_date=? WHERE version=? and platform=?', (url, int(time()), version, platform)) operation = 'update' db_connection.commit() + close_connection() return jsonify({'status':200, 'reason':'Version %s %sd successfully' % (version, operation)}), 200 @@ -82,9 +89,11 @@ def check_for_update(): current_version = req_json['version'] platform = req_json['platform'] except: + close_connection() return jsonify({'status':400, 'reason':'Invalid request; missing parameter or invalid JSON'}), 400 cursor.execute('SELECT version, url FROM releases WHERE platform=? ORDER BY created_date DESC', (platform,)) result = cursor.fetchone() + close_connection() if result is None: return jsonify({'status':404, 'reason':'Platform not found (%ss)' % (time()-start), 'url':'', 'out-of-date':False}), 404 elif result[0] != current_version: @@ -98,7 +107,7 @@ cursor = conn.cursor() cursor.execute(\ '''CREATE TABLE IF NOT EXISTS releases ( id INTEGER PRIMARY KEY, - version TEXT UNIQUE NOT NULL, + version TEXT NOT NULL, url TEXT NOT NULL, platform TEXT NOT NULL, created_date INTEGER NOT NULL