Handle exiting more gracefully

Set all threads to daemon mode and close
the write end of the pipe when exiting to
ensure we cleanup in a friendly manner.

Change-Id: I4cf64fa39e820c95d11943b848db87e5db15212e
This commit is contained in:
Joshua Harlow 2014-04-30 20:36:47 -07:00
parent 7898f63698
commit 5500eca644
2 changed files with 14 additions and 2 deletions

View File

@ -13,6 +13,7 @@
# under the License. # under the License.
import argparse import argparse
import contextlib
import logging import logging
import os import os
import sys import sys
@ -121,15 +122,21 @@ class App(object):
self.status.update(title=screen.title) self.status.update(title=screen.title)
self.loop = urwid.MainLoop(screen, palette=palette, self.loop = urwid.MainLoop(screen, palette=palette,
unhandled_input=self.unhandledInput) unhandled_input=self.unhandledInput)
sync_pipe = self.loop.watch_pipe(self.refresh) self.sync_pipe = self.loop.watch_pipe(self.refresh)
#self.loop.screen.set_terminal_properties(colors=88) #self.loop.screen.set_terminal_properties(colors=88)
if not disable_sync: if not disable_sync:
self.sync_thread = threading.Thread(target=self.sync.run, args=(sync_pipe,)) self.sync_thread = threading.Thread(target=self.sync.run, args=(self.sync_pipe,))
self.sync_thread.daemon = True
self.sync_thread.start() self.sync_thread.start()
else: else:
self.sync_thread = None self.sync_thread = None
def run(self):
self.loop.run() self.loop.run()
def close(self):
os.close(self.sync_pipe)
def changeScreen(self, widget): def changeScreen(self, widget):
self.status.update(title=widget.title) self.status.update(title=widget.title)
self.screens.append(self.loop.widget) self.screens.append(self.loop.widget)
@ -173,6 +180,8 @@ class App(object):
self.backScreen() self.backScreen()
elif key == 'f1': elif key == 'f1':
self.help() self.help()
elif key in ('q', 'Q'):
raise urwid.ExitMainLoop()
def getRepo(self, project_name): def getRepo(self, project_name):
local_path = os.path.join(self.config.git_root, project_name) local_path = os.path.join(self.config.git_root, project_name)
@ -192,3 +201,5 @@ if __name__ == '__main__':
help='the server to use (as specified in config file)') help='the server to use (as specified in config file)')
args = parser.parse_args() args = parser.parse_args()
g = App(args.server, args.debug, args.no_sync) g = App(args.server, args.debug, args.no_sync)
with contextlib.closing(g):
g.run()

View File

@ -377,6 +377,7 @@ class Sync(object):
self.submitTask(SyncSubscribedProjectsTask(HIGH_PRIORITY)) self.submitTask(SyncSubscribedProjectsTask(HIGH_PRIORITY))
self.submitTask(UploadReviewsTask(HIGH_PRIORITY)) self.submitTask(UploadReviewsTask(HIGH_PRIORITY))
self.periodic_thread = threading.Thread(target=self.periodicSync) self.periodic_thread = threading.Thread(target=self.periodicSync)
self.periodic_thread.daemon = True
self.periodic_thread.start() self.periodic_thread.start()
def periodicSync(self): def periodicSync(self):