use one variable to track app shutdown state

This commit is contained in:
j 2016-01-31 22:15:14 +05:30
commit 567952d91d
7 changed files with 35 additions and 44 deletions

View file

@ -31,7 +31,6 @@ logger = logging.getLogger(__name__)
ENCODING='base64'
class Node(Thread):
_running = True
host = None
local = None
_online = None
@ -46,13 +45,13 @@ class Node(Thread):
self._q = Queue()
Thread.__init__(self)
self.daemon = True
self.start()
self.ping()
self.start()
def run(self):
while self._running:
while not state.shutdown:
action = self._q.get()
if not self._running:
if state.shutdown:
break
if action == 'send_response':
self._send_response()
@ -62,11 +61,9 @@ class Node(Thread):
logger.debug('unknown action %s', action)
def join(self):
self._running = False
self._q.put('')
#return Thread.join(self)
def ping(self):
if state.online:
self._q.put('ping')
@ -155,7 +152,6 @@ class Node(Thread):
except urllib.error.HTTPError as e:
if e.code == 403:
logger.debug('403: %s (%s)', url, self.user_id)
self._running = False
if state.tasks:
state.tasks.queue('peering', (self.user_id, False))
del self._nodes[self.user_id]
@ -335,7 +331,7 @@ class Node(Thread):
'id': item.id, 'progress': t.progress
})
if state.bandwidth:
while not state.bandwidth.download(chunk_size) and self._running:
while not state.bandwidth.download(chunk_size) and not state.shutdown:
time.sleep(0.1)
t2 = datetime.utcnow()
duration = (t2-t1).total_seconds()
@ -407,7 +403,6 @@ class Nodes(Thread):
def __init__(self):
self._q = Queue()
self._running = True
with db.session():
for u in user.models.User.query.filter_by(peered=True):
if 'local' in u.info:
@ -427,7 +422,7 @@ class Nodes(Thread):
self.start()
def cleanup(self):
if self._running and self._local:
if not state.shutdown and self._local:
self._local.cleanup()
def pull(self):
@ -479,14 +474,14 @@ class Nodes(Thread):
return
self._pulling = True
for node in list(self._nodes.values()):
if self._running:
if not state.shutdown:
node.online = node.can_connect()
if self._running and node.online:
if not state.shutdown and node.online:
node.pullChanges()
self._pulling = False
def run(self):
while self._running:
while not state.shutdown:
args = self._q.get()
if args:
if args[0] == 'cleanup':
@ -499,7 +494,6 @@ class Nodes(Thread):
self._call(*args)
def join(self):
self._running = False
self._q.put(None)
for node in list(self._nodes.values()):
node.join()