{"id":308197,"date":"2020-08-09T09:00:12","date_gmt":"2020-08-09T09:00:12","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=308197"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=308197","title":{"rendered":"\u041c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u043d\u0433 \u0434\u0435\u043c\u043e\u043d \u043d\u0430 Asyncio + Dependency Injector \u2014 \u0440\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u043e \u043f\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044e dependency injection"},"content":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/514384\/\">\u041f\u0440\u0438\u0432\u0435\u0442,<\/p>\n<p>  \u042f \u0441\u043e\u0437\u0434\u0430\u0442\u0435\u043b\u044c <a href=\"https:\/\/github.com\/ets-labs\/python-dependency-injector\" rel=\"nofollow\">Dependency Injector<\/a>. \u042d\u0442\u043e dependency injection \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a \u0434\u043b\u044f Python.<\/p>\n<p>  \u042d\u0442\u043e \u0435\u0449\u0435 \u043e\u0434\u043d\u043e \u0440\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u043e \u043f\u043e \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e Dependency Injector.<\/p>\n<p>  \u0421\u0435\u0433\u043e\u0434\u043d\u044f \u0445\u043e\u0447\u0443 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u0434\u0435\u043c\u043e\u043d \u043d\u0430 \u0431\u0430\u0437\u0435 \u043c\u043e\u0434\u0443\u043b\u044f <a href=\"https:\/\/docs.python.org\/3\/library\/asyncio.html\" rel=\"nofollow\"><code>asyncio<\/code><\/a>.<\/p>\n<p>  \u0420\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u043e \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u0442\u0430\u043a\u0438\u0445 \u0447\u0430\u0441\u0442\u0435\u0439:<br \/>  <a name=\"habracut\"><\/a>  <\/p>\n<ol>\n<li><a href=\"#what-we-are-going-to-build\">\u0427\u0442\u043e \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0441\u0442\u0440\u043e\u0438\u0442\u044c?<\/a><\/li>\n<li><a href=\"#prerequisites\">\u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u0432<\/a><\/li>\n<li><a href=\"#structure\">\u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u043f\u0440\u043e\u0435\u043a\u0442\u0430<\/a><\/li>\n<li><a href=\"#prepare-the-env\">\u041f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043a\u0430 \u043e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u044f<\/a><\/li>\n<li><a href=\"#logging-and-configuration\">\u041b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044f<\/a><\/li>\n<li><a href=\"#dispatcher\">\u0414\u0438\u0441\u043f\u0435\u0442\u0447\u0435\u0440<\/a><\/li>\n<li><a href=\"#monitor-example-com\">\u041c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u043d\u0433 example.com<\/a><\/li>\n<li><a href=\"#monitor-httpbin-org\">\u041c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u043d\u0433 httpbin.org<\/a><\/li>\n<li><a href=\"#tests\">\u0422\u0435\u0441\u0442\u044b<\/a><\/li>\n<li><a href=\"#conclusion\">\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/a><\/li>\n<\/ol>\n<p>  \u0417\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u043d\u044b\u0439 \u043f\u0440\u043e\u0435\u043a\u0442 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u043d\u0430 <a href=\"https:\/\/github.com\/ets-labs\/python-dependency-injector\/tree\/master\/examples\/miniapps\/monitoring-daemon-asyncio\" rel=\"nofollow\">Github<\/a>.<\/p>\n<p>  \u0414\u043b\u044f \u0441\u0442\u0430\u0440\u0442\u0430 \u0436\u0435\u043b\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0438\u043c\u0435\u0442\u044c:<\/p>\n<ul>\n<li>\u041d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0435 \u0437\u043d\u0430\u043d\u0438\u044f \u043f\u043e <code>asyncio<\/code><\/li>\n<li>\u041e\u0431\u0449\u0435\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043e \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0435 dependency injection<\/li>\n<\/ul>\n<p>  <a name=\"what-we-are-going-to-build\"><\/a><\/p>\n<h3>\u0427\u0442\u043e \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0441\u0442\u0440\u043e\u0438\u0442\u044c?<\/h3>\n<p>  \u041c\u044b \u0431\u0443\u0434\u0435\u043c \u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u043d\u0433 \u0434\u0435\u043c\u043e\u043d, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0441\u043b\u0435\u0434\u0438\u0442\u044c \u0437\u0430 \u0434\u043e\u0441\u0442\u0443\u043f\u043e\u043c \u043a \u0432\u0435\u0431-\u0441\u0435\u0440\u0432\u0438\u0441\u0430\u043c.<\/p>\n<p>  \u0414\u0435\u043c\u043e\u043d \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u0441\u044b\u043b\u0430\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043a <a href=\"http:\/\/example.com\/\" rel=\"nofollow\">example.com<\/a> \u0438 <a href=\"https:\/\/httpbin.org\/\" rel=\"nofollow\">httpbin.org<\/a> \u043a\u0430\u0436\u0434\u044b\u0435 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u0435\u043a\u0443\u043d\u0434. \u041f\u0440\u0438 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0438 \u043e\u0442\u0432\u0435\u0442\u0430 \u043e\u043d \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u0432 \u043b\u043e\u0433 \u0442\u0430\u043a\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0435:<\/p>\n<ul>\n<li>\u041a\u043e\u0434 \u043e\u0442\u0432\u0435\u0442\u0430<\/li>\n<li>\u041a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0431\u0430\u0439\u0442 \u0432 \u043e\u0442\u0432\u0435\u0442\u0435<\/li>\n<li>\u0412\u0440\u0435\u043c\u044f, \u0437\u0430\u0442\u0440\u0430\u0447\u0435\u043d\u043d\u043e\u0435 \u043d\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u0430<\/li>\n<\/ul>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/hc\/0p\/te\/hc0ptem3s7towxxvfs6gnmwuqmc.png\"><\/p>\n<p>  <a name=\"prerequisites\"><\/a><\/p>\n<h3>\u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u0432<\/h3>\n<p>  \u041c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <a href=\"https:\/\/www.docker.com\/\" rel=\"nofollow\">Docker<\/a> \u0438 <a href=\"https:\/\/docs.docker.com\/compose\/\" rel=\"nofollow\">docker-compose<\/a>. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c, \u0447\u0442\u043e \u043e\u043d\u0438 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u044b:<\/p>\n<pre><code class=\"bash\">docker --version docker-compose --version <\/code><\/pre>\n<p>  \u0412\u044b\u0432\u043e\u0434 \u0434\u043e\u043b\u0436\u0435\u043d \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u043f\u0440\u0438\u0431\u043b\u0438\u0437\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0442\u0430\u043a:<\/p>\n<pre><code class=\"bash\">Docker version 19.03.12, build 48a66213fe docker-compose version 1.26.2, build eefe0d31 <\/code><\/pre>\n<p>  <\/p>\n<blockquote><p><i>\u0415\u0441\u043b\u0438 Docker \u0438\u043b\u0438 docker-compose \u043d\u0435 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u044b, \u0438\u0445 \u043d\u0443\u0436\u043d\u043e \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0435\u0440\u0435\u0434 \u0442\u0435\u043c \u043a\u0430\u043a \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u0442\u044c. \u0421\u043b\u0435\u0434\u0443\u0439\u0442\u0435 \u044d\u0442\u0438\u043c \u0440\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u0430\u043c:<\/i><\/p>\n<ul>\n<li><i><a href=\"https:\/\/docs.docker.com\/get-docker\/\" rel=\"nofollow\">\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 Docker<\/a><\/i><\/li>\n<li><i><a href=\"https:\/\/docs.docker.com\/compose\/install\/\" rel=\"nofollow\">\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 docker-compose<\/a><\/i><\/li>\n<\/ul>\n<\/blockquote>\n<p>  \u0418\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b \u0433\u043e\u0442\u043e\u0432\u044b. \u041f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u043a \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0435 \u043f\u0440\u043e\u0435\u043a\u0442\u0430.<\/p>\n<p>  <a name=\"structure\"><\/a><\/p>\n<h3>\u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u043f\u0440\u043e\u0435\u043a\u0442\u0430<\/h3>\n<p>  \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043f\u0430\u043f\u043a\u0443 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u0438 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u0432 \u043d\u0435\u0435:<\/p>\n<pre><code class=\"bash\">mkdir monitoring-daemon-tutorial cd monitoring-daemon-tutorial <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u043f\u0440\u043e\u0435\u043a\u0442\u0430. \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0444\u0430\u0439\u043b\u044b \u0438 \u043f\u0430\u043f\u043a\u0438 \u0441\u043b\u0435\u0434\u0443\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0435 \u043d\u0438\u0436\u0435. \u0412\u0441\u0435 \u0444\u0430\u0439\u043b\u044b \u043f\u043e\u043a\u0430 \u0431\u0443\u0434\u0443\u0442 \u043f\u0443\u0441\u0442\u044b\u043c\u0438. \u041c\u044b \u043d\u0430\u043f\u043e\u043b\u043d\u0438\u043c \u0438\u0445 \u043f\u043e\u0437\u0436\u0435.<\/p>\n<p>  \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u043f\u0440\u043e\u0435\u043a\u0442\u0430:<\/p>\n<pre><code class=\"bash\">.\/ \u251c\u2500\u2500 monitoringdaemon\/ \u2502   \u251c\u2500\u2500 __init__.py \u2502   \u251c\u2500\u2500 __main__.py \u2502   \u2514\u2500\u2500 containers.py \u251c\u2500\u2500 config.yml \u251c\u2500\u2500 docker-compose.yml \u251c\u2500\u2500 Dockerfile \u2514\u2500\u2500 requirements.txt <\/code><\/pre>\n<p>  \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u0433\u043e\u0442\u043e\u0432\u0430. \u041c\u044b \u0440\u0430\u0441\u0448\u0438\u0440\u0438\u043c \u0435\u0435 \u0441 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u0441\u0435\u043a\u0446\u0438\u044f\u0445.<\/p>\n<p>  \u0414\u0430\u043b\u044c\u0448\u0435 \u043d\u0430\u0441 \u0436\u0434\u0435\u0442 \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043a\u0430 \u043e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u044f.<\/p>\n<p>  <a name=\"prepare-the-env\"><\/a><\/p>\n<h3>\u041f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043a\u0430 \u043e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u044f<\/h3>\n<p>  \u0412 \u044d\u0442\u043e\u043c \u0440\u0430\u0437\u0434\u0435\u043b\u0435 \u043c\u044b \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u0438\u043c \u043e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u0435 \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u043d\u0430\u0448\u0435\u0433\u043e \u0434\u0435\u043c\u043e\u043d\u0430.<\/p>\n<p>  \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u043d\u0443\u0436\u043d\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438. \u041c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u0430\u043a\u0438\u0435 \u043f\u0430\u043a\u0435\u0442\u044b:<\/p>\n<ul>\n<li><code>dependency-injector<\/code> \u2014 dependency injection \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a<\/li>\n<li><code>aiohttp<\/code> \u2014 \u0432\u0435\u0431 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a (\u043d\u0430\u043c \u043d\u0443\u0436\u0435\u043d \u0442\u043e\u043b\u044c\u043a\u043e http \u043a\u043b\u0438\u0435\u043d\u0442)<\/li>\n<li><code>pyyaml<\/code> \u2014 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u0434\u043b\u044f \u043f\u0430\u0440\u0441\u0438\u043d\u0433\u0430 YAML \u0444\u0430\u0439\u043b\u043e\u0432, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0447\u0442\u0435\u043d\u0438\u044f \u043a\u043e\u043d\u0444\u0438\u0433\u0430<\/li>\n<li><code>pytest<\/code> \u2014 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a \u0434\u043b\u044f \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f<\/li>\n<li><code>pytest-asyncio<\/code> \u2014 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430-\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u043e\u0440 \u0434\u043b\u044f \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f <code> asyncio<\/code> \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439<\/li>\n<li><code>pytest-cov<\/code> \u2014 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430-\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u043e\u0440 \u0434\u043b\u044f \u0438\u0437\u043c\u0435\u0440\u0435\u043d\u0438\u044f \u043f\u043e\u043a\u0440\u044b\u0442\u0438\u044f \u043a\u043e\u0434\u0430 \u0442\u0435\u0441\u0442\u0430\u043c\u0438<\/li>\n<\/ul>\n<p>  \u0414\u043e\u0431\u0430\u0432\u0438\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0432 \u0444\u0430\u0439\u043b <code>requirements.txt<\/code>:<\/p>\n<pre><code class=\"bash\">dependency-injector aiohttp pyyaml pytest pytest-asyncio pytest-cov <\/code><\/pre>\n<p>  \u0418 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u043c \u0432 \u0442\u0435\u0440\u043c\u0438\u043d\u0430\u043b\u0435:<\/p>\n<pre><code class=\"bash\">pip install -r requirements.txt <\/code><\/pre>\n<p>  \u0414\u0430\u043b\u0435\u0435 \u0441\u043e\u0437\u0434\u0430\u0435\u043c <code>Dockerfile<\/code>. \u041e\u043d \u0431\u0443\u0434\u0435\u0442 \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0441\u0431\u043e\u0440\u043a\u0438 \u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u043d\u0430\u0448\u0435\u0433\u043e \u0434\u0435\u043c\u043e\u043d\u0430. \u041c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>python:3.8-buster<\/code> \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0431\u0430\u0437\u043e\u0432\u043e\u0433\u043e \u043e\u0431\u0440\u0430\u0437\u0430.<\/p>\n<p>  \u0414\u043e\u0431\u0430\u0432\u0438\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0432 \u0444\u0430\u0439\u043b <code>Dockerfile<\/code>:<\/p>\n<pre><code class=\"bash\">FROM python:3.8-buster  ENV PYTHONUNBUFFERED=1  WORKDIR \/code COPY . \/code\/  RUN apt-get install openssl \\  &amp;&amp; pip install --upgrade pip \\  &amp;&amp; pip install -r requirements.txt \\  &amp;&amp; rm -rf ~\/.cache  CMD [&quot;python&quot;, &quot;-m&quot;, &quot;monitoringdaemon&quot;] <\/code><\/pre>\n<p>  \u041f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u043c \u0448\u0430\u0433\u043e\u043c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 <code>docker-compose<\/code>.<\/p>\n<p>  \u0414\u043e\u0431\u0430\u0432\u0438\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0432 \u0444\u0430\u0439\u043b <code>docker-compose.yml<\/code>:<\/p>\n<pre><code class=\"plaintext\">version: &quot;3.7&quot;  services:    monitor:     build: .\/     image: monitoring-daemon     volumes:       - &quot;.\/:\/code&quot; <\/code><\/pre>\n<p>  \u0412\u0441\u0435 \u0433\u043e\u0442\u043e\u0432\u043e. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043c \u0441\u0431\u043e\u0440\u043a\u0443 \u043e\u0431\u0440\u0430\u0437\u0430 \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c \u0447\u0442\u043e \u043e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u0435 \u043d\u0430\u0441\u0442\u0440\u043e\u0435\u043d\u043e \u0432\u0435\u0440\u043d\u043e.<\/p>\n<p>  \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u043c \u0432 \u0442\u0435\u0440\u043c\u0438\u043d\u0430\u043b\u0435:<\/p>\n<pre><code class=\"bash\">docker-compose build <\/code><\/pre>\n<p>  \u041f\u0440\u043e\u0446\u0435\u0441\u0441 \u0441\u0431\u043e\u0440\u043a\u0438 \u043c\u043e\u0436\u0435\u0442 \u0437\u0430\u043d\u044f\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043c\u0438\u043d\u0443\u0442. \u0412 \u043a\u043e\u043d\u0446\u0435 \u0432\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0443\u0432\u0438\u0434\u0435\u0442\u044c:<\/p>\n<pre><code class=\"bash\">Successfully built 5b4ee5e76e35 Successfully tagged monitoring-daemon:latest <\/code><\/pre>\n<p>  \u041f\u043e\u0441\u043b\u0435 \u0442\u043e\u0433\u043e \u043a\u0430\u043a \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0441\u0431\u043e\u0440\u043a\u0438 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043c \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440:<\/p>\n<pre><code class=\"bash\">docker-compose up <\/code><\/pre>\n<p>  \u0412\u044b \u0443\u0432\u0438\u0434\u0438\u0442\u0435:<\/p>\n<pre><code class=\"bash\">Creating network &quot;monitoring-daemon-tutorial_default&quot; with the default driver Creating monitoring-daemon-tutorial_monitor_1 ... done Attaching to monitoring-daemon-tutorial_monitor_1 monitoring-daemon-tutorial_monitor_1 exited with code 0 <\/code><\/pre>\n<p>  \u041e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u0435 \u0433\u043e\u0442\u043e\u0432\u043e. \u041a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u0438 \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 \u043a\u043e\u0434\u043e\u043c <code>0<\/code>.<\/p>\n<p>  \u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u0448\u0430\u0433\u043e\u043c \u043c\u044b \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u043c \u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438 \u0447\u0442\u0435\u043d\u0438\u0435 \u0444\u0430\u0439\u043b\u0430 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438.<\/p>\n<p>  <a name=\"logging-and-configuration\"><\/a><\/p>\n<h3>\u041b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044f<\/h3>\n<p>  \u0412 \u044d\u0442\u043e\u043c \u0440\u0430\u0437\u0434\u0435\u043b\u0435 \u043c\u044b \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u043c \u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438 \u0447\u0442\u0435\u043d\u0438\u0435 \u0444\u0430\u0439\u043b\u0430 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438.<\/p>\n<p>  \u041d\u0430\u0447\u043d\u0435\u043c \u0441 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u043d\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u2014 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u0430 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439 (\u0434\u0430\u043b\u044c\u0448\u0435 \u043f\u0440\u043e\u0441\u0442\u043e \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u0430). \u041a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0432\u0441\u0435 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f.<\/p>\n<p>  \u0414\u043e\u0431\u0430\u0432\u0438\u043c \u043f\u0435\u0440\u0432\u044b\u0435 \u0434\u0432\u0430 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430. \u042d\u0442\u043e \u043e\u0431\u044a\u0435\u043a\u0442 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438 \u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f.<\/p>\n<p>  \u041e\u0442\u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u0443\u0435\u043c <code>containers.py<\/code>:<\/p>\n<pre><code class=\"python\">&quot;&quot;&quot;Application containers module.&quot;&quot;&quot;  import logging import sys  from dependency_injector import containers, providers   class ApplicationContainer(containers.DeclarativeContainer):     &quot;&quot;&quot;Application container.&quot;&quot;&quot;      config = providers.Configuration()      configure_logging = providers.Callable(         logging.basicConfig,         stream=sys.stdout,         level=config.log.level,         format=config.log.format,     ) <\/code><\/pre>\n<p>  <\/p>\n<blockquote><p><i>\u041c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438 \u043f\u0435\u0440\u0435\u0434 \u0442\u0435\u043c \u043a\u0430\u043a \u0437\u0430\u0434\u0430\u043b\u0438 \u0438\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f. \u042d\u0442\u043e \u043f\u0440\u0438\u043d\u0446\u0438\u043f, \u043f\u043e \u043a\u043e\u0442\u043e\u0440\u043e\u043c\u0443 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440 <code>Configuration<\/code>.<\/i><\/p>\n<p>  <i>\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c, \u043f\u043e\u0442\u043e\u043c \u0437\u0430\u0434\u0430\u0435\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f.<\/i>  <\/p><\/blockquote>\n<p>  \u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0431\u0443\u0434\u0443\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c\u0441\u044f \u0432 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u043e\u043c \u0444\u0430\u0439\u043b\u0435.<\/p>\n<p>  \u041e\u0442\u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u0443\u0435\u043c <code>config.yml<\/code>:<\/p>\n<pre><code class=\"plaintext\">log:   level: &quot;INFO&quot;   format: &quot;[%(asctime)s] [%(levelname)s] [%(name)s]: %(message)s&quot; <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u043d\u0430\u0448 \u0434\u0435\u043c\u043e\u043d. \u0415\u0451 \u043e\u0431\u044b\u0447\u043d\u043e \u043d\u0430\u0437\u044b\u0432\u0430\u044e\u0442 <code>main()<\/code>. \u041e\u043d\u0430 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440. \u041a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440 \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d \u0434\u043b\u044f \u0447\u0442\u0435\u043d\u0438\u044f \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u043e\u0433\u043e \u0444\u0430\u0439\u043b\u0430 \u0438 \u0432\u044b\u0437\u043e\u0432\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f.<\/p>\n<p>  \u041e\u0442\u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u0443\u0435\u043c <code>__main__.py<\/code>:<\/p>\n<pre><code class=\"python\">&quot;&quot;&quot;Main module.&quot;&quot;&quot;  from .containers import ApplicationContainer   def main() -&gt; None:     &quot;&quot;&quot;Run the application.&quot;&quot;&quot;     container = ApplicationContainer()      container.config.from_yaml('config.yml')     container.configure_logging()   if __name__ == '__main__':     main() <\/code><\/pre>\n<p>  <\/p>\n<blockquote><p><i>\u041a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440 \u2014 \u043f\u0435\u0440\u0432\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 \u0432 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438. \u041e\u043d \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0432\u0441\u0435\u0445 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432.<\/i><\/p><\/blockquote>\n<p>  \u041b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438 \u0447\u0442\u0435\u043d\u0438\u0435 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438 \u043d\u0430\u0441\u0442\u0440\u043e\u0435\u043d\u043e. \u0412 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u0440\u0430\u0437\u0434\u0435\u043b\u0435 \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0434\u0438\u0441\u043f\u0435\u0442\u0447\u0435\u0440 \u043c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u043d\u0433\u043e\u0432\u044b\u0445 \u0437\u0430\u0434\u0430\u0447.<\/p>\n<p>  <a name=\"dispatcher\"><\/a><\/p>\n<h3>\u0414\u0438\u0441\u043f\u0435\u0442\u0447\u0435\u0440<\/h3>\n<p>  \u041f\u0440\u0438\u0448\u043b\u043e \u0432\u0440\u0435\u043c\u044f \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0434\u0438\u0441\u043f\u0435\u0442\u0447\u0435\u0440 \u043c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u043d\u0433\u043e\u0432\u044b\u0445 \u0437\u0430\u0434\u0430\u0447.<\/p>\n<p>  \u0414\u0438\u0441\u043f\u0435\u0442\u0447\u0435\u0440 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u043c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u043d\u0433\u043e\u0432\u044b\u0445 \u0437\u0430\u0434\u0430\u0447 \u0438 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u0445 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435. \u041e\u043d \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043a\u0430\u0436\u0434\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443 \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0438 \u0441 \u0440\u0430\u0441\u043f\u0438\u0441\u0430\u043d\u0438\u0435\u043c. \u041a\u043b\u0430\u0441\u0441 <code>Monitor<\/code> \u2014 \u0431\u0430\u0437\u043e\u0432\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u0434\u043b\u044f \u043c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u043d\u0433\u043e\u0432\u044b\u0445 \u0437\u0430\u0434\u0430\u0447. \u0414\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447 \u043d\u0443\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0435 \u043a\u043b\u0430\u0441\u0441\u044b \u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434 <code>check()<\/code>.<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/ag\/t4\/iy\/agt4iyfgscply6c45jxdnuom8qm.png\"><br \/>  \u0414\u043e\u0431\u0430\u0432\u0438\u043c \u0434\u0438\u0441\u043f\u0435\u0442\u0447\u0435\u0440 \u0438 \u0431\u0430\u0437\u043e\u0432\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u043c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u043d\u0433\u043e\u0432\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438.<\/p>\n<p>  \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c <code>dispatcher.py<\/code> \u0438 <code>monitors.py<\/code> \u0432 \u043f\u0430\u043a\u0435\u0442\u0435 <code>monitoringdaemon<\/code>:<\/p>\n<pre><code class=\"bash\">.\/ \u251c\u2500\u2500 monitoringdaemon\/ \u2502   \u251c\u2500\u2500 __init__.py \u2502   \u251c\u2500\u2500 __main__.py \u2502   \u251c\u2500\u2500 containers.py \u2502   \u251c\u2500\u2500 dispatcher.py \u2502   \u2514\u2500\u2500 monitors.py \u251c\u2500\u2500 config.yml \u251c\u2500\u2500 docker-compose.yml \u251c\u2500\u2500 Dockerfile \u2514\u2500\u2500 requirements.txt <\/code><\/pre>\n<p>  \u0414\u043e\u0431\u0430\u0432\u0438\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0432 \u0444\u0430\u0439\u043b <code>monitors.py<\/code>:<\/p>\n<pre><code class=\"python\">&quot;&quot;&quot;Monitors module.&quot;&quot;&quot;  import logging   class Monitor:      def __init__(self, check_every: int) -&gt; None:         self.check_every = check_every         self.logger = logging.getLogger(self.__class__.__name__)      async def check(self) -&gt; None:         raise NotImplementedError() <\/code><\/pre>\n<p>  \u0438 \u0432 \u0444\u0430\u0439\u043b <code>dispatcher.py<\/code>:<\/p>\n<pre><code class=\"python\">&quot;&quot;&quot;&quot;Dispatcher module.&quot;&quot;&quot;  import asyncio import logging import signal import time from typing import List  from .monitors import Monitor   class Dispatcher:      def __init__(self, monitors: List[Monitor]) -&gt; None:         self._monitors = monitors         self._monitor_tasks: List[asyncio.Task] = []         self._logger = logging.getLogger(self.__class__.__name__)         self._stopping = False      def run(self) -&gt; None:         asyncio.run(self.start())      async def start(self) -&gt; None:         self._logger.info('Starting up')          for monitor in self._monitors:             self._monitor_tasks.append(                 asyncio.create_task(self._run_monitor(monitor)),             )          asyncio.get_event_loop().add_signal_handler(signal.SIGTERM, self.stop)         asyncio.get_event_loop().add_signal_handler(signal.SIGINT, self.stop)          await asyncio.gather(*self._monitor_tasks, return_exceptions=True)          self.stop()      def stop(self) -&gt; None:         if self._stopping:             return          self._stopping = True          self._logger.info('Shutting down')         for task, monitor in zip(self._monitor_tasks, self._monitors):             task.cancel()         self._logger.info('Shutdown finished successfully')      @staticmethod     async def _run_monitor(monitor: Monitor) -&gt; None:         def _until_next(last: float) -&gt; float:             time_took = time.time() - last             return monitor.check_every - time_took          while True:             time_start = time.time()              try:                 await monitor.check()             except asyncio.CancelledError:                 break             except Exception:                 monitor.logger.exception('Error executing monitor check')              await asyncio.sleep(_until_next(last=time_start)) <\/code><\/pre>\n<p>  \u0414\u0438\u0441\u043f\u0435\u0442\u0447\u0435\u0440 \u043d\u0443\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440.<\/p>\n<p>  \u041e\u0442\u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u0443\u0435\u043c <code>containers.py<\/code>:<\/p>\n<pre><code class=\"python\">&quot;&quot;&quot;Application containers module.&quot;&quot;&quot;  import logging import sys  from dependency_injector import containers, providers  from . import dispatcher   class ApplicationContainer(containers.DeclarativeContainer):     &quot;&quot;&quot;Application container.&quot;&quot;&quot;      config = providers.Configuration()      configure_logging = providers.Callable(         logging.basicConfig,         stream=sys.stdout,         level=config.log.level,         format=config.log.format,     )      dispatcher = providers.Factory(         dispatcher.Dispatcher,         monitors=providers.List(             # TODO: add monitors         ),     ) <\/code><\/pre>\n<p>  <\/p>\n<blockquote><p><i>\u041a\u0430\u0436\u0434\u044b\u0439 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0432 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440.<\/i><\/p><\/blockquote>\n<p>  \u0412 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u0438 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>main()<\/code>. \u041c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0434\u0438\u0441\u043f\u0435\u0442\u0447\u0435\u0440 \u0438\u0437 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u0430 \u0438 \u0432\u044b\u0437\u043e\u0432\u0435\u043c \u0435\u0433\u043e \u043c\u0435\u0442\u043e\u0434 <code>run()<\/code>.<\/p>\n<p>  \u041e\u0442\u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u0443\u0435\u043c <code>__main__.py<\/code>:<\/p>\n<pre><code class=\"python\">&quot;&quot;&quot;Main module.&quot;&quot;&quot;  from .containers import ApplicationContainer   def main() -&gt; None:     &quot;&quot;&quot;Run the application.&quot;&quot;&quot;     container = ApplicationContainer()      container.config.from_yaml('config.yml')     container.configure_logging()      dispatcher = container.dispatcher()     dispatcher.run()   if __name__ == '__main__':     main() <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043c \u0434\u0435\u043c\u043e\u043d \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c \u0435\u0433\u043e \u0440\u0430\u0431\u043e\u0442\u0443.<\/p>\n<p>  \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u043c \u0432 \u0442\u0435\u0440\u043c\u0438\u043d\u0430\u043b\u0435:<\/p>\n<pre><code class=\"bash\">docker-compose up <\/code><\/pre>\n<p>  \u0412\u044b\u0432\u043e\u0434 \u0434\u043e\u043b\u0436\u0435\u043d \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0442\u0430\u043a:<\/p>\n<pre><code class=\"bash\">Starting monitoring-daemon-tutorial_monitor_1 ... done Attaching to monitoring-daemon-tutorial_monitor_1 monitor_1  | [2020-08-08 16:12:35,772] [INFO] [Dispatcher]: Starting up monitor_1  | [2020-08-08 16:12:35,774] [INFO] [Dispatcher]: Shutting down monitor_1  | [2020-08-08 16:12:35,774] [INFO] [Dispatcher]: Shutdown finished successfully monitoring-daemon-tutorial_monitor_1 exited with code 0 <\/code><\/pre>\n<p>  \u0412\u0441\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0432\u0435\u0440\u043d\u043e. \u0414\u0438\u0441\u043f\u0435\u0442\u0447\u0435\u0440 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u0438 \u0432\u044b\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0442\u0430\u043a \u043a\u0430\u043a \u043c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u043d\u0433\u043e\u0432\u044b\u0445 \u0437\u0430\u0434\u0430\u0447 \u043d\u0435\u0442.<\/p>\n<p>  \u041a \u043a\u043e\u043d\u0446\u0443 \u044d\u0442\u043e\u0433\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u0430 \u043a\u0430\u0440\u043a\u0430\u0441 \u043d\u0430\u0448\u0435\u0433\u043e \u0434\u0435\u043c\u043e\u043d\u0430 \u0433\u043e\u0442\u043e\u0432. \u0412 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u0440\u0430\u0437\u0434\u0435\u043b\u0435 \u043c\u044b \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043f\u0435\u0440\u0432\u0443\u044e \u043c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u043d\u0433\u043e\u0432\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443.<\/p>\n<p>  <a name=\"monitor-example-com\"><\/a><\/p>\n<h3>\u041c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u043d\u0433 example.com<\/h3>\n<p>  \u0412 \u044d\u0442\u043e\u043c \u0440\u0430\u0437\u0434\u0435\u043b\u0435 \u043c\u044b \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u043d\u0433\u043e\u0432\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u0441\u043b\u0435\u0434\u0438\u0442\u044c \u0437\u0430 \u0434\u043e\u0441\u0442\u0443\u043f\u043e\u043c \u043a <a href=\"http:\/\/example.com\" rel=\"nofollow\">http:\/\/example.com<\/a>.<\/p>\n<p>  \u041c\u044b \u043d\u0430\u0447\u043d\u0435\u043c \u0441 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f \u043d\u0430\u0448\u0435\u0439 \u043c\u043e\u0434\u0435\u043b\u0438 \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u043d\u043e\u0432\u044b\u043c \u0442\u0438\u043f\u043e\u043c \u043c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u043d\u0433\u043e\u0432\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 <code>HttpMonitor<\/code>.<\/p>\n<p>  <code>HttpMonitor<\/code> \u044d\u0442\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u043a\u043b\u0430\u0441\u0441 <code>Monitor<\/code>. \u041c\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u043c\u0435\u0442\u043e\u0434 check(). \u041e\u043d \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c HTTP \u0437\u0430\u043f\u0440\u043e\u0441 \u0438 \u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0439 \u043e\u0442\u0432\u0435\u0442. \u0414\u0435\u0442\u0430\u043b\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f HTTP \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0431\u0443\u0434\u0443\u0442 \u0434\u0435\u043b\u0435\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u044b \u043a\u043b\u0430\u0441\u0441\u0443 <code>HttpClient<\/code>.<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/sb\/22\/se\/sb22seklgiwejrssxm7hc6ejqio.png\"><br \/>  \u0421\u043f\u0435\u0440\u0432\u0430 \u0434\u043e\u0431\u0430\u0432\u0438\u043c <code>HttpClient<\/code>.<\/p>\n<p>  \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0444\u0430\u0439\u043b <code>http.py<\/code> \u0432 \u043f\u0430\u043a\u0435\u0442\u0435 <code>monitoringdaemon<\/code>:<\/p>\n<pre><code class=\"bash\">.\/ \u251c\u2500\u2500 monitoringdaemon\/ \u2502   \u251c\u2500\u2500 __init__.py \u2502   \u251c\u2500\u2500 __main__.py \u2502   \u251c\u2500\u2500 containers.py \u2502   \u251c\u2500\u2500 dispatcher.py \u2502   \u251c\u2500\u2500 http.py \u2502   \u2514\u2500\u2500 monitors.py \u251c\u2500\u2500 config.yml \u251c\u2500\u2500 docker-compose.yml \u251c\u2500\u2500 Dockerfile \u2514\u2500\u2500 requirements.txt <\/code><\/pre>\n<p>  \u0418 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0432 \u043d\u0435\u0433\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0441\u0442\u0440\u043e\u043a\u0438:<\/p>\n<pre><code class=\"python\">&quot;&quot;&quot;Http client module.&quot;&quot;&quot;  from aiohttp import ClientSession, ClientTimeout, ClientResponse   class HttpClient:      async def request(self, method: str, url: str, timeout: int) -&gt; ClientResponse:         async with ClientSession(timeout=ClientTimeout(timeout)) as session:             async with session.request(method, url) as response:                 return response <\/code><\/pre>\n<p>  \u0414\u0430\u043b\u0435\u0435 \u043d\u0443\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c <code>HttpClient<\/code> \u0432 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440.<\/p>\n<p>  \u041e\u0442\u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u0443\u0435\u043c <code>containers.py<\/code>:<\/p>\n<pre><code class=\"python\">&quot;&quot;&quot;Application containers module.&quot;&quot;&quot;  import logging import sys  from dependency_injector import containers, providers  from . import http, dispatcher   class ApplicationContainer(containers.DeclarativeContainer):     &quot;&quot;&quot;Application container.&quot;&quot;&quot;      config = providers.Configuration()      configure_logging = providers.Callable(         logging.basicConfig,         stream=sys.stdout,         level=config.log.level,         format=config.log.format,     )      http_client = providers.Factory(http.HttpClient)      dispatcher = providers.Factory(         dispatcher.Dispatcher,         monitors=providers.List(             # TODO: add monitors         ),     ) <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u0433\u043e\u0442\u043e\u0432\u044b \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c <code>HttpMonitor<\/code>. \u0414\u043e\u0431\u0430\u0432\u0438\u043c \u0435\u0433\u043e \u0432 \u043c\u043e\u0434\u0443\u043b\u044c <code>monitors<\/code>.<\/p>\n<p>  \u041e\u0442\u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u0443\u0435\u043c <code>monitors.py<\/code>:<\/p>\n<pre><code class=\"python\">&quot;&quot;&quot;Monitors module.&quot;&quot;&quot;  import logging import time from typing import Dict, Any  from .http import HttpClient   class Monitor:      def __init__(self, check_every: int) -&gt; None:         self.check_every = check_every         self.logger = logging.getLogger(self.__class__.__name__)      async def check(self) -&gt; None:         raise NotImplementedError()   class HttpMonitor(Monitor):      def __init__(             self,             http_client: HttpClient,             options: Dict[str, Any],     ) -&gt; None:         self._client = http_client         self._method = options.pop('method')         self._url = options.pop('url')         self._timeout = options.pop('timeout')         super().__init__(check_every=options.pop('check_every'))      @property     def full_name(self) -&gt; str:         return '{0}.{1}(url=&quot;{2}&quot;)'.format(__name__, self.__class__.__name__, self._url)      async def check(self) -&gt; None:         time_start = time.time()          response = await self._client.request(             method=self._method,             url=self._url,             timeout=self._timeout,         )          time_end = time.time()         time_took = time_end - time_start          self.logger.info(             'Response code: %s, content length: %s, request took: %s seconds',             response.status,             response.content_length,             round(time_took, 3)         ) <\/code><\/pre>\n<p>  \u0423 \u043d\u0430\u0441 \u0432\u0441\u0435 \u0433\u043e\u0442\u043e\u0432\u043e \u0434\u043b\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 <a href=\"http:\/\/example.com\" rel=\"nofollow\">http:\/\/example.com<\/a>. \u041d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0434\u0432\u0430 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0432 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u0435:<\/p>\n<ul>\n<li>\u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0444\u0430\u0431\u0440\u0438\u043a\u0443 <code>example_monitor<\/code>.<\/li>\n<li>\u041f\u0435\u0440\u0435\u0434\u0430\u0442\u044c <code>example_monitor<\/code> \u0432 \u0434\u0438\u0441\u043f\u0435\u0442\u0447\u0435\u0440.<\/li>\n<\/ul>\n<p>  \u041e\u0442\u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u0443\u0435\u043c <code>containers.py<\/code>:<\/p>\n<pre><code class=\"python\">&quot;&quot;&quot;Application containers module.&quot;&quot;&quot;  import logging import sys  from dependency_injector import containers, providers  from . import http, monitors, dispatcher   class ApplicationContainer(containers.DeclarativeContainer):     &quot;&quot;&quot;Application container.&quot;&quot;&quot;      config = providers.Configuration()      configure_logging = providers.Callable(         logging.basicConfig,         stream=sys.stdout,         level=config.log.level,         format=config.log.format,     )      http_client = providers.Factory(http.HttpClient)      example_monitor = providers.Factory(         monitors.HttpMonitor,         http_client=http_client,         options=config.monitors.example,     )      dispatcher = providers.Factory(         dispatcher.Dispatcher,         monitors=providers.List(             example_monitor,         ),     ) <\/code><\/pre>\n<p>  \u041f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440 <code>example_monitor<\/code> \u0438\u043c\u0435\u0435\u0442 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u044c \u043e\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u044d\u0442\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f:<\/p>\n<p>  \u041e\u0442\u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u0443\u0435\u043c <code>config.yml<\/code>:<\/p>\n<pre><code class=\"plaintext\">log:   level: &quot;INFO&quot;   format: &quot;[%(asctime)s] [%(levelname)s] [%(name)s]: %(message)s&quot;  monitors:    example:     method: &quot;GET&quot;     url: &quot;http:\/\/example.com&quot;     timeout: 5     check_every: 5 <\/code><\/pre>\n<p>  \u0412\u0441\u0435 \u0433\u043e\u0442\u043e\u0432\u043e. \u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0434\u0435\u043c\u043e\u043d \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u0440\u0430\u0431\u043e\u0442\u0443.<\/p>\n<p>  \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c \u0432 \u0442\u0435\u0440\u043c\u0438\u043d\u0430\u043b\u0435:<\/p>\n<pre><code class=\"bash\">docker-compose up <\/code><\/pre>\n<p>  \u0418 \u0432\u0438\u0434\u0438\u043c \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0439 \u0432\u044b\u0432\u043e\u0434:<\/p>\n<pre><code class=\"bash\">Starting monitoring-daemon-tutorial_monitor_1 ... done Attaching to monitoring-daemon-tutorial_monitor_1 monitor_1  | [2020-08-08 17:06:41,965] [INFO] [Dispatcher]: Starting up monitor_1  | [2020-08-08 17:06:42,033] [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  | monitor_1  | [2020-08-08 17:06:47,040] [INFO] [HttpMonitor]: Check monitor_1  |     GET http:\/\/example.com monitor_1  |     response code: 200 monitor_1  |     content length: 648 monitor_1  |     request took: 0.073 seconds <\/code><\/pre>\n<p>  \u041d\u0430\u0448 \u0434\u0435\u043c\u043e\u043d \u043c\u043e\u0436\u0435\u0442 \u0441\u043b\u0435\u0434\u0438\u0442\u044c \u0437\u0430 \u043d\u0430\u043b\u0438\u0447\u0438\u0435\u043c \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a <a href=\"http:\/\/example.com\" rel=\"nofollow\">http:\/\/example.com<\/a>.<\/p>\n<p>  \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u043d\u0433 <a href=\"https:\/\/httpbin.org\" rel=\"nofollow\">https:\/\/httpbin.org<\/a>.<\/p>\n<p>  <a name=\"monitor-httpbin-org\"><\/a><\/p>\n<h3>\u041c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u043d\u0433 httpbin.org<\/h3>\n<p>  \u0412 \u044d\u0442\u043e\u043c \u0440\u0430\u0437\u0434\u0435\u043b\u0435 \u043c\u044b \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u043d\u0433\u043e\u0432\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u0441\u043b\u0435\u0434\u0438\u0442\u044c \u0437\u0430 \u0434\u043e\u0441\u0442\u0443\u043f\u043e\u043c \u043a <a href=\"http:\/\/example.com\" rel=\"nofollow\">http:\/\/example.com<\/a>.<\/p>\n<p>  \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u043d\u0433\u043e\u0432\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u0434\u043b\u044f <a href=\"https:\/\/httpbin.org\" rel=\"nofollow\">https:\/\/httpbin.org<\/a> \u0431\u0443\u0434\u0435\u0442 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043b\u0435\u0433\u0447\u0435, \u0442\u0430\u043a \u043a\u0430\u043a \u0432\u0441\u0435 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b \u0443\u0436\u0435 \u0433\u043e\u0442\u043e\u0432\u044b. \u041d\u0430\u043c \u043f\u0440\u043e\u0441\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440 \u0432 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044e.<\/p>\n<p>  \u041e\u0442\u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u0443\u0435\u043c <code>containers.py<\/code>:<\/p>\n<pre><code class=\"python\">&quot;&quot;&quot;Application containers module.&quot;&quot;&quot;  import logging import sys  from dependency_injector import containers, providers  from . import http, monitors, dispatcher   class ApplicationContainer(containers.DeclarativeContainer):     &quot;&quot;&quot;Application container.&quot;&quot;&quot;      config = providers.Configuration()      configure_logging = providers.Callable(         logging.basicConfig,         stream=sys.stdout,         level=config.log.level,         format=config.log.format,     )      http_client = providers.Factory(http.HttpClient)      example_monitor = providers.Factory(         monitors.HttpMonitor,         http_client=http_client,         options=config.monitors.example,     )      httpbin_monitor = providers.Factory(         monitors.HttpMonitor,         http_client=http_client,         options=config.monitors.httpbin,     )      dispatcher = providers.Factory(         dispatcher.Dispatcher,         monitors=providers.List(             example_monitor,             httpbin_monitor,         ),     ) <\/code><\/pre>\n<p>  \u041e\u0442\u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u0443\u0435\u043c <code>config.yml<\/code>:<\/p>\n<pre><code class=\"plaintext\">log:   level: &quot;INFO&quot;   format: &quot;[%(asctime)s] [%(levelname)s] [%(name)s]: %(message)s&quot;  monitors:    example:     method: &quot;GET&quot;     url: &quot;http:\/\/example.com&quot;     timeout: 5     check_every: 5    httpbin:     method: &quot;GET&quot;     url: &quot;https:\/\/httpbin.org\/get&quot;     timeout: 5     check_every: 5 <\/code><\/pre>\n<p>  \u0417\u0430\u043f\u0443\u0441\u0442\u0438\u043c \u0434\u0435\u043c\u043e\u043d \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c \u043b\u043e\u0433\u0438.<\/p>\n<p>  \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u043c \u0432 \u0442\u0435\u0440\u043c\u0438\u043d\u0430\u043b\u0435:<\/p>\n<pre><code class=\"bash\">docker-compose up <\/code><\/pre>\n<p>  \u0418 \u0432\u0438\u0434\u0438\u043c \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0439 \u0432\u044b\u0432\u043e\u0434:<\/p>\n<pre><code class=\"bash\">Starting monitoring-daemon-tutorial_monitor_1 ... done Attaching to monitoring-daemon-tutorial_monitor_1 monitor_1  | [2020-08-08 18:09:08,540] [INFO] [Dispatcher]: Starting up monitor_1  | [2020-08-08 18:09:08,618] [INFO] [HttpMonitor]: Check monitor_1  |     GET http:\/\/example.com monitor_1  |     response code: 200 monitor_1  |     content length: 648 monitor_1  |     request took: 0.077 seconds monitor_1  | monitor_1  | [2020-08-08 18:09:08,722] [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.18 seconds monitor_1  | monitor_1  | [2020-08-08 18:09:13,619] [INFO] [HttpMonitor]: Check monitor_1  |     GET http:\/\/example.com monitor_1  |     response code: 200 monitor_1  |     content length: 648 monitor_1  |     request took: 0.066 seconds monitor_1  | monitor_1  | [2020-08-08 18:09:13,681] [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.126 seconds <\/code><\/pre>\n<p>  \u0424\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0430. \u0414\u0435\u043c\u043e\u043d \u0441\u043b\u0435\u0434\u0438\u0442 \u0437\u0430 \u043d\u0430\u043b\u0438\u0447\u0438\u0435\u043c \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a <a href=\"http:\/\/example.com\" rel=\"nofollow\">http:\/\/example.com<\/a> \u0438 <a href=\"https:\/\/httpbin.org\" rel=\"nofollow\">https:\/\/httpbin.org<\/a>.<\/p>\n<p>  \u0412 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u0440\u0430\u0437\u0434\u0435\u043b\u0435 \u043c\u044b \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0442\u0435\u0441\u0442\u043e\u0432.<\/p>\n<p>  <a name=\"tests\"><\/a><\/p>\n<h3>\u0422\u0435\u0441\u0442\u044b<\/h3>\n<p>  \u0411\u044b\u043b\u043e \u0431\u044b \u043d\u0435\u043f\u043b\u043e\u0445\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0442\u0435\u0441\u0442\u043e\u0432. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u044d\u0442\u043e.<\/p>\n<p>  \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0444\u0430\u0439\u043b <code>tests.py<\/code> \u0432 \u043f\u0430\u043a\u0435\u0442\u0435 <code>monitoringdaemon<\/code>:<\/p>\n<pre><code class=\"bash\">.\/ \u251c\u2500\u2500 monitoringdaemon\/ \u2502   \u251c\u2500\u2500 __init__.py \u2502   \u251c\u2500\u2500 __main__.py \u2502   \u251c\u2500\u2500 containers.py \u2502   \u251c\u2500\u2500 dispatcher.py \u2502   \u251c\u2500\u2500 http.py \u2502   \u251c\u2500\u2500 monitors.py \u2502   \u2514\u2500\u2500 tests.py \u251c\u2500\u2500 config.yml \u251c\u2500\u2500 docker-compose.yml \u251c\u2500\u2500 Dockerfile \u2514\u2500\u2500 requirements.txt <\/code><\/pre>\n<p>  \u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0432 \u043d\u0435\u0433\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0441\u0442\u0440\u043e\u043a\u0438:<\/p>\n<pre><code class=\"python\">&quot;&quot;&quot;Tests module.&quot;&quot;&quot;  import asyncio import dataclasses from unittest import mock  import pytest  from .containers import ApplicationContainer   @dataclasses.dataclass class RequestStub:     status: int     content_length: int   @pytest.fixture def container():     container = ApplicationContainer()     container.config.from_dict({         'log': {             'level': 'INFO',             'formant': '[%(asctime)s] [%(levelname)s] [%(name)s]: %(message)s',         },         'monitors': {             'example': {                 'method': 'GET',                 'url': 'http:\/\/fake-example.com',                 'timeout': 1,                 'check_every': 1,             },             'httpbin': {                 'method': 'GET',                 'url': 'https:\/\/fake-httpbin.org\/get',                 'timeout': 1,                 'check_every': 1,             },         },     })     return container   @pytest.mark.asyncio async def test_example_monitor(container, caplog):     caplog.set_level('INFO')      http_client_mock = mock.AsyncMock()     http_client_mock.request.return_value = RequestStub(         status=200,         content_length=635,     )      with container.http_client.override(http_client_mock):         example_monitor = container.example_monitor()         await example_monitor.check()      assert 'http:\/\/fake-example.com' in caplog.text     assert 'response code: 200' in caplog.text     assert 'content length: 635' in caplog.text   @pytest.mark.asyncio async def test_dispatcher(container, caplog, event_loop):     caplog.set_level('INFO')      example_monitor_mock = mock.AsyncMock()     httpbin_monitor_mock = mock.AsyncMock()      with container.example_monitor.override(example_monitor_mock), \\             container.httpbin_monitor.override(httpbin_monitor_mock):          dispatcher = container.dispatcher()         event_loop.create_task(dispatcher.start())         await asyncio.sleep(0.1)         dispatcher.stop()      assert example_monitor_mock.check.called     assert httpbin_monitor_mock.check.called <\/code><\/pre>\n<p>  \u0414\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0442\u0435\u0441\u0442\u043e\u0432 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u043c \u0432 \u0442\u0435\u0440\u043c\u0438\u043d\u0430\u043b\u0435:<\/p>\n<pre><code class=\"bash\">docker-compose run --rm monitor py.test monitoringdaemon\/tests.py --cov=monitoringdaemon <\/code><\/pre>\n<p>  \u0414\u043e\u043b\u0436\u0435\u043d \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c\u0441\u044f \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442:<\/p>\n<pre><code class=\"bash\">platform linux -- Python 3.8.3, pytest-6.0.1, py-1.9.0, pluggy-0.13.1 rootdir: \/code plugins: asyncio-0.14.0, cov-2.10.0 collected 2 items  monitoringdaemon\/tests.py ..                                    [100%]  ----------- coverage: platform linux, python 3.8.3-final-0 ----------- Name                             Stmts   Miss  Cover ---------------------------------------------------- monitoringdaemon\/__init__.py         0      0   100% monitoringdaemon\/__main__.py         9      9     0% monitoringdaemon\/containers.py      11      0   100% monitoringdaemon\/dispatcher.py      43      5    88% monitoringdaemon\/http.py             6      3    50% monitoringdaemon\/monitors.py        23      1    96% monitoringdaemon\/tests.py           37      0   100% ---------------------------------------------------- TOTAL                              129     18    86% <\/code><\/pre>\n<p>  <\/p>\n<blockquote><p><i>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043a\u0430\u043a \u0432 \u0442\u0435\u0441\u0442\u0435 <code>test_example_monitor<\/code> \u043c\u044b \u043f\u043e\u0434\u043c\u0435\u043d\u044f\u0435\u043c <code>HttpClient<\/code> \u043c\u043e\u043a\u043e\u043c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u0435\u0442\u043e\u0434\u0430 <code>.override()<\/code>. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043b\u044e\u0431\u043e\u0433\u043e \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440\u0430.<br \/>  <\/i><br \/>  <i>\u0422\u0430\u043a\u0438\u0435 \u0436\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u0432 \u0442\u0435\u0441\u0442\u0435 <code>test_dispatcher<\/code> \u0434\u043b\u044f \u043f\u043e\u0434\u043c\u0435\u043d\u044b \u043c\u043e\u043a\u0430\u043c\u0438 \u043c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u043d\u0433\u043e\u0432\u044b\u0445 \u0437\u0430\u0434\u0430\u0447.<\/i>  <\/p><\/blockquote>\n<p>  <a name=\"conclusion\"><\/a><\/p>\n<h3>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h3>\n<p>  \u041c\u044b \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u043b\u0438 \u043c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u043d\u0433 \u0434\u0435\u043c\u043e\u043d \u043d\u0430 \u0431\u0430\u0437\u0435 <code>asyncio<\/code> \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u044f \u043f\u0440\u0438\u043d\u0446\u0438\u043f dependency injection. \u041c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 Dependency Injector \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 dependency injection \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0430.<\/p>\n<p>  \u041f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u043e, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0432\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0435 \u0441 Dependency Injector \u2014 \u044d\u0442\u043e \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440.<\/p>\n<p>  \u041a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440 \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442 \u043e\u043a\u0443\u043f\u0430\u0442\u044c\u0441\u044f, \u043a\u043e\u0433\u0434\u0430 \u0432\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u043d\u044f\u0442\u044c \u0438\u043b\u0438 \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f. \u0421 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u043e\u043c \u044d\u0442\u043e \u043b\u0435\u0433\u043a\u043e, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0432\u0441\u0435 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0438 \u0438\u0445 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u0432 \u043e\u0434\u043d\u043e\u043c \u043c\u0435\u0441\u0442\u0435:<\/p>\n<pre><code class=\"python\">&quot;&quot;&quot;Application containers module.&quot;&quot;&quot;  import logging import sys  from dependency_injector import containers, providers  from . import http, monitors, dispatcher   class ApplicationContainer(containers.DeclarativeContainer):     &quot;&quot;&quot;Application container.&quot;&quot;&quot;      config = providers.Configuration()      configure_logging = providers.Callable(         logging.basicConfig,         stream=sys.stdout,         level=config.log.level,         format=config.log.format,     )      http_client = providers.Factory(http.HttpClient)      example_monitor = providers.Factory(         monitors.HttpMonitor,         http_client=http_client,         options=config.monitors.example,     )      httpbin_monitor = providers.Factory(         monitors.HttpMonitor,         http_client=http_client,         options=config.monitors.httpbin,     )      dispatcher = providers.Factory(         dispatcher.Dispatcher,         monitors=providers.List(             example_monitor,             httpbin_monitor,         ),     ) <\/code><\/pre>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/bg\/ah\/8j\/bgah8jswglao0nmeuw43wkoy920.png\"><br \/>  <i>\u041a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440 \u043a\u0430\u043a \u043a\u0430\u0440\u0442\u0430 \u0432\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f. \u0412\u044b \u0432\u0441\u0435\u0433\u0434\u0430 \u0437\u043d\u0430\u0439\u0442\u0435 \u0447\u0442\u043e \u043e\u0442 \u0447\u0435\u0433\u043e \u0437\u0430\u0432\u0438\u0441\u0438\u0442.<\/i><\/p>\n<h4>\u0427\u0442\u043e \u0434\u0430\u043b\u044c\u0448\u0435?<\/h4>\n<p>  <\/p>\n<ul>\n<li>\u0423\u0437\u043d\u0430\u0439\u0442\u0435 \u0431\u043e\u043b\u044c\u0448\u0435 \u043e <a href=\"https:\/\/github.com\/ets-labs\/python-dependency-injector\" rel=\"nofollow\">Dependency Injector \u043d\u0430 GitHub<\/a><\/li>\n<li>\u041e\u0437\u043d\u0430\u043a\u043e\u043c\u0442\u0435\u0441\u044c \u0441 <a href=\"http:\/\/python-dependency-injector.ets-labs.org\/\" rel=\"nofollow\">\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0435\u0439 \u043d\u0430 Read the Docs<\/a><\/li>\n<li>\u0415\u0441\u0442\u044c \u0432\u043e\u043f\u0440\u043e\u0441 \u0438\u043b\u0438 \u043d\u0430\u0448\u043b\u0438 \u0431\u0430\u0433? <a href=\"https:\/\/github.com\/ets-labs\/python-dependency-injector\/issues\" rel=\"nofollow\">\u041e\u0442\u043a\u0440\u043e\u0439\u0442\u0435 issue \u043d\u0430 Github<\/a><\/li>\n<\/ul>\n<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/514384\/\"> https:\/\/habr.com\/ru\/post\/514384\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/514384\/\">\u041f\u0440\u0438\u0432\u0435\u0442,<\/p>\n<p>  \u042f \u0441\u043e\u0437\u0434\u0430\u0442\u0435\u043b\u044c <a href=\"https:\/\/github.com\/ets-labs\/python-dependency-injector\" rel=\"nofollow\">Dependency Injector<\/a>. \u042d\u0442\u043e dependency injection \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a \u0434\u043b\u044f Python.<\/p>\n<p>  \u042d\u0442\u043e \u0435\u0449\u0435 \u043e\u0434\u043d\u043e \u0440\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u043e \u043f\u043e \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e Dependency Injector.<\/p>\n<p>  \u0421\u0435\u0433\u043e\u0434\u043d\u044f \u0445\u043e\u0447\u0443 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u0434\u0435\u043c\u043e\u043d \u043d\u0430 \u0431\u0430\u0437\u0435 \u043c\u043e\u0434\u0443\u043b\u044f <a href=\"https:\/\/docs.python.org\/3\/library\/asyncio.html\" rel=\"nofollow\"><code>asyncio<\/code><\/a>.<\/p>\n<p>  \u0420\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u043e \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u0442\u0430\u043a\u0438\u0445 \u0447\u0430\u0441\u0442\u0435\u0439:  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-308197","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/308197","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=308197"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/308197\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=308197"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=308197"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=308197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}