mirror of
https://github.com/sprockets/sprockets-status.git
synced 2024-11-24 11:19:53 +00:00
Cache application name & version on the class.
This prevents each status call from doing a pkg_resources resolution which might not be as cheap as it appears to be.
This commit is contained in:
parent
33d8c5f743
commit
3a57fede12
2 changed files with 63 additions and 31 deletions
|
@ -1,7 +1,7 @@
|
||||||
import json
|
import json
|
||||||
import pkg_resources
|
import pkg_resources
|
||||||
|
|
||||||
from tornado import concurrent, gen, web
|
from tornado import web
|
||||||
|
|
||||||
|
|
||||||
class StatusHandler(web.RequestHandler):
|
class StatusHandler(web.RequestHandler):
|
||||||
|
@ -25,12 +25,44 @@ class StatusHandler(web.RequestHandler):
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
OK = 'ok'
|
||||||
self.name = 'UNKNOWN'
|
FAILURE = 'failed'
|
||||||
self.version = '0.0.0'
|
|
||||||
self.status = 'ok'
|
_package_name = None
|
||||||
self._package_name = None
|
_application_name = 'UNKNOWN'
|
||||||
super(StatusHandler, self).__init__(*args, **kwargs)
|
_application_version = None
|
||||||
|
|
||||||
|
@property
|
||||||
|
def application_name(self):
|
||||||
|
"""The application's reported name."""
|
||||||
|
self._lookup_package_info()
|
||||||
|
return self._application_name
|
||||||
|
|
||||||
|
@property
|
||||||
|
def application_version(self):
|
||||||
|
"""The application's version number."""
|
||||||
|
self._lookup_package_info()
|
||||||
|
return self._application_version
|
||||||
|
|
||||||
|
@property
|
||||||
|
def application_status(self):
|
||||||
|
"""The application's current status."""
|
||||||
|
self._lookup_package_info()
|
||||||
|
if self.application_name is None:
|
||||||
|
return self.FAILURE
|
||||||
|
return self.OK
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _lookup_package_info(cls):
|
||||||
|
if cls._package_name is not None:
|
||||||
|
try:
|
||||||
|
pkg_info = pkg_resources.get_distribution(cls._package_name)
|
||||||
|
cls._application_name = pkg_info.project_name
|
||||||
|
cls._application_version = pkg_info.version
|
||||||
|
cls._package_name = None
|
||||||
|
except pkg_resources.ResolutionError:
|
||||||
|
cls._application_name = None
|
||||||
|
cls._application_version = None
|
||||||
|
|
||||||
def initialize(self, **kwargs):
|
def initialize(self, **kwargs):
|
||||||
"""
|
"""
|
||||||
|
@ -47,28 +79,12 @@ class StatusHandler(web.RequestHandler):
|
||||||
keywords are used.
|
keywords are used.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
cls = self.__class__
|
||||||
if 'package' in kwargs:
|
if 'package' in kwargs:
|
||||||
self._package_name = kwargs['package']
|
cls._package_name = kwargs['package']
|
||||||
else:
|
else:
|
||||||
self.name = kwargs.get('name') or self.name
|
cls._application_name = kwargs['name']
|
||||||
self.version = kwargs.get('version') or self.version
|
cls._application_version = kwargs['version']
|
||||||
|
|
||||||
@gen.coroutine
|
|
||||||
def prepare(self):
|
|
||||||
maybe_future = super(StatusHandler, self).prepare()
|
|
||||||
if concurrent.is_future(maybe_future):
|
|
||||||
yield maybe_future
|
|
||||||
|
|
||||||
if not self._finished:
|
|
||||||
if self._package_name is not None:
|
|
||||||
try:
|
|
||||||
pkg_info = pkg_resources.get_distribution(
|
|
||||||
self._package_name)
|
|
||||||
self.name = pkg_info.project_name
|
|
||||||
self.version = pkg_info.version
|
|
||||||
except pkg_resources.ResolutionError:
|
|
||||||
self.set_status(500)
|
|
||||||
self.finish()
|
|
||||||
|
|
||||||
def get(self):
|
def get(self):
|
||||||
"""
|
"""
|
||||||
|
@ -85,9 +101,15 @@ class StatusHandler(web.RequestHandler):
|
||||||
}
|
}
|
||||||
|
|
||||||
"""
|
"""
|
||||||
self.set_status(200)
|
if self.application_status == self.FAILURE:
|
||||||
|
self.set_status(500)
|
||||||
|
else:
|
||||||
|
self.set_status(200)
|
||||||
|
|
||||||
self.set_header('Content-Type', 'application/json')
|
self.set_header('Content-Type', 'application/json')
|
||||||
self.write(json.dumps({'name': self.name,
|
self.write(json.dumps({
|
||||||
'version': self.version,
|
'name': self.application_name,
|
||||||
'status': self.status}).encode('utf-8'))
|
'version': self.application_version,
|
||||||
|
'status': self.application_status
|
||||||
|
}).encode('utf-8'))
|
||||||
self.finish()
|
self.finish()
|
||||||
|
|
10
tests.py
10
tests.py
|
@ -9,6 +9,11 @@ import sprockets_status.handlers
|
||||||
|
|
||||||
class SimpleStatusTests(testing.AsyncHTTPTestCase):
|
class SimpleStatusTests(testing.AsyncHTTPTestCase):
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def setUpClass(cls):
|
||||||
|
super(SimpleStatusTests, cls).setUpClass()
|
||||||
|
sprockets_status.handlers.StatusHandler._package_name = None
|
||||||
|
|
||||||
def get_app(self):
|
def get_app(self):
|
||||||
return examples.app.make_app()
|
return examples.app.make_app()
|
||||||
|
|
||||||
|
@ -39,6 +44,11 @@ class SimpleStatusTests(testing.AsyncHTTPTestCase):
|
||||||
|
|
||||||
class PackageLookupTests(testing.AsyncHTTPTestCase):
|
class PackageLookupTests(testing.AsyncHTTPTestCase):
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def setUpClass(cls):
|
||||||
|
super(PackageLookupTests, cls).setUpClass()
|
||||||
|
sprockets_status.handlers.StatusHandler._package_name = None
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.application = None
|
self.application = None
|
||||||
super(PackageLookupTests, self).setUp()
|
super(PackageLookupTests, self).setUp()
|
||||||
|
|
Loading…
Reference in a new issue