Skip to content

Instantly share code, notes, and snippets.

@Fluxx
Created January 1, 2013 00:41
Show Gist options
  • Select an option

  • Save Fluxx/4424240 to your computer and use it in GitHub Desktop.

Select an option

Save Fluxx/4424240 to your computer and use it in GitHub Desktop.
diff --git a/disqus/application.py b/disqus/application.py
index 6674d30..297dc7f 100644
--- a/disqus/application.py
+++ b/disqus/application.py
@@ -8,7 +8,6 @@ If you're writing a new script, ensure it gets fired either via
manage.py, runs via a wsgi file which inherits wsgi/default.py,
or imports cli if its a script (in scripts/).
"""
-import functools
import os
import sys
@@ -25,30 +24,21 @@ if path not in sys.path:
if 'DJANGO_SETTINGS_MODULE' not in os.environ:
os.environ['DJANGO_SETTINGS_MODULE'] = 'disqus.settings'
-USE_EVENTLET = os.environ.get('EVENTLET_NOPATCH')
+settings.USE_EVENTLET = os.environ.get('EVENTLET_NOPATCH')
if os.environ.get('USE_ZOOKEEPER_SETTINGS'):
print >> sys.stderr, '* Patching Django settings to use ZooKeeper...'
import menagerie
- from kazoo.client import KazooClient
- from kzeventlet.handler import SequentialEventletHandler
-
- options = {}
-
- if USE_EVENTLET:
- print >> sys.stderr, '* Using Eventlet handler for ZooKeeper settings...'
- options['client'] = functools.partial(KazooClient,
- handler=SequentialEventletHandler())
-
- menagerie.configure(**options)
+ from disqus.common.zookeeper import Client
+ menagerie.configure(client=Client)
# HACK: Django requires the package root to have .__file__
print >> sys.stderr, "* Hacking disqus.__file__"
import disqus
disqus.__file__ = os.path.join(os.path.dirname(__file__), '__init__.py')
-if USE_EVENTLET:
+if settings.USE_EVENTLET:
print >> sys.stderr, "* Installing eventlet patcher"
import eventlet
import eventlet.debug
diff --git a/disqus/common/events.py b/disqus/common/events.py
index d23fe7f..1112f62 100644
--- a/disqus/common/events.py
+++ b/disqus/common/events.py
@@ -13,10 +13,9 @@ def initialize(func):
# only-once register the kafka logger with da
if not analytics.KafkaLogger.initialized:
increment("jones.decorator.init-zk")
- samsa_zk = kafka_client()
- samsa_zk.start()
+ kafka_client.start()
- samsa_client = Cluster(samsa_zk)
+ samsa_client = Cluster(kafka_client)
jones_client = get_connection()
analytics.KafkaLogger.init(jones_client, samsa_client)
diff --git a/disqus/common/zookeeper.py b/disqus/common/zookeeper.py
index d7c7f82..6734c20 100644
--- a/disqus/common/zookeeper.py
+++ b/disqus/common/zookeeper.py
@@ -1,25 +1,24 @@
from django.conf import settings
-from django.utils.functional import memoize
-hosts_string = ','.join(settings.ZOOKEEPER_HOSTS)
-kafka_hosts_string = '%s/kafka' % hosts_string
+import functools
+from kazoo.client import KazooClient
-def __client_builder_for(hosts):
- # KazooClient must be imported in this lazy fashion due to an
- # incompatability when running under eventlet.monkey_patch(). When
- # imported in a monkey patched environment, an exception is raised.
- # Thus, that import is made lazy, and put here.
- #
- # At present, only certain celery tasks have monkey patching turned on,
- # and those tasks do not use jones, so this code will never be executed
- # in that situation.
- def builder():
- from kazoo.client import KazooClient
- return KazooClient(hosts)
+from kzeventlet.handler import SequentialEventletHandler
- return builder
+HOSTS_STRING = ','.join(settings.ZOOKEEPER_HOSTS)
+KAFKA_HOSTS_STRING = '%s/kafka' % HOSTS_STRING
-client = memoize(__client_builder_for(hosts_string), {}, 0)
-kafka_client = memoize(__client_builder_for(kafka_hosts_string), {}, 0)
+
+if settings.USE_EVENTLET:
+ KazooClient = functools.partial(
+ KazooClient,
+ handler=SequentialEventletHandler()
+ )
+
+Client = KazooClient
+
+
+client = Client(HOSTS_STRING)
+kafka_client = Client(KAFKA_HOSTS_STRING)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment