Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / logic / management / commands / queue-inspect.py @ d7841399

History | View | Annotate | Download (3.3 kB)

1 8417c3df Christos Stavrakakis
# Copyright 2011-2012 GRNET S.A. All rights reserved.
2 8417c3df Christos Stavrakakis
#
3 8417c3df Christos Stavrakakis
# Redistribution and use in source and binary forms, with or without
4 8417c3df Christos Stavrakakis
# modification, are permitted provided that the following conditions
5 8417c3df Christos Stavrakakis
# are met:
6 8417c3df Christos Stavrakakis
#
7 8417c3df Christos Stavrakakis
#   1. Redistributions of source code must retain the above copyright
8 8417c3df Christos Stavrakakis
#      notice, this list of conditions and the following disclaimer.
9 8417c3df Christos Stavrakakis
#
10 8417c3df Christos Stavrakakis
#  2. Redistributions in binary form must reproduce the above copyright
11 8417c3df Christos Stavrakakis
#     notice, this list of conditions and the following disclaimer in the
12 8417c3df Christos Stavrakakis
#     documentation and/or other materials provided with the distribution.
13 8417c3df Christos Stavrakakis
#
14 8417c3df Christos Stavrakakis
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
15 8417c3df Christos Stavrakakis
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 8417c3df Christos Stavrakakis
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 8417c3df Christos Stavrakakis
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18 8417c3df Christos Stavrakakis
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 8417c3df Christos Stavrakakis
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 8417c3df Christos Stavrakakis
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 8417c3df Christos Stavrakakis
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 8417c3df Christos Stavrakakis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 8417c3df Christos Stavrakakis
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 8417c3df Christos Stavrakakis
# SUCH DAMAGE.
25 8417c3df Christos Stavrakakis
#
26 8417c3df Christos Stavrakakis
# The views and conclusions contained in the software and documentation are
27 8417c3df Christos Stavrakakis
# those of the authors and should not be interpreted as representing official
28 8417c3df Christos Stavrakakis
# policies, either expressed or implied, of GRNET S.A.
29 8417c3df Christos Stavrakakis
#
30 8417c3df Christos Stavrakakis
31 8417c3df Christos Stavrakakis
import pprint
32 8417c3df Christos Stavrakakis
33 8417c3df Christos Stavrakakis
from optparse import make_option
34 8417c3df Christos Stavrakakis
from django.core.management.base import BaseCommand, CommandError
35 8417c3df Christos Stavrakakis
36 8417c3df Christos Stavrakakis
from synnefo.lib.amqp import AMQPClient
37 8417c3df Christos Stavrakakis
38 8417c3df Christos Stavrakakis
39 8417c3df Christos Stavrakakis
class Command(BaseCommand):
40 8417c3df Christos Stavrakakis
    args = "<queue name>"
41 8417c3df Christos Stavrakakis
    help = "Inspect the messages of a queue. Close all other clients in "\
42 8417c3df Christos Stavrakakis
           "order to be able to inspect unacknowledged messages."
43 8417c3df Christos Stavrakakis
44 8417c3df Christos Stavrakakis
    option_list = BaseCommand.option_list + (
45 8417c3df Christos Stavrakakis
        make_option('--no-requeue', action='store_false', dest='requeue',
46 8417c3df Christos Stavrakakis
                    default=True, help="Do not requeue the messages"),
47 8417c3df Christos Stavrakakis
        make_option('-i', '--interactive', action='store_true', default=False,
48 8417c3df Christos Stavrakakis
                    dest='interactive', help="Interactive mode")
49 8417c3df Christos Stavrakakis
        )
50 8417c3df Christos Stavrakakis
51 8417c3df Christos Stavrakakis
    def handle(self, *args, **options):
52 8417c3df Christos Stavrakakis
        if len(args) != 1:
53 8417c3df Christos Stavrakakis
            raise CommandError("Please provide a queue")
54 8417c3df Christos Stavrakakis
55 8417c3df Christos Stavrakakis
        queue = args[0]
56 8417c3df Christos Stavrakakis
        interactive = options['interactive']
57 8417c3df Christos Stavrakakis
        requeue = options['requeue']
58 8417c3df Christos Stavrakakis
59 8417c3df Christos Stavrakakis
        client = AMQPClient()
60 8417c3df Christos Stavrakakis
        client.connect()
61 8417c3df Christos Stavrakakis
62 8417c3df Christos Stavrakakis
        pp = pprint.PrettyPrinter(indent=4, width=4)
63 8417c3df Christos Stavrakakis
64 8417c3df Christos Stavrakakis
        more_msgs = True
65 8417c3df Christos Stavrakakis
        counter = 0
66 8417c3df Christos Stavrakakis
        sep = '-' * 80
67 8417c3df Christos Stavrakakis
        while more_msgs:
68 8417c3df Christos Stavrakakis
            msg = client.basic_get(queue=queue)
69 8417c3df Christos Stavrakakis
            if msg:
70 8417c3df Christos Stavrakakis
                counter += 1
71 8417c3df Christos Stavrakakis
                print sep
72 8417c3df Christos Stavrakakis
                print 'Message %d:' % counter
73 8417c3df Christos Stavrakakis
                print sep
74 8417c3df Christos Stavrakakis
                pp.pprint(msg)
75 8417c3df Christos Stavrakakis
                if not requeue or interactive:
76 8417c3df Christos Stavrakakis
                    if interactive and not get_user_confirmation():
77 8417c3df Christos Stavrakakis
                        continue
78 8417c3df Christos Stavrakakis
                    # Acknowledging the message will remove it from the queue
79 8417c3df Christos Stavrakakis
                    client.basic_ack(msg)
80 8417c3df Christos Stavrakakis
            else:
81 8417c3df Christos Stavrakakis
                more_msgs = False
82 8417c3df Christos Stavrakakis
83 8417c3df Christos Stavrakakis
84 8417c3df Christos Stavrakakis
def get_user_confirmation():
85 8417c3df Christos Stavrakakis
    ans = raw_input("Do you want to delete this message (N/y):")
86 8417c3df Christos Stavrakakis
87 8417c3df Christos Stavrakakis
    if not ans:
88 8417c3df Christos Stavrakakis
        return False
89 8417c3df Christos Stavrakakis
    if ans not in ['Y', 'y']:
90 8417c3df Christos Stavrakakis
        return False
91 8417c3df Christos Stavrakakis
    return True