Skip to content

Commit

Permalink
Except fixes (#8)
Browse files Browse the repository at this point in the history
* nginx reloader: dont print stdout

* app: watch for Future exceptions and stop loop on error

* service model: catch ConsulException

* test fix

* test fiiiix
  • Loading branch information
holycheater authored and ramm committed Dec 16, 2016
1 parent a5a4837 commit 33c3ca9
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 8 deletions.
14 changes: 11 additions & 3 deletions src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,25 @@ def sig_handler(sig, frame):
logger.warning('Caught signal: %s', sig)
tornado.ioloop.IOLoop.instance().add_callback(shutdown)

def handle_future(f):
tornado.ioloop.IOLoop.current().stop()
if f.exception() != None:
raise f.exception()

def main():
signal.signal(signal.SIGTERM, sig_handler)
signal.signal(signal.SIGINT, sig_handler)

vergilius.Vergilius.init()

consul_handler = ServiceWatcher()
nginx_reloader = NginxReloader()
consul_handler = ServiceWatcher().watch_services()
nginx_reloader = NginxReloader().nginx_reload()

io_loop = tornado.ioloop.IOLoop.current()
io_loop.add_future(consul_handler, handle_future)
io_loop.add_future(nginx_reloader, handle_future)

tornado.ioloop.IOLoop.current().start()
io_loop.start()


if __name__ == '__main__':
Expand Down
4 changes: 2 additions & 2 deletions src/vergilius/loop/nginx_reloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class NginxReloader(object):
nginx_update_event = Event()

def __init__(self):
self.nginx_reload()
pass

@classmethod
@tornado.gen.coroutine
Expand All @@ -18,7 +18,7 @@ def nginx_reload(cls):
yield cls.nginx_update_event.wait()
cls.nginx_update_event.clear()
vergilius.logger.info('[nginx]: reload')
subprocess.check_call([vergilius.config.NGINX_BINARY, '-s', 'reload'])
subprocess.check_call([vergilius.config.NGINX_BINARY, '-s', 'reload'], stdout=subprocess.DEVNULL)

@classmethod
def queue_reload(cls):
Expand Down
1 change: 0 additions & 1 deletion src/vergilius/loop/service_watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ def __init__(self):

self.data = {}
self.modified = False
self.watch_services()

@tornado.gen.coroutine
def watch_services(self):
Expand Down
4 changes: 3 additions & 1 deletion src/vergilius/models/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import tempfile
import unicodedata

from consul import tornado, base
from consul import tornado, base, ConsulException
from vergilius import config, consul_tornado, consul, logger, template_loader
from vergilius.loop.nginx_reloader import NginxReloader
from vergilius.models.certificate import Certificate
Expand Down Expand Up @@ -45,6 +45,8 @@ def watch(self):
try:
index, data = yield consul_tornado.health.service(self.name, index, wait=None, passing=True)
self.parse_data(data)
except ConsulException as e:
logger.error('consul exception: %s' % e)
except base.Timeout:
pass

Expand Down
4 changes: 3 additions & 1 deletion tests/base_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ class BaseTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
super(BaseTest, cls).setUpClass()
cls.watcher = ServiceWatcher().watch_services()
cls.watcher = ServiceWatcher()
cls.watcher.watch_services()

threading.Thread(target=start_tornado).start()

@classmethod
Expand Down

0 comments on commit 33c3ca9

Please sign in to comment.