-
Notifications
You must be signed in to change notification settings - Fork 194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RabbitMQ Healthcheck Contribution #197
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @IgooorGP great work! Really appreciate the effort. Especially about including tests!
I do have some comments. Please also make sure to add documentation.
.gitignore
Outdated
@@ -102,3 +102,8 @@ ENV/ | |||
|
|||
# pytest | |||
.pytest_cache/ | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add editor specific ignores to you global git ignore. Not to a repository.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello there @codingjoe! I have removed these files to a global git ignore. Sorry for this mistake.
|
||
def ready(self): | ||
from .backends import RabbitMQHealthCheck | ||
plugin_dir.register(RabbitMQHealthCheck) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing newline
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
class RabbitMQHealthCheck(BaseHealthCheckBackend): | ||
""" | ||
Healthchecker for RabbitMQ. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please drop trailing blank line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
def __get_broker_url(self): | ||
""" | ||
Gets the broker url based on django.confg.settings dictionary. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please drop trailing blank line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have removed this method due to the mentioned complexity of the configuration.
""" | ||
broker_url = getattr(settings, "BROKER_URL", None) | ||
|
||
if broker_url is None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have the feeling this drives complexity. I'd rather have only one approach on how to configure this correctly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have removed this method due to the mentioned complexity. Right now, the RabbitMQ healthchecker only looks for a BROKER_URL variable on django settings dict.
|
||
logger.info("Got broker_url: %s. Creating a connection to the broker...", broker_url) | ||
|
||
conn = pika.BlockingConnection(pika.URLParameters(broker_url)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not so sure about pika. Most people will use celery and it's amqplib
fork.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have removed pika from the dependencies. Instead of it, I use Kombu which is a dependency of the Celery package and therefore comes together with Celery! Kombu uses pyamqp by default according to its docs http://docs.celeryproject.org/projects/kombu/en/latest/userguide/connections.html
logger.info("RabbitMQ is healthy.") | ||
|
||
except ConnectionClosed as e: | ||
self.add_error(ServiceUnavailable("Unable to connect to RabbitMQ: Connection refused."), e) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can also just use raise ServiceUnavailable("your message") from e
requirements-dev.txt
Outdated
@@ -9,3 +9,4 @@ pydocstyle | |||
pep8-naming | |||
pytest | |||
pytest-django | |||
pika |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yet another dependency...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed pika dependency.
setup.cfg
Outdated
@@ -35,6 +35,9 @@ norecursedirs=venv env .eggs | |||
DJANGO_SETTINGS_MODULE=tests.testapp.settings | |||
addopts = --tb=short -rxs | |||
|
|||
[pep8] | |||
max-line-length = 119 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unrelated change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed this unnecessary change.
tests/test_rabbitmq.py
Outdated
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Kudos for writing tests. I really appreciate it. But we use pytest as a test runner.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed from the tests.
Hello @codingjoe! Thanks for the code review. I have implemented the requested changes. Please, could you take another look to see if it is acceptable now? Should you have any comments or new requests, please do so. Thanks a lot! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Almost there! Just some small changes and this is ready to roll. Thanks 👍
broker_url = getattr(settings, "BROKER_URL", None) | ||
|
||
try: | ||
logger.info("Got %s as the broker_url. Connecting to rabbit...") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Argument for %s
is missing
broker_url = getattr(settings, "BROKER_URL", None) | ||
|
||
try: | ||
logger.info("Got %s as the broker_url. Connecting to rabbit...") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please use debug level here
conn = Connection(broker_url) | ||
|
||
# opens and closes a channel to rabbitmq to make sure the service is up | ||
logger.info("Openning a channel to RabbitMQ...") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here, debug please
conn.channel() | ||
|
||
logger.info("Closing the channel to RabbitMQ...") | ||
conn.close() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there a context manager you could use? Even if something fails the connection should be closed
|
||
broker_url = getattr(settings, "BROKER_URL", None) | ||
|
||
try: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is too much happening in the try block. Please be more narrow, where you expect the exception to occour.
Checks if RabbitMQ service is up by opening and closing | ||
a broker channel. | ||
""" | ||
logger.info("Checking for a broker_url on django settings...") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
debug please
tests/test_rabbitmq.py
Outdated
from health_check.contrib.rabbitmq.backends import RabbitMQHealthCheck | ||
|
||
|
||
class TestRabbitMQHealthCheck(unittest.TestCase): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, we don't use UnitTest
but pytest
. Please just use a call and switch the assert to simple assert
instead of self.assertEqual
and co
Hey there @codingjoe ! Thanks again for the review. I have changed the code to use the kombu connection as a context to release opened resources. Also, I decided to use the connect method of the kombu library for the healthcheck because it is easier to understand, produces the same result as before and solves the problem with too many statements in the try/except blocks. Oh, of course, I have changed the tests as well. Please, could you review the code again to make sure everything's all right? Thanks a lot! |
Codecov Report
@@ Coverage Diff @@
## master #197 +/- ##
==========================================
- Coverage 75.39% 74.27% -1.13%
==========================================
Files 29 32 +3
Lines 313 346 +33
==========================================
+ Hits 236 257 +21
- Misses 77 89 +12
Continue to review full report at Codecov.
|
Released in 3.7.0 |
This pull request contains the following implementations: