queue peering requests per node
This commit is contained in:
parent
148b41087f
commit
c95e22869c
4 changed files with 53 additions and 19 deletions
50
oml/nodes.py
50
oml/nodes.py
|
|
@ -37,10 +37,9 @@ class Node(Thread):
|
|||
_online = None
|
||||
TIMEOUT = 5
|
||||
|
||||
def __init__(self, nodes, user):
|
||||
def __init__(self, nodes, user_id):
|
||||
self._nodes = nodes
|
||||
self.user = user
|
||||
self.user_id = user.id
|
||||
self.user_id = user_id
|
||||
self._opener = get_opener(self.user_id)
|
||||
self._q = Queue()
|
||||
Thread.__init__(self)
|
||||
|
|
@ -57,6 +56,13 @@ class Node(Thread):
|
|||
self._send_response()
|
||||
elif action == 'ping':
|
||||
self.online = self.can_connect()
|
||||
elif isinstance(action, list) and len(action) == 2:
|
||||
if self.online:
|
||||
self._call(action[0], *action[1])
|
||||
else:
|
||||
if not self._q.qsize():
|
||||
time.sleep(5)
|
||||
self.queue(action[0], *action[1])
|
||||
else:
|
||||
logger.debug('unknown action %s', action)
|
||||
|
||||
|
|
@ -68,6 +74,14 @@ class Node(Thread):
|
|||
if state.online or self.get_local():
|
||||
self._q.put('ping')
|
||||
|
||||
def queue(self, action, *args):
|
||||
logger.debug('queue node action %s->%s%s', self.user_id, action, args)
|
||||
self._q.put([action, args])
|
||||
|
||||
def _call(self, action, *args):
|
||||
r = getattr(self, action)(*args)
|
||||
logger.debug('call node api %s->%s%s = %s', self.user_id, action, args, r)
|
||||
|
||||
@property
|
||||
def url(self):
|
||||
if self.local:
|
||||
|
|
@ -321,6 +335,7 @@ class Node(Thread):
|
|||
return False
|
||||
|
||||
def peering(self, action):
|
||||
pull_changes = False
|
||||
with db.session():
|
||||
u = user.models.User.get_or_create(self.user_id)
|
||||
user_info = u.info
|
||||
|
|
@ -335,6 +350,8 @@ class Node(Thread):
|
|||
if 'message' in u.info:
|
||||
del u.info['message']
|
||||
u.save()
|
||||
if action == 'acceptPeering':
|
||||
pull_changes = True
|
||||
else:
|
||||
logger.debug('peering failed? %s %s', action, r)
|
||||
if action in ('cancelPeering', 'rejectPeering', 'removePeering'):
|
||||
|
|
@ -342,6 +359,8 @@ class Node(Thread):
|
|||
with db.session():
|
||||
u = user.models.User.get(self.user_id)
|
||||
trigger_event('peering.%s' % action.replace('Peering', ''), u.json())
|
||||
if pull_changes:
|
||||
self.pullChanges()
|
||||
return True
|
||||
|
||||
headers = {
|
||||
|
|
@ -514,9 +533,15 @@ class Nodes(Thread):
|
|||
|
||||
def queue(self, *args):
|
||||
if args:
|
||||
logger.debug('queue "%s", %s entries in queue', args[0], self._q.qsize())
|
||||
logger.debug('queue "%s", %s entries in queue', args, self._q.qsize())
|
||||
self._q.put(list(args))
|
||||
|
||||
def peer_queue(self, peer, action, *args):
|
||||
if peer not in self._nodes:
|
||||
self._add(peer)
|
||||
self._nodes[peer].queue(action, *args)
|
||||
|
||||
|
||||
def is_online(self, id):
|
||||
return id in self._nodes and self._nodes[id].is_online()
|
||||
|
||||
|
|
@ -536,18 +561,18 @@ class Nodes(Thread):
|
|||
elif target == 'online':
|
||||
nodes = [n for n in list(self._nodes.values()) if n.online]
|
||||
else:
|
||||
if not target in self._nodes:
|
||||
if target not in self._nodes:
|
||||
self._add(target)
|
||||
nodes = [self._nodes[target]]
|
||||
for node in nodes:
|
||||
r = getattr(node, action)(*args)
|
||||
logger.debug('call node api %s->%s%s = %s', node.user_id, action, args, r)
|
||||
node._call(action, *args)
|
||||
|
||||
def _add(self, user_id, send_response=False):
|
||||
if user_id not in self._nodes:
|
||||
from user.models import User
|
||||
with db.session():
|
||||
self._nodes[user_id] = Node(self, User.get_or_create(user_id))
|
||||
User.get_or_create(user_id)
|
||||
self._nodes[user_id] = Node(self, user_id)
|
||||
else:
|
||||
self._nodes[user_id].ping()
|
||||
if send_response:
|
||||
|
|
@ -608,8 +633,11 @@ def update_online():
|
|||
|
||||
def check_nodes():
|
||||
if state.online:
|
||||
ids = []
|
||||
with db.session():
|
||||
for u in user.models.User.query.filter_by(queued=True):
|
||||
if not state.nodes.is_online(u.id):
|
||||
logger.debug('queued peering message for %s trying to connect...', u.id)
|
||||
state.nodes.queue('add', u.id, True)
|
||||
ids.append(u.id)
|
||||
for id in ids:
|
||||
if not state.nodes.is_online(id):
|
||||
logger.debug('queued peering message for %s trying to connect...', id)
|
||||
state.nodes.queue('add', id, True)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue