root / snf-cyclades-app / synnefo / logic / management / commands / queue-retry.py @ b6426ead
History | View | Annotate | Download (4.1 kB)
1 |
# Copyright 2011-2012 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or without
|
4 |
# modification, are permitted provided that the following conditions
|
5 |
# are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above copyright
|
8 |
# notice, this list of conditions and the following disclaimer.
|
9 |
#
|
10 |
# 2. Redistributions in binary form must reproduce the above copyright
|
11 |
# notice, this list of conditions and the following disclaimer in the
|
12 |
# documentation and/or other materials provided with the distribution.
|
13 |
#
|
14 |
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
15 |
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
16 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
17 |
# ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
18 |
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
19 |
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
20 |
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
21 |
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
22 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
23 |
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
24 |
# SUCH DAMAGE.
|
25 |
#
|
26 |
# The views and conclusions contained in the software and documentation are
|
27 |
# those of the authors and should not be interpreted as representing official
|
28 |
# policies, either expressed or implied, of GRNET S.A.
|
29 |
#
|
30 |
from optparse import make_option |
31 |
|
32 |
from synnefo.lib.amqp import AMQPClient |
33 |
from snf_django.management.commands import SynnefoCommand |
34 |
|
35 |
from synnefo.logic import queues |
36 |
|
37 |
import json |
38 |
import logging |
39 |
log = logging.getLogger("")
|
40 |
|
41 |
|
42 |
class Command(SynnefoCommand): |
43 |
help = "Resend messages from dead letter queues to original exchange""" |
44 |
|
45 |
option_list = SynnefoCommand.option_list + ( |
46 |
make_option( |
47 |
'--keep-zombies',
|
48 |
action='store_true',
|
49 |
dest='keep_zombies',
|
50 |
default=False,
|
51 |
help="Do not remove messages that died more than one times"),
|
52 |
) |
53 |
|
54 |
def handle(self, *args, **options): |
55 |
verbose = (options["verbosity"] == "2") |
56 |
self.keep_zombies = options["keep_zombies"] |
57 |
log_level = logging.DEBUG if verbose else logging.WARNING |
58 |
log.setLevel(log_level) |
59 |
|
60 |
client = AMQPClient(confirms=False)
|
61 |
client.connect() |
62 |
|
63 |
self.client = client
|
64 |
|
65 |
for queue in queues.QUEUES: |
66 |
dead_queue = queues.convert_queue_to_dead(queue) |
67 |
while 1: |
68 |
message = client.basic_get(dead_queue) |
69 |
if not message: |
70 |
break
|
71 |
log.debug("Received message %s", message)
|
72 |
self.handle_message(message)
|
73 |
client.close() |
74 |
return 0 |
75 |
|
76 |
def handle_message(self, message): |
77 |
try:
|
78 |
body = message['body']
|
79 |
except KeyError: |
80 |
log.warning("Received message without body: %s", message)
|
81 |
return
|
82 |
|
83 |
try:
|
84 |
body = json.loads(body) |
85 |
except ValueError: |
86 |
log.error("Removed malformed message with body %s", body)
|
87 |
self.client.basic_nack(message)
|
88 |
return
|
89 |
|
90 |
if "from-dead-letter" in message['headers']: |
91 |
if not self.keep_zombies: |
92 |
log.info("Removed message that died twice %s", body)
|
93 |
self.client.basic_nack(message)
|
94 |
return
|
95 |
|
96 |
try:
|
97 |
headers = message['headers']
|
98 |
death = headers['x-death'][0] |
99 |
except KeyError: |
100 |
log.warning("Received message without death section %s."
|
101 |
"Removing..",
|
102 |
message) |
103 |
self.client.basic_nack(message)
|
104 |
|
105 |
# Get Routing Info
|
106 |
exchange = death['exchange']
|
107 |
routing_key = death['routing-keys'][0] |
108 |
|
109 |
# Add after Death
|
110 |
body = json.dumps(body) |
111 |
|
112 |
self.client.basic_publish(exchange, routing_key, body,
|
113 |
headers={"from-dead-letter": True}) |
114 |
self.client.basic_ack(message)
|