Permalink
Browse files

Handle bad modules

* Keep the server running
* Log the import exception
* Do somewhat graceful handling of modules that become bad on reload
  • Loading branch information...
1 parent a5842a6 commit 52c1df60994af4297ceb0cfd4f4266ecc8aeedce @parente committed Feb 3, 2011
Showing with 27 additions and 4 deletions.
  1. +27 −4 handlerbag.py
View
31 handlerbag.py
@@ -13,6 +13,7 @@
import shelve
import sys
import re
+import logging
# handlerbag
import hbag
import login
@@ -105,6 +106,12 @@ def refresh_handlers_in_db(self):
for name in avail:
info = self.db.get(name, {})
mod = self.load_module(name)
+ if not mod:
+ try:
+ del self.db[name]
+ except KeyError:
+ pass
+ continue
opts = mod.get_default_options(self)
try:
desc = mod.__doc__.split('\n')[0]
@@ -133,11 +140,21 @@ def load_module(self, name):
mod = self.modules[name]
except KeyError:
# load the module for the first time
- mod = __import__('hbag.'+name, fromlist=[name])
+ try:
+ mod = __import__('hbag.'+name, fromlist=[name])
+ except Exception:
+ # bad module, log and bail
+ logging.exception('failed to load %s', name)
+ return
self.modules[name] = mod
else:
# reload the module if loaded
- mod = reload(mod)
+ try:
+ mod = reload(mod)
+ except Exception:
+ # bad module, log and bail
+ logging.exception('failed to load %s', name)
+ return
self.modules[name] = mod
return mod
@@ -172,8 +189,14 @@ def set_handler_status(self, name, enable, opts=None):
opts = info['options']
# register new handler
mod = self.load_module(name)
- handlers = mod.get_handler_map(self, options.webroot, opts)
- self.add_dynamic_handlers('.*$', handlers, opts)
+ if mod:
+ handlers = mod.get_handler_map(self, options.webroot, opts)
+ self.add_dynamic_handlers('.*$', handlers, opts)
+ else:
+ try:
+ del self.db[name]
+ except KeyError:
+ pass
if __name__ == '__main__':
define('webroot', default='/', help='absolute root url of all handlers (default: /)')

0 comments on commit 52c1df6

Please sign in to comment.