1
0
Fork 0
mirror of https://github.com/Kozea/Radicale.git synced 2025-08-04 18:22:26 +00:00
Radicale/radicale/hook/rabbitmq/__init__.py

68 lines
2.7 KiB
Python
Raw Normal View History

2025-07-22 20:58:59 +02:00
# This file is part of Radicale - CalDAV and CardDAV server
# Copyright © 2020-2024 Tuna Celik <tuna@jakpark.com>
# Copyright © 2025-2025 Peter Bieringer <pb@bieringer.de>
#
# This library is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Radicale. If not, see <http://www.gnu.org/licenses/>.
2020-08-17 02:05:02 +02:00
import pika
from pika.exceptions import ChannelWrongStateError, StreamLostError
2020-08-17 02:38:06 +02:00
2020-08-17 02:05:02 +02:00
from radicale import hook
2020-08-17 02:23:49 +02:00
from radicale.hook import HookNotificationItem
2020-08-17 15:09:38 +02:00
from radicale.log import logger
2020-08-17 02:05:02 +02:00
class Hook(hook.BaseHook):
def __init__(self, configuration):
super().__init__(configuration)
2020-08-17 15:09:38 +02:00
self._endpoint = configuration.get("hook", "rabbitmq_endpoint")
2020-08-17 03:19:27 +02:00
self._topic = configuration.get("hook", "rabbitmq_topic")
self._queue_type = configuration.get("hook", "rabbitmq_queue_type")
2020-08-17 03:19:27 +02:00
self._encoding = configuration.get("encoding", "stock")
2020-08-17 02:05:02 +02:00
2020-08-17 15:09:38 +02:00
self._make_connection_synced()
self._make_declare_queue_synced()
2020-08-17 02:05:02 +02:00
2020-08-17 15:09:38 +02:00
def _make_connection_synced(self):
parameters = pika.URLParameters(self._endpoint)
2020-08-17 03:19:27 +02:00
connection = pika.BlockingConnection(parameters)
self._channel = connection.channel()
2020-08-17 02:05:02 +02:00
2020-08-17 15:09:38 +02:00
def _make_declare_queue_synced(self):
self._channel.queue_declare(queue=self._topic, durable=True, arguments={"x-queue-type": self._queue_type})
2020-08-17 02:05:02 +02:00
2020-08-17 02:14:04 +02:00
def notify(self, notification_item):
if isinstance(notification_item, HookNotificationItem):
2020-08-17 15:09:38 +02:00
self._notify(notification_item, True)
def _notify(self, notification_item, recall):
try:
2020-08-17 03:19:27 +02:00
self._channel.basic_publish(
2020-08-17 02:14:04 +02:00
exchange='',
2020-08-17 03:19:27 +02:00
routing_key=self._topic,
2020-08-17 03:32:13 +02:00
body=notification_item.to_json().encode(
encoding=self._encoding
)
2020-08-17 03:01:21 +02:00
)
except Exception as e:
if (isinstance(e, ChannelWrongStateError) or
isinstance(e, StreamLostError)) and recall:
2020-08-17 15:09:38 +02:00
self._make_connection_synced()
self._notify(notification_item, False)
return
logger.error("An exception occurred during "
2020-08-17 15:09:38 +02:00
"publishing hook notification item: %s",
e, exc_info=True)