2020-10-09 22:16:27 +03:00
|
|
|
Asyncio Daemon + Dependency Injector Example
|
|
|
|
============================================
|
2020-08-06 05:11:26 +03:00
|
|
|
|
2020-10-09 22:16:27 +03:00
|
|
|
This is an `asyncio <https://docs.python.org/3/library/asyncio.html>`_ +
|
2020-10-20 00:18:39 +03:00
|
|
|
`Dependency Injector <https://python-dependency-injector.ets-labs.org/>`_ example application.
|
2020-10-09 22:16:27 +03:00
|
|
|
|
|
|
|
The example application is a daemon that monitors availability of web services.
|
2020-08-06 05:11:26 +03:00
|
|
|
|
|
|
|
Run
|
|
|
|
---
|
|
|
|
|
|
|
|
Build the Docker image:
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
docker-compose build
|
|
|
|
|
|
|
|
Run the docker-compose environment:
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
docker-compose up
|
|
|
|
|
|
|
|
The output should be something like:
|
|
|
|
|
|
|
|
.. code-block::
|
|
|
|
|
|
|
|
Starting monitoring-daemon-asyncio_monitor_1 ... done
|
|
|
|
Attaching to monitoring-daemon-asyncio_monitor_1
|
2020-08-08 21:48:05 +03:00
|
|
|
monitor_1 | [2020-08-08 17:04:36,655] [INFO] [Dispatcher]: Starting up
|
|
|
|
monitor_1 | [2020-08-08 17:04:36,732] [INFO] [HttpMonitor]: Check
|
|
|
|
monitor_1 | GET http://example.com
|
|
|
|
monitor_1 | response code: 200
|
|
|
|
monitor_1 | content length: 648
|
|
|
|
monitor_1 | request took: 0.074 seconds
|
|
|
|
monitor_1 | [2020-08-08 17:04:36,811] [INFO] [HttpMonitor]: Check
|
|
|
|
monitor_1 | GET https://httpbin.org/get
|
|
|
|
monitor_1 | response code: 200
|
|
|
|
monitor_1 | content length: 310
|
|
|
|
monitor_1 | request took: 0.153 seconds
|
|
|
|
monitor_1 | [2020-08-08 17:04:41,731] [INFO] [HttpMonitor]: Check
|
|
|
|
monitor_1 | GET http://example.com
|
|
|
|
monitor_1 | response code: 200
|
|
|
|
monitor_1 | content length: 648
|
|
|
|
monitor_1 | request took: 0.067 seconds
|
|
|
|
monitor_1 | [2020-08-08 17:04:41,787] [INFO] [HttpMonitor]: Check
|
|
|
|
monitor_1 | GET https://httpbin.org/get
|
|
|
|
monitor_1 | response code: 200
|
|
|
|
monitor_1 | content length: 310
|
|
|
|
monitor_1 | request took: 0.122 seconds
|
|
|
|
monitor_1 |
|
2020-08-06 05:11:26 +03:00
|
|
|
|
|
|
|
Test
|
|
|
|
----
|
|
|
|
|
|
|
|
This application comes with the unit tests.
|
|
|
|
|
|
|
|
To run the tests do:
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
docker-compose run --rm monitor py.test monitoringdaemon/tests.py --cov=monitoringdaemon
|
|
|
|
|
|
|
|
The output should be something like:
|
|
|
|
|
|
|
|
.. code-block::
|
|
|
|
|
2021-11-01 03:31:39 +03:00
|
|
|
platform linux -- Python 3.10.0, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
|
2020-08-06 05:11:26 +03:00
|
|
|
rootdir: /code
|
2021-11-01 03:31:39 +03:00
|
|
|
plugins: asyncio-0.16.0, cov-3.0.0
|
2020-08-06 05:11:26 +03:00
|
|
|
collected 2 items
|
|
|
|
|
2020-10-09 22:16:27 +03:00
|
|
|
monitoringdaemon/tests.py .. [100%]
|
2020-08-06 05:11:26 +03:00
|
|
|
|
2021-11-01 03:31:39 +03:00
|
|
|
---------- coverage: platform linux, python 3.10.0-final-0 -----------
|
2020-08-06 05:11:26 +03:00
|
|
|
Name Stmts Miss Cover
|
|
|
|
----------------------------------------------------
|
|
|
|
monitoringdaemon/__init__.py 0 0 100%
|
2021-11-01 03:31:39 +03:00
|
|
|
monitoringdaemon/__main__.py 11 11 0%
|
2020-08-06 05:11:26 +03:00
|
|
|
monitoringdaemon/containers.py 11 0 100%
|
2021-11-01 03:31:39 +03:00
|
|
|
monitoringdaemon/dispatcher.py 45 5 89%
|
2020-08-06 05:11:26 +03:00
|
|
|
monitoringdaemon/http.py 6 3 50%
|
2020-08-08 21:48:05 +03:00
|
|
|
monitoringdaemon/monitors.py 23 1 96%
|
2021-11-01 03:31:39 +03:00
|
|
|
monitoringdaemon/tests.py 35 0 100%
|
2020-08-06 05:11:26 +03:00
|
|
|
----------------------------------------------------
|
2021-11-01 03:31:39 +03:00
|
|
|
TOTAL 131 20 85%
|