simplify the orders.queue locking logic, remove it from python

This commit is contained in:
Enno Rehling 2019-02-10 09:51:06 +01:00
parent 4edff66e61
commit a87e9b6c5c
5 changed files with 15 additions and 86 deletions

View File

@ -60,28 +60,6 @@ writeheaders = True
# reject all html email? # reject all html email?
rejecthtml = True rejecthtml = True
def unlock_file(filename):
try:
os.unlink(filename+".lock")
except:
print "could not unlock %s.lock, file not found" % filename
def lock_file(filename):
i = 0
wait = 1
if not os.path.exists(filename):
file=open(filename, "w")
file.close()
while True:
try:
os.symlink(filename, filename+".lock")
return
except:
i = i+1
if i == 5: unlock_file(filename)
sleep(wait)
wait = wait*2
messages = { messages = {
"multipart-en" : "multipart-en" :
"ERROR: The orders you sent contain no plaintext. " \ "ERROR: The orders you sent contain no plaintext. " \
@ -300,14 +278,12 @@ def accept(game, locale, stream, extend=None):
return -1 return -1
logger.info("received orders from " + email) logger.info("received orders from " + email)
# get an available filename # get an available filename
lock_file(gamedir + "/orders.queue")
maxdate, filename = available_file(savedir, prefix + email) maxdate, filename = available_file(savedir, prefix + email)
if filename is None: if filename is None:
logger.warning("more than " + str(maxfiles) + " orders from " + email) logger.warning("more than " + str(maxfiles) + " orders from " + email)
return -1 return -1
# copy the orders to the file # copy the orders to the file
text_ok = copy_orders(message, filename, email) text_ok = copy_orders(message, filename, email)
unlock_file(gamedir + "/orders.queue")
warning, msg, fail = None, "", False warning, msg, fail = None, "", False
maildate = message.get("Date") maildate = message.get("Date")
@ -331,10 +307,8 @@ def accept(game, locale, stream, extend=None):
os.unlink(filename) os.unlink(filename)
savedir = savedir + "/rejected" savedir = savedir + "/rejected"
if not os.path.exists(savedir): os.mkdir(savedir) if not os.path.exists(savedir): os.mkdir(savedir)
lock_file(gamedir + "/orders.queue")
maxdate, filename = available_file(savedir, prefix + email) maxdate, filename = available_file(savedir, prefix + email)
store_message(message, filename) store_message(message, filename)
unlock_file(gamedir + "/orders.queue")
fail = True fail = True
if sendmail and warning is not None: if sendmail and warning is not None:
@ -348,11 +322,9 @@ def accept(game, locale, stream, extend=None):
print filename print filename
if not fail: if not fail:
lock_file(gamedir + "/orders.queue")
queue = open(gamedir + "/orders.queue", "a") queue = open(gamedir + "/orders.queue", "a")
queue.write("email=%s file=%s locale=%s game=%s\n" % (email, filename, locale, game)) queue.write("email=%s file=%s locale=%s game=%s\n" % (email, filename, locale, game))
queue.close() queue.close()
unlock_file(gamedir + "/orders.queue")
logger.info("done - accepted orders from " + email) logger.info("done - accepted orders from " + email)

View File

@ -19,30 +19,21 @@ mkdir $REPORTS
cd $ERESSEA/game-$GAME cd $ERESSEA/game-$GAME
# wait for the queue lock to go away
maxt=5
to=5
while [ -e orders.queue.lock ] ; do
echo "waiting for orders to finish processing."
sleep $to
let to=$to+$to
let mast=$maxt-1
[ $maxt -lt 0 ] && break
done
if [ -d test ]; then if [ -d test ]; then
touch test/execute.lock touch test/execute.lock
fi fi
$BIN/create-orders $GAME $TURN $BIN/create-orders $GAME $TURN
if [ ! -s $ERESSEA/game-$GAME/orders.$TURN ]; then
echo "server did not create orders for turn $TURN in game $GAME"
exit 2
fi
$BIN/backup-eressea $GAME $TURN $BIN/backup-eressea $GAME $TURN
rm -f execute.lock rm -f execute.lock
$BIN/run-turn $GAME $TURN $BIN/run-turn $GAME $TURN
touch execute.lock touch execute.lock
if [ ! -s $ERESSEA/game-$GAME/orders.$TURN ]; then
echo "server did not create orders for turn $TURN in game $GAME"
exit 2
fi
if [ ! -s $REPORTS/reports.txt ]; then if [ ! -s $REPORTS/reports.txt ]; then
echo "server did not create reports.txt in game $GAME" echo "server did not create reports.txt in game $GAME"
exit 4 exit 4

View File

@ -1,5 +1,7 @@
#!/bin/sh #!/bin/sh
SCRIPT=$(readlink -f $0) SCRIPT=$(readlink -f $0)
cd $(dirname $SCRIPT) cd $(dirname $SCRIPT)
python accept-orders.py "$@"
lockfile -r3 -l120 orders.queue.lock
python accept-orders.py "$@"
rm -f orders.queue.lock

View File

@ -1,5 +1,7 @@
#!/bin/sh #!/bin/sh
SCRIPT=$(readlink -f $0) SCRIPT=$(readlink -f $0)
cd $(dirname $SCRIPT) cd $(dirname $SCRIPT)
python process-orders.py "$@"
lockfile -r3 -l120 orders.queue.lock
python process-orders.py "$@"
rm -f orders.queue.lock

View File

@ -21,29 +21,6 @@ def pwd_get_email(faction, pwd, pwdfile=None):
def split_filename(filename): def split_filename(filename):
return os.path.split(filename) return os.path.split(filename)
def unlock_file(filename):
try:
unlink(filename+".lock")
except:
print "could not unlock %s.lock, file not found" % filename
raise
def lock_file(filename):
i = 0
wait = 1
if not os.path.exists(filename):
file=open(filename, "w")
file.close()
while True:
try:
symlink(filename, filename+".lock")
return
except:
i = i+1
if i == 5:
raise
sleep(wait)
wait = wait*2
messages = { messages = {
"subject-de": "Befehle angekommen", "subject-de": "Befehle angekommen",
@ -138,11 +115,7 @@ except:
pw_data.load_file(os.path.join(game_dir, "passwd")) pw_data.load_file(os.path.join(game_dir, "passwd"))
#print "reading orders.queue..." #print "reading orders.queue..."
# move the queue file to a save space while locking it: # move the queue file to a safe space while locking it:
try:
lock_file(queue_file)
except:
exit(0)
queuefile = open(queue_file, "r") queuefile = open(queue_file, "r")
lines = queuefile.readlines() lines = queuefile.readlines()
queuefile.close() queuefile.close()
@ -150,10 +123,6 @@ queuefile.close()
# copy to a temp file # copy to a temp file
tname="/tmp/orders.queue.%s" % str(time()) tname="/tmp/orders.queue.%s" % str(time())
try:
lock_file(tname)
except:
exit(0)
tmpfile=open(tname, "w") tmpfile=open(tname, "w")
for line in lines: for line in lines:
tmpfile.write(line) tmpfile.write(line)
@ -161,12 +130,6 @@ tmpfile.close()
openlog("orders") openlog("orders")
unlink(queue_file)
try:
unlock_file(queue_file)
except:
pass
for line in lines: for line in lines:
tokens = split(line[:-1], ' ') tokens = split(line[:-1], ' ')
dict = {} dict = {}
@ -221,4 +184,3 @@ for line in lines:
closelog() closelog()
unlink(tname) unlink(tname)
unlock_file(tname)