Added locking for critical selects
Signed-off-by: fly <merspieler@airmail.cc>
This commit is contained in:
parent
a71010edc4
commit
fda27979ef
1 changed files with 17 additions and 0 deletions
|
@ -75,6 +75,18 @@ db = pymysql.connect("localhost", dbuser, dbpw, "worldbuild")
|
|||
|
||||
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):
|
||||
if verbose:
|
||||
print(name + " set status to " + status)
|
||||
|
@ -87,6 +99,7 @@ def set_state(name, status):
|
|||
print("WARNING: Invalid status '" + status + "' recived from " + name)
|
||||
else:
|
||||
match = re.match(r"[ew]\d{3}[ns]\d{2}", name)
|
||||
get_lock()
|
||||
if match != None:
|
||||
sql ="SELECT id FROM secondLevel WHERE name = '" + name + "'"
|
||||
cursor.execute(sql)
|
||||
|
@ -101,6 +114,7 @@ def set_state(name, status):
|
|||
else:
|
||||
sql = "UPDATE tile SET status_id = (SELECT id FROM status WHERE name = '" + status + "') WHERE id = " + name
|
||||
cursor.execute(sql)
|
||||
rel_lock()
|
||||
db.commit()
|
||||
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
|
@ -144,6 +158,7 @@ try:
|
|||
additional_type = ""
|
||||
if get != "pending":
|
||||
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"
|
||||
cursor.execute(sql)
|
||||
result = cursor.fetchone()
|
||||
|
@ -155,6 +170,7 @@ try:
|
|||
cursor.execute(sql)
|
||||
db.commit()
|
||||
|
||||
rel_lock()
|
||||
|
||||
if verbose:
|
||||
print("Asigning job on tile " + str(tile))
|
||||
|
@ -190,6 +206,7 @@ try:
|
|||
c.close()
|
||||
|
||||
except KeyboardInterrupt:
|
||||
rel_lock()
|
||||
sock.close()
|
||||
db.close()
|
||||
print("Exiting...")
|
||||
|
|
Loading…
Reference in a new issue