-
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.
- Loading branch information
1 parent
c56ef4c
commit 093199e
Showing
14 changed files
with
435 additions
and
47 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,25 @@ | ||
name: Lifeguard RabbitMQ CI | ||
|
||
on: [push] | ||
|
||
jobs: | ||
build: | ||
|
||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
python-version: [3.7] | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Set up Python ${{ matrix.python-version }} | ||
uses: actions/setup-python@v2 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
make deps | ||
- name: Test with nose2 | ||
run: | | ||
make test |
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,32 @@ | ||
# This workflows will upload a Python Package using Twine when a release is created | ||
# For more information see: https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries | ||
|
||
name: Lifeguard RabbitMQ Publish | ||
|
||
on: | ||
release: | ||
types: [created] | ||
|
||
jobs: | ||
deploy: | ||
|
||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Set up Python | ||
uses: actions/setup-python@v2 | ||
with: | ||
python-version: '3.x' | ||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
make deps | ||
pip install setuptools wheel twine | ||
- name: Build and publish | ||
env: | ||
TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }} | ||
TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }} | ||
run: | | ||
python setup.py sdist bdist_wheel | ||
twine upload dist/* |
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,45 @@ | ||
""" | ||
RabbitMQ Plugin Context | ||
""" | ||
|
||
|
||
class RabbitMQPluginContext: | ||
""" | ||
RabbitMQ Context | ||
""" | ||
|
||
def __init__(self): | ||
self._consumers_validation_options = { | ||
"actions": [], | ||
"schedule": {"every": {"minutes": 1}}, | ||
"settings": {}, | ||
"queues": {}, | ||
} | ||
|
||
@property | ||
def consumers_validation_options(self): | ||
""" | ||
Getter for consumers validation options | ||
""" | ||
return self._consumers_validation_options | ||
|
||
@consumers_validation_options.setter | ||
def consumers_validation_options(self, value): | ||
""" | ||
Setter for consumers validation options | ||
Example: | ||
{ | ||
"actions": [], | ||
"schedule": {"every": {"minutes": 1}}, | ||
"settings": {}, | ||
"queues": { | ||
"rabbitmq_admin_instance": [{"name": "queue_name", "min_number_of_consumers": 1}] | ||
} | ||
} | ||
""" | ||
self._consumers_validation_options = value | ||
|
||
|
||
RABBITMQ_PLUGIN_CONTEXT = RabbitMQPluginContext() |
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,40 @@ | ||
""" | ||
RabbitMQ admin resources | ||
""" | ||
import json | ||
|
||
from lifeguard.http_client import get | ||
|
||
from lifeguard_rabbitmq.settings import get_rabbitmq_admin_instances | ||
|
||
BASE_URL = "{}" | ||
QUEUE = "/api/queues/{}/{}" | ||
|
||
|
||
def count_consumers(instance_name, queue): | ||
""" | ||
Get consumers for a queue | ||
""" | ||
instance_attributes = get_rabbitmq_admin_instances()[instance_name] | ||
url = __url(QUEUE, instance_attributes["base_url"]).format( | ||
__vhost(instance_attributes["vhost"]), queue | ||
) | ||
response = __get(url, instance_attributes["user"], instance_attributes["passwd"]) | ||
|
||
print("\n\n\n") | ||
print(response) | ||
print("\n\n\n") | ||
|
||
return len(response["consumer_details"]) | ||
|
||
|
||
def __get(url, user, password): | ||
return json.loads(get(url, auth=(user, password)).content) | ||
|
||
|
||
def __url(api, admin): | ||
return BASE_URL.format(admin) + api | ||
|
||
|
||
def __vhost(vhost): | ||
return vhost.replace(vhost, "%2f") |
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 |
---|---|---|
@@ -1,2 +1,59 @@ | ||
def consumers_validation(): | ||
pass | ||
""" | ||
RabbitMQ Common Validations | ||
""" | ||
import traceback | ||
|
||
from lifeguard.logger import lifeguard_logger as logger | ||
from lifeguard import NORMAL, PROBLEM, change_status | ||
from lifeguard.validations import ValidationResponse | ||
|
||
from lifeguard_rabbitmq.context import RABBITMQ_PLUGIN_CONTEXT | ||
from lifeguard_rabbitmq.rabbitmq.admin import count_consumers | ||
|
||
|
||
def __check_consumers(rabbitmq_admin_instance, queues, details): | ||
details[rabbitmq_admin_instance] = [] | ||
status = NORMAL | ||
|
||
for queue in queues: | ||
queue_status = { | ||
"queue": queue["name"], | ||
"status": NORMAL, | ||
} | ||
try: | ||
queue_status["number_of_consumers"] = count_consumers( | ||
rabbitmq_admin_instance, queue["name"] | ||
) | ||
if queue_status["number_of_consumers"] < queue["min_number_of_consumers"]: | ||
queue_status["status"] = PROBLEM | ||
except Exception as exception: | ||
logger.error( | ||
"error on recover queue infos %s", | ||
str(exception), | ||
extra={"traceback": traceback.format_exc()}, | ||
) | ||
queue_status["status"] = PROBLEM | ||
queue_status["error"] = "error on recover queue infos" | ||
|
||
details[rabbitmq_admin_instance].append(queue_status) | ||
|
||
status = change_status(status, queue_status["status"]) | ||
|
||
return status | ||
|
||
|
||
def consumers_running_validation(): | ||
""" | ||
Validates number of consumers for a queue | ||
""" | ||
options = RABBITMQ_PLUGIN_CONTEXT.consumers_validation_options | ||
status = NORMAL | ||
details = {} | ||
|
||
for rabbitmq_admin_instance in options["queues"]: | ||
queues = options["queues"][rabbitmq_admin_instance] | ||
|
||
status = change_status( | ||
status, __check_consumers(rabbitmq_admin_instance, queues, details) | ||
) | ||
return ValidationResponse("consumers_running_validation", status, details) |
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 +1 @@ | ||
lifeguard==0.0.15 | ||
lifeguard==0.0.16 |
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,22 @@ | ||
import unittest | ||
|
||
from unittest.mock import patch, MagicMock | ||
|
||
from lifeguard_rabbitmq.rabbitmq.admin import count_consumers | ||
|
||
|
||
class RabbitMQAdminTest(unittest.TestCase): | ||
@patch("lifeguard_rabbitmq.rabbitmq.admin.get") | ||
def test_count_consumers(self, mock_get): | ||
|
||
mock_response = MagicMock(name="response") | ||
mock_response.content = '{"consumer_details": []}' | ||
|
||
mock_get.return_value = mock_response | ||
|
||
self.assertEqual(count_consumers("default", "queue"), 0) | ||
|
||
mock_get.assert_called_with( | ||
"http://localhost:15672/api/queues/%2f/queue", | ||
auth=("guest", "guest"), | ||
) |
Oops, something went wrong.