-
Notifications
You must be signed in to change notification settings - Fork 0
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 #5 from Hanagotchi/HAN-7
HAN-7: Implementar conexión con RabbitMQ y envío de paquetes
- Loading branch information
Showing
11 changed files
with
222 additions
and
49 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
RABBITMQ_HOST= | ||
QUEUE_NAME= | ||
LOGGING_LEVEL= | ||
WEATHER_API_KEY= | ||
WEATHER_API_URL= |
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,31 @@ | ||
|
||
name: Linters | ||
|
||
on: 'push' | ||
|
||
jobs: | ||
run-linters: | ||
name: Run linters | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Check out Git repository | ||
uses: actions/checkout@v2 | ||
|
||
- name: Set up Python | ||
uses: actions/setup-python@v1 | ||
with: | ||
python-version: 3.8 | ||
|
||
- name: Install Python dependencies | ||
run: pip install black flake8 | ||
|
||
- name: Run linters | ||
uses: wearerequired/lint-action@v1 | ||
with: | ||
black: true | ||
|
||
- name: flake8 Lint | ||
uses: py-actions/flake8@v1.2.0 | ||
with: | ||
max-line-length: "88" |
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,10 @@ | ||
FROM python:3.9.7-slim | ||
|
||
WORKDIR /app | ||
|
||
COPY requirements.txt . | ||
RUN pip install --no-cache-dir -r requirements.txt | ||
|
||
COPY src src | ||
|
||
CMD ["python", "src/main.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,20 @@ | ||
default: docker-compose-up | ||
|
||
all: | ||
|
||
docker-image: | ||
docker build -f Dockerfile -t main-app . | ||
.PHONY: docker-image | ||
|
||
docker-compose-up: docker-image | ||
docker-compose -f docker-compose.yaml up -d --build | ||
.PHONY: docker-compose-up | ||
|
||
docker-compose-down: | ||
docker-compose -f docker-compose.yaml stop -t 20 | ||
docker-compose -f docker-compose.yaml down --remove-orphans | ||
.PHONY: docker-compose-down | ||
|
||
docker-compose-logs: | ||
docker-compose -f docker-compose.yaml logs -f | ||
.PHONY: docker-compose-logs |
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,16 @@ | ||
version: '3.9' | ||
|
||
services: | ||
main-app: | ||
build: | ||
context: . | ||
dockerfile: Dockerfile | ||
image: main-app | ||
env_file: | ||
- .env | ||
networks: | ||
- common_network | ||
|
||
networks: | ||
common_network: | ||
external: true |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
requests | ||
pika | ||
requests | ||
python-dotenv |
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,52 @@ | ||
import pika | ||
import os | ||
|
||
|
||
class Middleware: | ||
|
||
def __init__(self): | ||
rabbitmq_host = os.environ.get("RABBITMQ_HOST", "localhost") | ||
self._connection = pika.BlockingConnection( | ||
pika.ConnectionParameters(host=rabbitmq_host) | ||
) | ||
self._channel = self._connection.channel() | ||
self._exit = False | ||
self._remake = False | ||
|
||
def create_queue(self, queue_name): | ||
self._channel.queue_declare(queue=queue_name) | ||
|
||
def _setup_message_consumption(self, queue_name, user_function): | ||
self._channel.basic_consume(queue=queue_name, | ||
on_message_callback=lambda channel, | ||
method, properties, body: | ||
(user_function(body), | ||
channel.basic_ack | ||
(delivery_tag=method.delivery_tag), | ||
self._verify_connection_end())) | ||
self._channel.start_consuming() | ||
|
||
def _verify_connection_end(self): | ||
if self._exit: | ||
self._channel.close() | ||
if self._remake: | ||
self._exit = False | ||
self._channel = self._connection.channel() | ||
|
||
def finish(self, open_new_channel=False): | ||
self._exit = True | ||
self._remake = open_new_channel | ||
|
||
# Work queue methods | ||
def listen_on(self, queue_name, user_function): | ||
self.create_queue(queue_name) | ||
self._channel.basic_qos(prefetch_count=30) | ||
self._setup_message_consumption(queue_name, user_function) | ||
|
||
def send_message(self, queue_name, message): | ||
self._channel.basic_publish(exchange='', | ||
routing_key=queue_name, | ||
body=message) | ||
|
||
def __del__(self): | ||
self._connection.close() |
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,2 @@ | ||
[flake8] | ||
max-line-length = 88 |