Deployment configuration
This commit is contained in:
1 parent
f67fd99333
commit
aab311dc86
13 files changed
+206
-91
No files matched your search
@@ -0,0 +1,56 @@
|
||||
import pika
|
||||
from typing import Dict
|
||||
import ssl
|
||||
from hvac_handler import get_secret
|
||||
import json
|
||||
import time
|
||||
import sys
|
||||
|
||||
rmq_username = get_secret("secret/api-internal/rmq", "username")
|
||||
rmq_password = get_secret("secret/api-internal/rmq", "password")
|
||||
|
||||
MAX_RETRIES = 5
|
||||
RETRY_DELAY = 5
|
||||
|
||||
def send_message_to_rmq(user_id: int, message: Dict):
|
||||
credentials = pika.PlainCredentials(username=rmq_username, password=rmq_password)
|
||||
context = ssl.create_default_context()
|
||||
context.check_hostname = False
|
||||
ssl_options = pika.SSLOptions(context)
|
||||
conn_params = pika.ConnectionParameters(
|
||||
host="localhost",
|
||||
port=5671,
|
||||
ssl_options=ssl_options,
|
||||
credentials=credentials,
|
||||
virtual_host="app_notifications"
|
||||
)
|
||||
|
||||
for attempt in range(1, MAX_RETRIES + 1):
|
||||
try:
|
||||
connection = pika.BlockingConnection(conn_params)
|
||||
channel = connection.channel()
|
||||
channel.exchange_declare(exchange="app_notifications", exchange_type="topic", durable=True)
|
||||
channel.confirm_delivery()
|
||||
channel.basic_publish(
|
||||
exchange='app_notifications',
|
||||
routing_key=f"notify.user.{user_id}",
|
||||
body=json.dumps(message),
|
||||
properties=pika.BasicProperties(
|
||||
content_type="application/json",
|
||||
delivery_mode=2
|
||||
),
|
||||
mandatory=True
|
||||
)
|
||||
connection.close()
|
||||
return
|
||||
|
||||
except Exception as e:
|
||||
print(f"[RMQ] Attempt {attempt} failed: {e}")
|
||||
if attempt < MAX_RETRIES:
|
||||
time.sleep(RETRY_DELAY)
|
||||
else:
|
||||
print("[RMQ] Failed to connect after maximum retries — exiting.")
|
||||
sys.exit(1)
|
||||
|
||||
if __name__ == "__main__":
|
||||
send_message_to_rmq(1, {"type": "notification", "content": "Vault TLS cert reloaded successfully."})
|
||||
Reference in new issue
Block a user