root / logic / amqp_connection.py @ a4d2780c
History | View | Annotate | Download (3.5 kB)
1 | 464a3579 | Georgios Gousios | # Copyright 2011 GRNET S.A. All rights reserved.
|
---|---|---|---|
2 | 464a3579 | Georgios Gousios | #
|
3 | 464a3579 | Georgios Gousios | # Redistribution and use in source and binary forms, with or without
|
4 | 464a3579 | Georgios Gousios | # modification, are permitted provided that the following conditions
|
5 | 464a3579 | Georgios Gousios | # are met:
|
6 | 464a3579 | Georgios Gousios | #
|
7 | 464a3579 | Georgios Gousios | # 1. Redistributions of source code must retain the above copyright
|
8 | 464a3579 | Georgios Gousios | # notice, this list of conditions and the following disclaimer.
|
9 | 464a3579 | Georgios Gousios | #
|
10 | 464a3579 | Georgios Gousios | # 2. Redistributions in binary form must reproduce the above copyright
|
11 | 464a3579 | Georgios Gousios | # notice, this list of conditions and the following disclaimer in the
|
12 | 464a3579 | Georgios Gousios | # documentation and/or other materials provided with the distribution.
|
13 | 464a3579 | Georgios Gousios | #
|
14 | 464a3579 | Georgios Gousios | # THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
15 | 464a3579 | Georgios Gousios | # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
16 | 464a3579 | Georgios Gousios | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
17 | 464a3579 | Georgios Gousios | # ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
18 | 464a3579 | Georgios Gousios | # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
19 | 464a3579 | Georgios Gousios | # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
20 | 464a3579 | Georgios Gousios | # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
21 | 464a3579 | Georgios Gousios | # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
22 | 464a3579 | Georgios Gousios | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
23 | 464a3579 | Georgios Gousios | # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
24 | 464a3579 | Georgios Gousios | # SUCH DAMAGE.
|
25 | 464a3579 | Georgios Gousios | #
|
26 | 464a3579 | Georgios Gousios | # The views and conclusions contained in the software and documentation are
|
27 | 464a3579 | Georgios Gousios | # those of the authors and should not be interpreted as representing official
|
28 | 464a3579 | Georgios Gousios | # policies, either expressed or implied, of GRNET S.A.
|
29 | 464a3579 | Georgios Gousios | |
30 | 4f6e36d9 | Georgios Gousios | import time |
31 | 75d20793 | Georgios Gousios | import socket |
32 | 4f6e36d9 | Georgios Gousios | from amqplib import client_0_8 as amqp |
33 | 4f6e36d9 | Georgios Gousios | from django.conf import settings |
34 | f2bdb9ab | Georgios Gousios | from synnefo.logic import log |
35 | 4f6e36d9 | Georgios Gousios | |
36 | 75d20793 | Georgios Gousios | _conn = None
|
37 | 75d20793 | Georgios Gousios | _chan = None
|
38 | f2bdb9ab | Georgios Gousios | _logger = log.get_logger("amqplib")
|
39 | 4f6e36d9 | Georgios Gousios | |
40 | 75d20793 | Georgios Gousios | def _connect(): |
41 | 75d20793 | Georgios Gousios | global _conn, _chan
|
42 | 75d20793 | Georgios Gousios | while _conn == None: |
43 | 75d20793 | Georgios Gousios | try:
|
44 | 75d20793 | Georgios Gousios | _conn = amqp.Connection(host=settings.RABBIT_HOST, |
45 | 75d20793 | Georgios Gousios | userid=settings.RABBIT_USERNAME, |
46 | 75d20793 | Georgios Gousios | password=settings.RABBIT_PASSWORD, |
47 | 75d20793 | Georgios Gousios | virtual_host=settings.RABBIT_VHOST) |
48 | 75d20793 | Georgios Gousios | except socket.error:
|
49 | 75d20793 | Georgios Gousios | time.sleep(1)
|
50 | 75d20793 | Georgios Gousios | _chan = _conn.channel() |
51 | 4f6e36d9 | Georgios Gousios | |
52 | 4f6e36d9 | Georgios Gousios | |
53 | 75d20793 | Georgios Gousios | def send(payload, exchange, key): |
54 | 5ad78098 | Georgios Gousios | """
|
55 | 464a3579 | Georgios Gousios | Send payload to the specified exchange using the provided routing key.
|
56 | 464a3579 | Georgios Gousios |
|
57 | 464a3579 | Georgios Gousios | This method will try reconnecting to the message server if a connection
|
58 | 464a3579 | Georgios Gousios | error occurs when sending the message. All other errors are forwarded
|
59 | 464a3579 | Georgios Gousios | to the client.
|
60 | 5ad78098 | Georgios Gousios | """
|
61 | 75d20793 | Georgios Gousios | global _conn, _chan
|
62 | 6afc46cb | Georgios Gousios | |
63 | 6afc46cb | Georgios Gousios | if payload is None: |
64 | 6afc46cb | Georgios Gousios | raise AMQPError("Message is empty") |
65 | 6afc46cb | Georgios Gousios | |
66 | 6afc46cb | Georgios Gousios | if exchange is None: |
67 | 6afc46cb | Georgios Gousios | raise AMQPError("Unknown exchange") |
68 | 6afc46cb | Georgios Gousios | |
69 | 6afc46cb | Georgios Gousios | if key is None: |
70 | 6afc46cb | Georgios Gousios | raise AMQPError("Unknown routing key") |
71 | 6afc46cb | Georgios Gousios | |
72 | 75d20793 | Georgios Gousios | msg = amqp.Message(payload) |
73 | 75d20793 | Georgios Gousios | msg.properties["delivery_mode"] = 2 # Persistent |
74 | 4f6e36d9 | Georgios Gousios | |
75 | 75d20793 | Georgios Gousios | while True: |
76 | 6afc46cb | Georgios Gousios | try:
|
77 | 75d20793 | Georgios Gousios | _chan.basic_publish(msg, |
78 | 75d20793 | Georgios Gousios | exchange=exchange, |
79 | 75d20793 | Georgios Gousios | routing_key=key) |
80 | 75d20793 | Georgios Gousios | return
|
81 | f2bdb9ab | Georgios Gousios | except socket.error as se: |
82 | f2bdb9ab | Georgios Gousios | _logger.exception("Server went away, reconnecting...")
|
83 | 75d20793 | Georgios Gousios | _connect() |
84 | 6afc46cb | Georgios Gousios | except Exception as e: |
85 | 6afc46cb | Georgios Gousios | if _conn is None: |
86 | 6afc46cb | Georgios Gousios | _connect() |
87 | 6afc46cb | Georgios Gousios | else:
|
88 | f2bdb9ab | Georgios Gousios | _logger.exception('Caught unexpected exception (msg: %s)'%msg)
|
89 | f2bdb9ab | Georgios Gousios | raise AMQPError("Error sending message to exchange %s with \ |
90 | f2bdb9ab | Georgios Gousios | key %s.Payload: %s. Error was: %s",
|
91 | f2bdb9ab | Georgios Gousios | (exchange, key, payload, e.message)) |
92 | f2bdb9ab | Georgios Gousios | |
93 | 75d20793 | Georgios Gousios | |
94 | 75d20793 | Georgios Gousios | def __init__(): |
95 | 75d20793 | Georgios Gousios | _connect() |
96 | 6afc46cb | Georgios Gousios | |
97 | 6afc46cb | Georgios Gousios | |
98 | 6afc46cb | Georgios Gousios | class AMQPError(Exception): |
99 | 6afc46cb | Georgios Gousios | def __init__(self, msg): |
100 | 6afc46cb | Georgios Gousios | self.message = msg |