mirror of https://github.com/dirtbags/moth.git
Add a little disaster recovery
This commit is contained in:
parent
36daf80cf7
commit
15a2009506
|
@ -90,7 +90,7 @@ def incdict(dict, key, amt=1):
|
||||||
dict[key] = dict.get(key, 0) + amt
|
dict[key] = dict.get(key, 0) + amt
|
||||||
|
|
||||||
class Storage:
|
class Storage:
|
||||||
def __init__(self, fn=None):
|
def __init__(self, fn=None, fix=False):
|
||||||
if not fn:
|
if not fn:
|
||||||
fn = config.datafile('scores.dat')
|
fn = config.datafile('scores.dat')
|
||||||
self.teams = set()
|
self.teams = set()
|
||||||
|
@ -100,6 +100,8 @@ class Storage:
|
||||||
self.f = io.BytesIO()
|
self.f = io.BytesIO()
|
||||||
|
|
||||||
# Read stored scores
|
# Read stored scores
|
||||||
|
truncate = False
|
||||||
|
lastgood = 0
|
||||||
try:
|
try:
|
||||||
f = open(fn, 'rb')
|
f = open(fn, 'rb')
|
||||||
while True:
|
while True:
|
||||||
|
@ -113,7 +115,13 @@ class Storage:
|
||||||
team = b[catlen:].decode('utf-8')
|
team = b[catlen:].decode('utf-8')
|
||||||
req = (when, cat, team, score)
|
req = (when, cat, team, score)
|
||||||
self.add(req, False)
|
self.add(req, False)
|
||||||
|
lastgood = f.tell()
|
||||||
f.close()
|
f.close()
|
||||||
|
except struct.error:
|
||||||
|
if fix:
|
||||||
|
truncate = True
|
||||||
|
else:
|
||||||
|
raise
|
||||||
except IOError:
|
except IOError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@ -122,6 +130,10 @@ class Storage:
|
||||||
except IOError:
|
except IOError:
|
||||||
self.f = None
|
self.f = None
|
||||||
|
|
||||||
|
if truncate:
|
||||||
|
self.f.seek(lastgood)
|
||||||
|
self.f.truncate()
|
||||||
|
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
return len(self.log)
|
return len(self.log)
|
||||||
|
|
||||||
|
|
|
@ -14,7 +14,7 @@ class MyHandler(asyncore.dispatcher):
|
||||||
asyncore.dispatcher.__init__(self)
|
asyncore.dispatcher.__init__(self)
|
||||||
self.create_socket(socket.AF_INET, socket.SOCK_DGRAM)
|
self.create_socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||||
self.bind(('', port))
|
self.bind(('', port))
|
||||||
self.store = points.Storage()
|
self.store = points.Storage(fix=True)
|
||||||
self.acked = set()
|
self.acked = set()
|
||||||
self.outq = []
|
self.outq = []
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue