]> OzVa Git service - critters-api/commitdiff
Added better locking multiprocess with database table
authorMax Value <greenwoodw50@gmail.com>
Tue, 10 Dec 2024 04:24:15 +0000 (04:24 +0000)
committerMax Value <greenwoodw50@gmail.com>
Tue, 10 Dec 2024 04:24:15 +0000 (04:24 +0000)
critters.py
schema

index 91683542de9f25b2a84f9a06a87b32f1671dac74..943ae459268a9e71f6504e3ce87fc7e1967de44e 100755 (executable)
@@ -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 fb6c3d3e12b4c86bb0bf105fcd8ff1b25b9980e4..8b4fc58b47068fc35b0f045cb7e71b69d2933dcd 100644 (file)
--- 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);