-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflask_kazoo.py
45 lines (37 loc) · 1.2 KB
/
flask_kazoo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# coding=utf-8
import logging
from kazoo.client import KazooClient
from flask import current_app
try:
from flask import _app_ctx_stack as stack
except ImportError:
from flask import _request_ctx_stack as stack
__author__ = 'comyn'
class Kazoo(object):
def __init__(self, app=None):
self.app = app
if app is not None:
self.init_app(app)
def init_app(self, app):
logger = logging.getLogger('kazoo.client')
logger.parent = app.logger
app.config.setdefault('ZOOKEEPER_HOSTS', 'localhost:2181')
if hasattr(app, "teardown_appcontext"):
app.teardown_appcontext(self.teardown)
else:
app.teardown_request(self.teardown)
def teardown(self, exception):
ctx = stack.top
if hasattr(ctx, 'kazoo'):
ctx.kazoo.stop()
ctx.kazoo.close()
def connect(self):
kz = KazooClient(hosts=current_app.config.get('ZOOKEEPER_HOSTS'))
kz.start()
return kz
def __getattr__(self, item):
ctx = stack.top
if ctx is not None:
if not hasattr(ctx, 'kazoo'):
ctx.kazoo = self.connect()
return getattr(ctx.kazoo, item)