Skip to content
This repository was archived by the owner on Jul 13, 2020. It is now read-only.

Commit f520db7

Browse files
Merge pull request #1 from rinse-inc/dont-initialize-celery-in-module
Initializes Celery app in CeleryEmitter.
2 parents 91da00a + 0d87c2f commit f520db7

1 file changed

Lines changed: 17 additions & 12 deletions

File tree

snowplow_tracker/emitters.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,6 @@
5252

5353
new_contract("redis", lambda x: isinstance(x, (redis.Redis, redis.StrictRedis)))
5454

55-
try:
56-
# Check whether a custom Celery configuration module named "snowplow_celery_config" exists
57-
import snowplow_celery_config
58-
app = Celery()
59-
app.config_from_object(snowplow_celery_config)
60-
61-
except ImportError:
62-
# Otherwise configure Celery with default settings
63-
app = Celery("Snowplow", broker="redis://guest@localhost//")
64-
6555

6656
class Emitter(object):
6757
"""
@@ -172,7 +162,6 @@ def reached_limit(self):
172162
else:
173163
return self.bytes_queued >= self.byte_limit or len(self.buffer) >= self.buffer_size
174164

175-
@task(name="Flush")
176165
def flush(self):
177166
"""
178167
Sends all events in the buffer to the collector.
@@ -393,16 +382,32 @@ class CeleryEmitter(Emitter):
393382
Works like the base Emitter class,
394383
but on_success and on_failure callbacks cannot be set.
395384
"""
385+
celery_app = None
386+
396387
def __init__(self, endpoint, protocol="http", port=None, method="get", buffer_size=None, byte_limit=None):
397388
super(CeleryEmitter, self).__init__(endpoint, protocol, port, method, buffer_size, None, None, byte_limit)
398389

390+
try:
391+
# Check whether a custom Celery configuration module named "snowplow_celery_config" exists
392+
import snowplow_celery_config
393+
self.celery_app = Celery()
394+
self.celery_app.config_from_object(snowplow_celery_config)
395+
except ImportError:
396+
# Otherwise configure Celery with default settings
397+
self.celery_app = Celery("Snowplow", broker="redis://guest@localhost//")
398+
399+
self.async_flush = self.celery_app.task(self.async_flush)
400+
399401
def flush(self):
400402
"""
401403
Schedules a flush task
402404
"""
403-
super(CeleryEmitter, self).flush.delay()
405+
self.async_flush.delay()
404406
logger.info("Scheduled a Celery task to flush the event queue")
405407

408+
def async_flush(self):
409+
super(CeleryEmitter, self).flush()
410+
406411

407412
class RedisEmitter(object):
408413
"""

0 commit comments

Comments
 (0)