Skip to content

Instantly share code, notes, and snippets.

@rochacon
Created April 5, 2014 03:04
Show Gist options
  • Save rochacon/9986953 to your computer and use it in GitHub Desktop.
Save rochacon/9986953 to your computer and use it in GitHub Desktop.
Simple script to consume every "celery.backend_cleanup" task from a queue
# -*- coding: utf-8 -*-
"""
This script cleans up celery.backend_cleanup task from a queue.
Different tasks will be printed and returned to the queue
"""
import kombu
AMQP_ENDPOINT = 'amqp://guest:guest@localhost:5672/vhost'
AMQP_QUEUE = 'celery'
def consume_messages():
conn = kombu.Connection(AMQP_ENDPOINT)
q = kombu.Queue(AMQP_QUEUE, channel=conn.channel())
tasks = list()
while True:
m = q.get(no_ack=False)
if m is None:
break
d = m.decode()
# ack celery.backend_cleanup tasks (removing it from the queue)
if d['task'] == 'celery.backend_cleanup':
m.ack()
else:
tasks.append(d)
conn.close()
return tasks
if __name__ == '__main__':
tasks = consume_messages()
for t in tasks:
print(t)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment