From 9207f3bce432116353eb598bfd226b955893414d Mon Sep 17 00:00:00 2001 From: David Snopek Date: Thu, 18 Jul 2013 02:17:36 +0100 Subject: [PATCH] * Simplified logging helper. * Fixed think-o in how to close the CollectionWrapper inside a ThreadingCollectionWrapper. --- AnkiServer/apps/rest_app.py | 4 +--- AnkiServer/threading.py | 23 ++++++++++++++++++----- AnkiServer/utils.py | 19 +++++++++++-------- 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/AnkiServer/apps/rest_app.py b/AnkiServer/apps/rest_app.py index f1322a6..4faf092 100644 --- a/AnkiServer/apps/rest_app.py +++ b/AnkiServer/apps/rest_app.py @@ -478,9 +478,7 @@ class CardHandler(RestHandlerBase): def make_app(global_conf, **local_conf): # setup the logger from AnkiServer.utils import setup_logging - logging_config_file = local_conf.get('logging.config_file') - if logging_config_file: - setup_logging(logging_config_file) + setup_logging(local_conf.get('logging.config_file')) return RestApp( data_root=local_conf.get('data_root', '.'), diff --git a/AnkiServer/threading.py b/AnkiServer/threading.py index b820700..c5fe199 100644 --- a/AnkiServer/threading.py +++ b/AnkiServer/threading.py @@ -114,11 +114,21 @@ class ThreadingCollectionWrapper(object): if self._thread is not None: self._thread.join() + # # Mimic the CollectionWrapper interface + # + def open(self): + """Non-op. The collection will be opened on demand.""" pass + def close(self): - self.stop() + """Closes the underlying collection without stopping the thread.""" + + def _close(col): + self.wrapper.close() + self.execute(_close, waitForReturn=False) + def opened(self): return self.wrapper.opened() @@ -152,15 +162,18 @@ class ThreadingCollectionManager(CollectionManager): for path, thread in self.collections.items(): if thread.running and thread.wrapper.opened() and thread.qempty() and cur - thread.last_timestamp >= self.monitor_inactivity: logging.info('Monitor is closing collection on inactive CollectionThread[%s]', thread.path) - def closeCollection(wrapper): - wrapper.close() - thread.execute(closeCollection, waitForReturn=False) + thread.close() time.sleep(self.monitor_frequency) def shutdown(self): # TODO: stop the monitor thread! - # This will stop all the collection threads + # stop all the threads + for path, col in self.collections.items(): + del self.collections[path] + col.stop() + + # let the parent do whatever else it might want to do... super(ThreadingCollectionManager, self).shutdown() # diff --git a/AnkiServer/utils.py b/AnkiServer/utils.py index bd83664..844ef14 100644 --- a/AnkiServer/utils.py +++ b/AnkiServer/utils.py @@ -1,15 +1,18 @@ -def setup_logging(config_file): +def setup_logging(config_file=None): """Setup logging based on a config_file.""" import logging - # monkey patch the logging.config.SMTPHandler if necessary - import sys - if sys.version_info[0] == 2 and sys.version_info[1] == 5: - import AnkiServer.logpatch + if config_file is not None: + # monkey patch the logging.config.SMTPHandler if necessary + import sys + if sys.version_info[0] == 2 and sys.version_info[1] == 5: + import AnkiServer.logpatch - # load the config file - import logging.config - logging.config.fileConfig(config_file) + # load the config file + import logging.config + logging.config.fileConfig(config_file) + else: + logging.getLogger().setLevel(logging.INFO)