avoid stale db sessoins in nodes thread

This commit is contained in:
j 2016-01-12 13:02:39 +05:30
parent cf74e622be
commit cf6d6acc5f
3 changed files with 64 additions and 80 deletions

View file

@ -116,6 +116,8 @@ class Changelog(db.Model):
if trigger: if trigger:
trigger_event('change', {}); trigger_event('change', {});
return True return True
else:
logger.debug('could not apply change')
else: else:
logger.debug('revsion does not match! got %s expecting %s', revision, next_revision) logger.debug('revsion does not match! got %s expecting %s', revision, next_revision)
return False return False

View file

@ -348,7 +348,8 @@ class Item(db.Model):
load metadata from user_metadata or get via isbn? load metadata from user_metadata or get via isbn?
''' '''
for key in self.meta_keys: for key in self.meta_keys:
if key not in self.meta and key in self.info: if key in self.info:
if key not in self.meta:
self.meta[key] = self.info[key] self.meta[key] = self.info[key]
del self.info[key] del self.info[key]

View file

@ -44,6 +44,7 @@ class Node(Thread):
def __init__(self, nodes, user): def __init__(self, nodes, user):
self._nodes = nodes self._nodes = nodes
self.user = user
self.user_id = user.id self.user_id = user.id
self._opener = get_opener(self.user_id) self._opener = get_opener(self.user_id)
self._q = Queue() self._q = Queue()
@ -200,32 +201,9 @@ class Node(Thread):
state.update_required = True state.update_required = True
return None return None
'''
sig = r.headers.get('X-Node-Signature')
if sig and self._valid(data, sig):
response = json.loads(data.decode('utf-8'))
else:
logger.debug('invalid signature %s', data)
response = None
'''
response = json.loads(data.decode('utf-8')) response = json.loads(data.decode('utf-8'))
return response return response
def _valid(self, data, sig):
if isinstance(data, str):
data = data.encode()
try:
self.vk.verify(sig, data, encoding=ENCODING)
#except ed25519.BadSignatureError:
except:
return False
return True
@property
def user(self):
with db.session():
return user.models.User.get_or_create(self.user_id)
def can_connect(self): def can_connect(self):
self.resolve() self.resolve()
url = self.url url = self.url
@ -244,10 +222,10 @@ class Node(Thread):
logger.debug('version does not match local: %s remote %s', settings.NODE_PROTOCOL, version) logger.debug('version does not match local: %s remote %s', settings.NODE_PROTOCOL, version)
return False return False
c = r.read() c = r.read()
logger.debug('can connect to: %s (%s)', url, self.user.nickname) logger.debug('can connect to: %s', url)
return True return True
except: except:
logger.debug('can not connect to: %s (%s)', url, self.user.nickname) logger.debug('can not connect to: %s', url)
pass pass
return False return False
@ -255,7 +233,8 @@ class Node(Thread):
return self.online or self.get_local() != None return self.online or self.get_local() != None
def _go_online(self): def _go_online(self):
u = self.user with db.session():
u = user.models.User.get_or_create(self.user_id)
if u.peered or u.queued: if u.peered or u.queued:
logger.debug('go_online peered=%s queued=%s %s (%s)', u.peered, u.queued, u.id, u.nickname) logger.debug('go_online peered=%s queued=%s %s (%s)', u.peered, u.queued, u.id, u.nickname)
try: try:
@ -285,7 +264,9 @@ class Node(Thread):
}) })
def pullChanges(self): def pullChanges(self):
if not self.online or not self.user.peered: with db.session():
u = user.models.User.get_or_create(self.user_id)
if not self.online or not u.peered:
return True return True
last = Changelog.query.filter_by(user_id=self.user_id).order_by('-revision').first() last = Changelog.query.filter_by(user_id=self.user_id).order_by('-revision').first()
from_revision = last.revision + 1 if last else 0 from_revision = last.revision + 1 if last else 0
@ -293,12 +274,11 @@ class Node(Thread):
changes = self.request('pullChanges', from_revision) changes = self.request('pullChanges', from_revision)
except: except:
self.online = False self.online = False
logger.debug('%s went offline', self.user.name) logger.debug('%s went offline', u.name)
return False return False
if not changes: if not changes:
return False return False
with db.session(): r = Changelog.apply_changes(u, changes)
r = Changelog.apply_changes(self.user, changes)
return r return r
def pushChanges(self, changes): def pushChanges(self, changes):
@ -312,7 +292,8 @@ class Node(Thread):
logger.debug('pushedChanges %s %s', r, self.user_id) logger.debug('pushedChanges %s %s', r, self.user_id)
def peering(self, action): def peering(self, action):
u = self.user with db.session():
u = user.models.User.get_or_create(self.user_id)
if action in ('requestPeering', 'acceptPeering'): if action in ('requestPeering', 'acceptPeering'):
r = self.request(action, settings.preferences['username'], u.info.get('message')) r = self.request(action, settings.preferences['username'], u.info.get('message'))
else: else: