From: Max Value Date: Tue, 10 Dec 2024 04:24:15 +0000 (+0000) Subject: Added better locking multiprocess with database table X-Git-Url: https://git.ozva.co.uk/?a=commitdiff_plain;h=50bc17cfb53042a5480bd7f3671cc94ce191a60d;p=critters-api Added better locking multiprocess with database table --- diff --git a/critters.py b/critters.py index 9168354..943ae45 100755 --- a/critters.py +++ b/critters.py @@ -21,9 +21,6 @@ db_keys = ["title", "album", "artist", "year", "link", "info", "category", "play allowed_audio = ["mp3", "wav", "ogg", "flac"] allowed_image = ["png", "jpg", "jpeg"] -id_lock = threading.Lock() -current_track_id = 1 - critters_path = os.environ["CRITTERS_PATH"] audio_path = critters_path + "/audio/" cover_path = critters_path + "/cover/" @@ -32,6 +29,18 @@ upload_path = critters_path + "/upload/" templates_path = critters_path + "/critters-api/templates/" db_path = critters_path + "/../metadata.db" +def get_current_track_id(): + con = sqlite3.connect(db_path) + cur = con.cursor() + track_id = cur.execute(f"SELECT trackid FROM critters_global WHERE id=1;").fetchone() + return track_id[0] + +def set_current_track_id(track_id): + con = sqlite3.connect(db_path) + cur = con.cursor() + cur.execute(f"UPDATE critters_global SET trackid = {track_id} WHERE id=1;") + con.commit() + def check_files(track_id): return { "track_path": url_for("get_audio", track_id=track_id), @@ -190,7 +199,6 @@ def admin(): @app.route("/api/next") @auth.login_required def set_current_id(): - global current_track_id server_data = { # The value for each of the keys is the position in the database columns that peice of data can be found "id": 0, # (Note 1) "title": 1, @@ -199,8 +207,7 @@ def set_current_id(): "outtime": 11 } - with id_lock: - track_id = current_track_id + track_id = get_current_track_id() current_album = "" current_artist = "" @@ -228,8 +235,7 @@ def set_current_id(): if tracks is not None: target_index = random.randint(0, len(tracks)-1) - with id_lock: - current_track_id = tracks[target_index][0] + set_current_track_id(tracks[target_index][0]) for key, i in server_data.items(): server_data[key] = tracks[target_index][i] # See note 1 @@ -266,8 +272,7 @@ def get_single_track(track_id): global current_track_id if track_id == 0: - with id_lock: - track_id = current_track_id + track_id = get_current_track_id() con = sqlite3.connect(db_path) cur = con.cursor() track = cur.execute(f"SELECT * FROM critters WHERE id={track_id}").fetchone() @@ -296,8 +301,7 @@ def get_single_field(track_id, feild): global current_track_id if track_id == 0: - with id_lock: - track_id = current_track_id + track_id = get_current_track_id() result = "Unknown" if feild in db_keys: con = sqlite3.connect(db_path) @@ -336,4 +340,4 @@ def get_cover(track_id): return send_from_directory(cover_path, "default.png") if __name__ == "__main__": - app.run(host='127.0.0.1', port=5000, ssl_context='adhoc') + app.run(host='127.0.0.1', port=5000, debug=True) diff --git a/schema b/schema index fb6c3d3..8b4fc58 100644 --- a/schema +++ b/schema @@ -12,3 +12,10 @@ CREATE TABLE critters ( intime TEXT, outtime TEXT ); + +CREATE TABLE critters_global ( + id INTEGER PRIMARY KEY, + trackid INTEGER +); + +INSERT INTO critters_global (id, trackid) VALUES (1, 1);