Added locking for critical selects

Signed-off-by: fly <merspieler@airmail.cc>
This commit is contained in:
fly 2021-02-19 23:09:13 +00:00
parent a71010edc4
commit fda27979ef

View file

@ -75,6 +75,18 @@ db = pymysql.connect("localhost", dbuser, dbpw, "worldbuild")
cursor = db.cursor() cursor = db.cursor()
def get_lock():
ret = 0
while ret == 0:
sql = "SELECT GET_LOCK('tile-status-lock', 10)"
cursor.execute(sql)
ret = cursor.fetchone()
return
def rel_lock()
sql = "SELECT RELEASE_LOCK('tile-status-lock')"
cursor.execute(sql)
def set_state(name, status): def set_state(name, status):
if verbose: if verbose:
print(name + " set status to " + status) print(name + " set status to " + status)
@ -87,6 +99,7 @@ def set_state(name, status):
print("WARNING: Invalid status '" + status + "' recived from " + name) print("WARNING: Invalid status '" + status + "' recived from " + name)
else: else:
match = re.match(r"[ew]\d{3}[ns]\d{2}", name) match = re.match(r"[ew]\d{3}[ns]\d{2}", name)
get_lock()
if match != None: if match != None:
sql ="SELECT id FROM secondLevel WHERE name = '" + name + "'" sql ="SELECT id FROM secondLevel WHERE name = '" + name + "'"
cursor.execute(sql) cursor.execute(sql)
@ -101,6 +114,7 @@ def set_state(name, status):
else: else:
sql = "UPDATE tile SET status_id = (SELECT id FROM status WHERE name = '" + status + "') WHERE id = " + name sql = "UPDATE tile SET status_id = (SELECT id FROM status WHERE name = '" + status + "') WHERE id = " + name
cursor.execute(sql) cursor.execute(sql)
rel_lock()
db.commit() db.commit()
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
@ -144,6 +158,7 @@ try:
additional_type = "" additional_type = ""
if get != "pending": if get != "pending":
additional_type = "OR status_id = (SELECT id FROM status WHERE name = '" + get + "')" additional_type = "OR status_id = (SELECT id FROM status WHERE name = '" + get + "')"
get_lock()
sql = "SELECT id FROM tile WHERE status_id = (SELECT id FROM status WHERE name = 'pending') " + additional_type + " ORDER BY parent_id LIMIT 1" sql = "SELECT id FROM tile WHERE status_id = (SELECT id FROM status WHERE name = 'pending') " + additional_type + " ORDER BY parent_id LIMIT 1"
cursor.execute(sql) cursor.execute(sql)
result = cursor.fetchone() result = cursor.fetchone()
@ -155,6 +170,7 @@ try:
cursor.execute(sql) cursor.execute(sql)
db.commit() db.commit()
rel_lock()
if verbose: if verbose:
print("Asigning job on tile " + str(tile)) print("Asigning job on tile " + str(tile))
@ -190,6 +206,7 @@ try:
c.close() c.close()
except KeyboardInterrupt: except KeyboardInterrupt:
rel_lock()
sock.close() sock.close()
db.close() db.close()
print("Exiting...") print("Exiting...")