HealthView'da status kod qo'shildi; RabbitMQ ulanishi qo'shildi; .gitignore yaratildi.

This commit is contained in:
A'zamov Samandar
2025-04-21 19:53:10 +05:00
parent 5a38d016a8
commit 80fa39bd1b
9 changed files with 83 additions and 20 deletions

View File

@@ -4,4 +4,4 @@ from rest_framework.response import Response
class HealthView(APIView):
def get(self, *args, **kwargs):
return Response(data={"detail": "OK"})
return Response(data={"detail": "OK"}, status=200)

31
rabbitmq.py Normal file
View File

@@ -0,0 +1,31 @@
# rabbitmq.py
from kombu import Connection, Exchange, Producer
rabbit_url = 'amqp://guest:guest@rabbitmq:5672/'
_connection = None
_channel = None
_exchange = Exchange('notification', type='direct')
_producer = None
def get_connection():
global _connection
if _connection is None or not _connection.connected:
_connection = Connection(rabbit_url)
_connection.ensure_connection(max_retries=3)
return _connection
def get_producer():
global _producer, _channel
if _producer is None:
conn = get_connection()
_channel = conn.channel()
_producer = Producer(_channel, exchange=_exchange, routing_key="notification")
return _producer
def send_notification(message: dict):
producer = get_producer()
producer.publish(message, serializer='json')

2
template/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
./staticfiles
.env

View File

@@ -119,6 +119,7 @@ USE_TZ = True
# https://docs.djangoproject.com/en/5.1/howto/static-files/
STATIC_URL = "static/"
STATIC_ROOT = BASE_DIR / "staticfiles"
# Default primary key field type
# https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field

View File

@@ -3,6 +3,9 @@ networks:
external: true
name: lamenu
volumes:
pycache: null
services:
app:
labels:
@@ -15,5 +18,9 @@ services:
build:
dockerfile: Dockerfile
context: .
networks:
- lamenu
restart: always
volumes:
- ./:/code
- pycache:/var/cache/pycache

17
test.py
View File

@@ -1,17 +0,0 @@
from kombu import Connection, Exchange, Producer
# RabbitMQ ulanishi
rabbit_url = 'amqp://guest:guest@127.0.0.1:5672/'
connection = Connection(rabbit_url)
channel = connection.channel()
exchange = Exchange('notification', type='direct')
# Producer yaratish
producer = Producer(channel, exchange=exchange, routing_key="notification")
# Xabar yuborish
message = {'type': 'sms', 'message': "classcom.uz sayti va mobil ilovasiga ro'yxatdan o'tishingingiz uchun tasdiqlash kodi: 1234", "to": ["+998888112309", "+998943990509"]}
producer.publish(message)
print("Message sent to all workers!")

View File

@@ -1,6 +1,8 @@
from datetime import datetime, timedelta
from django_core import exceptions, models, tasks
from django_core import exceptions, models
from core.utils.notification import send_notification
from config.env import env
class SmsService:
@@ -28,7 +30,8 @@ class SmsService:
) # noqa
sms_confirm.save()
tasks.SendConfirm.delay(phone, code)
# tasks.SendConfirm.delay(phone, code)
send_notification({'type': 'sms', 'message': env.str("SMS_TEMPLATE") % {"code": code}, "to": [phone]})
return True
@staticmethod

View File

@@ -0,0 +1,21 @@
# rabbitmq.py
from kombu import Exchange, Producer
from .rabbitmq import get_connection
_channel = None
_exchange = Exchange('notification', type='direct')
_producer = None
def get_producer():
global _producer, _channel
if _producer is None:
conn = get_connection()
_channel = conn.channel()
_producer = Producer(_channel, exchange=_exchange, routing_key="notification")
return _producer
def send_notification(message: dict):
producer = get_producer()
producer.publish(message, serializer='json')

View File

@@ -0,0 +1,15 @@
# rabbitmq.py
from kombu import Connection
rabbit_url = 'amqp://guest:guest@rabbitmq:5672/'
_connection = None
def get_connection():
global _connection
if _connection is None or not _connection.connected:
_connection = Connection(rabbit_url)
_connection.ensure_connection(max_retries=3)
return _connection