* Simplified logging helper.

* Fixed think-o in how to close the CollectionWrapper inside a ThreadingCollectionWrapper.
This commit is contained in:
David Snopek 2013-07-18 02:17:36 +01:00
parent f891a939c3
commit 9207f3bce4
3 changed files with 30 additions and 16 deletions

View File

@ -478,9 +478,7 @@ class CardHandler(RestHandlerBase):
def make_app(global_conf, **local_conf): def make_app(global_conf, **local_conf):
# setup the logger # setup the logger
from AnkiServer.utils import setup_logging from AnkiServer.utils import setup_logging
logging_config_file = local_conf.get('logging.config_file') setup_logging(local_conf.get('logging.config_file'))
if logging_config_file:
setup_logging(logging_config_file)
return RestApp( return RestApp(
data_root=local_conf.get('data_root', '.'), data_root=local_conf.get('data_root', '.'),

View File

@ -114,11 +114,21 @@ class ThreadingCollectionWrapper(object):
if self._thread is not None: if self._thread is not None:
self._thread.join() self._thread.join()
#
# Mimic the CollectionWrapper interface # Mimic the CollectionWrapper interface
#
def open(self): def open(self):
"""Non-op. The collection will be opened on demand."""
pass pass
def close(self): 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): def opened(self):
return self.wrapper.opened() return self.wrapper.opened()
@ -152,15 +162,18 @@ class ThreadingCollectionManager(CollectionManager):
for path, thread in self.collections.items(): 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: 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) logging.info('Monitor is closing collection on inactive CollectionThread[%s]', thread.path)
def closeCollection(wrapper): thread.close()
wrapper.close()
thread.execute(closeCollection, waitForReturn=False)
time.sleep(self.monitor_frequency) time.sleep(self.monitor_frequency)
def shutdown(self): def shutdown(self):
# TODO: stop the monitor thread! # 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() super(ThreadingCollectionManager, self).shutdown()
# #

View File

@ -1,9 +1,10 @@
def setup_logging(config_file): def setup_logging(config_file=None):
"""Setup logging based on a config_file.""" """Setup logging based on a config_file."""
import logging import logging
if config_file is not None:
# monkey patch the logging.config.SMTPHandler if necessary # monkey patch the logging.config.SMTPHandler if necessary
import sys import sys
if sys.version_info[0] == 2 and sys.version_info[1] == 5: if sys.version_info[0] == 2 and sys.version_info[1] == 5:
@ -12,4 +13,6 @@ def setup_logging(config_file):
# load the config file # load the config file
import logging.config import logging.config
logging.config.fileConfig(config_file) logging.config.fileConfig(config_file)
else:
logging.getLogger().setLevel(logging.INFO)