Add a little disaster recovery

This commit is contained in:
Neale Pickett 2009-10-07 08:31:54 -06:00
parent 36daf80cf7
commit 15a2009506
2 changed files with 14 additions and 2 deletions

View File

@ -90,7 +90,7 @@ def incdict(dict, key, amt=1):
dict[key] = dict.get(key, 0) + amt
class Storage:
def __init__(self, fn=None):
def __init__(self, fn=None, fix=False):
if not fn:
fn = config.datafile('scores.dat')
self.teams = set()
@ -100,6 +100,8 @@ class Storage:
self.f = io.BytesIO()
# Read stored scores
truncate = False
lastgood = 0
try:
f = open(fn, 'rb')
while True:
@ -113,7 +115,13 @@ class Storage:
team = b[catlen:].decode('utf-8')
req = (when, cat, team, score)
self.add(req, False)
lastgood = f.tell()
f.close()
except struct.error:
if fix:
truncate = True
else:
raise
except IOError:
pass
@ -122,6 +130,10 @@ class Storage:
except IOError:
self.f = None
if truncate:
self.f.seek(lastgood)
self.f.truncate()
def __len__(self):
return len(self.log)

View File

@ -14,7 +14,7 @@ class MyHandler(asyncore.dispatcher):
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_DGRAM)
self.bind(('', port))
self.store = points.Storage()
self.store = points.Storage(fix=True)
self.acked = set()
self.outq = []