-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #659 from weni-ai/feature/rabbitmq-apps
Feature/rabbitmq apps
- Loading branch information
Showing
21 changed files
with
440 additions
and
1,393 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Generated by Django 3.2.20 on 2023-07-27 14:00 | ||
|
||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('template_projects', '0004_templatetype_uuid'), | ||
('common', '0079_auto_20230523_1821'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='project', | ||
name='project_template_type', | ||
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='template_projects', to='template_projects.templatetype'), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class InternalsConfig(AppConfig): | ||
default_auto_field = 'django.db.models.BigAutoField' | ||
name = 'connect.internals' |
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from pika import BlockingConnection, ConnectionParameters, PlainCredentials | ||
from django.conf import settings | ||
|
||
|
||
class RabbitMQConnection: # pragma: no cover | ||
_instance = None | ||
|
||
def __new__(cls): | ||
if cls._instance is None: | ||
cls._instance = super(RabbitMQConnection, cls).__new__(cls) | ||
cls._instance.connect() | ||
return cls._instance | ||
|
||
def connect(self): | ||
if not hasattr(self, "connection"): | ||
self.connection = BlockingConnection(ConnectionParameters( | ||
host=settings.EDA_BROKER_HOST, | ||
port=settings.EDA_BROKER_PORT, | ||
credentials=PlainCredentials( | ||
username=settings.EDA_BROKER_USER, | ||
password=settings.EDA_BROKER_PASSWORD | ||
) | ||
)) | ||
self.channel = self.connection.channel() | ||
|
||
def close(self): | ||
if hasattr(self, "connection"): | ||
self.connection.close() |
Empty file.
26 changes: 26 additions & 0 deletions
26
connect/internals/event_driven/producer/rabbitmq_publisher.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import json | ||
|
||
from pika import BasicProperties | ||
from typing import Dict | ||
|
||
from connect.internals.event_driven.connection.rabbitmq import RabbitMQConnection | ||
|
||
|
||
class RabbitmqPublisher: # pragma: no cover | ||
def __init__(self) -> None: | ||
self.rabbitmq_connection = RabbitMQConnection() | ||
|
||
def send_message(self, body: Dict, exchange: str, routing_key: str): | ||
try: | ||
self.rabbitmq_connection.channel.basic_publish( | ||
exchange=exchange, | ||
routing_key=routing_key, | ||
body=json.dumps(body), | ||
properties=BasicProperties( | ||
delivery_mode=2 | ||
) | ||
) | ||
print("Message sent") | ||
except Exception as e: | ||
print(e) | ||
raise e |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
connect/template_projects/migrations/0004_templatetype_uuid.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Generated by Django 3.2.20 on 2023-07-27 14:00 | ||
|
||
from django.db import migrations, models | ||
import uuid | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('template_projects', '0003_templatetype_photo'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='templatetype', | ||
name='uuid', | ||
field=models.UUIDField(default=uuid.uuid4, verbose_name='UUID'), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import uuid | ||
|
||
from django.db.models.signals import post_save | ||
from django.dispatch import receiver | ||
from django.conf import settings | ||
|
||
from connect.internals.event_driven.producer.rabbitmq_publisher import RabbitmqPublisher | ||
from connect.template_projects.models import TemplateType | ||
|
||
|
||
@receiver(post_save, sender=TemplateType) | ||
def create_template_type(sender, instance, created, **kwargs): | ||
if created and not settings.TESTING: | ||
rabbitmq_publisher = RabbitmqPublisher() | ||
if instance.uuid is None: | ||
instance.uuid = uuid.uuid4() | ||
instance.save() | ||
|
||
message_body = { | ||
"uuid": str(instance.uuid), | ||
"name": instance.name | ||
} | ||
|
||
rabbitmq_publisher.send_message(message_body, exchange="template-types.topic", routing_key="") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.