3131 from queue import Queue
3232
3333from celery import Celery
34- from celery .contrib .methods import task
3534import redis
3635import requests
3736from contracts import contract , new_contract
5756 import snowplow_celery_config
5857 app = Celery ()
5958 app .config_from_object (snowplow_celery_config )
60-
6159except ImportError :
6260 # Otherwise configure Celery with default settings
61+ snowplow_celery_config = None
6362 app = Celery ("Snowplow" , broker = "redis://guest@localhost//" )
6463
6564
@@ -172,7 +171,6 @@ def reached_limit(self):
172171 else :
173172 return self .bytes_queued >= self .byte_limit or len (self .buffer ) >= self .buffer_size
174173
175- @task (name = "Flush" )
176174 def flush (self ):
177175 """
178176 Sends all events in the buffer to the collector.
@@ -387,6 +385,14 @@ def consume(self):
387385 self .queue .task_done ()
388386
389387
388+ @app .task (bind = True , name = 'tasks.flush' ) # the self passed with bind can be used for on_fail/retrying
389+ def flush_emitter (self , emitter ):
390+ try :
391+ emitter .flush ()
392+ finally :
393+ logger .info ("Flush called on emitter" )
394+
395+
390396class CeleryEmitter (Emitter ):
391397 """
392398 Uses a Celery worker to send HTTP requests asynchronously.
@@ -400,7 +406,7 @@ def flush(self):
400406 """
401407 Schedules a flush task
402408 """
403- super ( CeleryEmitter , self ). flush . delay ()
409+ flush_emitter . delay ( self ) # passes emitter (self - CeleryEmitter) to task
404410 logger .info ("Scheduled a Celery task to flush the event queue" )
405411
406412
0 commit comments