From 880a79a7adcff8a706793ebdd2a8d6dab61b5a18 Mon Sep 17 00:00:00 2001 From: Dmitry Afanasyev Date: Mon, 8 Aug 2022 13:43:01 +0300 Subject: [PATCH] uvicorn run --- gunicorn.sh | 27 +++++++++++++++++++++++++ main.py | 44 ++++++++++++++++++++++++++++++---------- poetry.lock | 54 ++++++++++++++++++++++++++++++++------------------ pyproject.toml | 2 +- 4 files changed, 96 insertions(+), 31 deletions(-) create mode 100644 gunicorn.sh diff --git a/gunicorn.sh b/gunicorn.sh new file mode 100644 index 0000000..1feaa4c --- /dev/null +++ b/gunicorn.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env sh + +set -o errexit +set -o nounset + +# We are using `gunicorn` for production, see: +# http://docs.gunicorn.org/en/stable/configure.html + +# Check that $DJANGO_ENV is set to "production", +# fail otherwise, since it may break things: + + +# Start gunicorn: +# Docs: http://docs.gunicorn.org/en/stable/settings.html +# Concerning `workers` setting see: +# https://github.com/wemake-services/wemake-django-template/issues/1022 +/usr/local/bin/gunicorn server.wsgi \ + --workers 9 \ + --timeout 150 \ + --max-requests 2000 \ + --max-requests-jitter 400 \ + --bind '0.0.0.0:8000' \ + --chdir '/opt/telebot_balsh/telebot' \ + --log-level info \ + --error-logfile "$SHARED_DIR/logs/gunicorn.log" \ + --worker-tmp-dir '/dev/shm' \ + --pid "$SHARED_DIR/logs/gunicorn_pid" diff --git a/main.py b/main.py index b399c1d..e967c31 100644 --- a/main.py +++ b/main.py @@ -1,12 +1,14 @@ from aiogram import Bot, types from aiogram.contrib.middlewares.logging import LoggingMiddleware from aiogram.dispatcher import Dispatcher -from aiogram.dispatcher.webhook import SendMessage -from aiogram.utils.executor import start_webhook +from aiogram.dispatcher.webhook import SendMessage, get_new_configured_app +from aiogram.utils.executor import start_webhook, Executor from mos_gor import logger, parse_site, download_gecko_driver, configure_firefox_driver from settings import API_TOKEN, WEBHOOK_URL, WEBHOOK_PATH, WEBAPP_HOST, WEBAPP_PORT +from aiohttp import web + bot = Bot(token=API_TOKEN) dp = Dispatcher(bot) dp.middleware.setup(LoggingMiddleware()) @@ -43,15 +45,35 @@ async def on_shutdown(dp): logger.warning('Bye!') +# async def async_app(): +# app = make_app() +# executor = Executor(dp, skip_updates=True) +# executor.on_startup(on_startup) +# executor.on_shutdown(on_shutdown) +# executor._prepare_webhook(config.WEBHOOK_PATH, app=app) +# await executor._startup_webhook() +# return app + + if __name__ == '__main__': + import uvicorn download_gecko_driver() driver = configure_firefox_driver() - start_webhook( - dispatcher=dp, - webhook_path=WEBHOOK_PATH, - on_startup=on_startup, - on_shutdown=on_shutdown, - skip_updates=True, - host=WEBAPP_HOST, - port=WEBAPP_PORT, - ) + + app = get_new_configured_app(dispatcher=dp, path=WEBHOOK_PATH) + # Setup event handlers. + app.on_startup.append(on_startup) + app.on_shutdown.append(on_shutdown) + app.skip_updates = True + + uvicorn.run(app, host=WEBAPP_HOST, port=WEBAPP_PORT) + + # start_webhook( + # dispatcher=dp, + # webhook_path=WEBHOOK_PATH, + # on_startup=on_startup, + # on_shutdown=on_shutdown, + # skip_updates=True, + # host=WEBAPP_HOST, + # port=WEBAPP_PORT, + # ) diff --git a/poetry.lock b/poetry.lock index 7bf5eac..855199f 100644 --- a/poetry.lock +++ b/poetry.lock @@ -134,6 +134,17 @@ python-versions = ">=3.6.0" [package.extras] unicode_backport = ["unicodedata2"] +[[package]] +name = "click" +version = "8.1.3" +description = "Composable command line interface toolkit" +category = "main" +optional = false +python-versions = ">=3.7" + +[package.dependencies] +colorama = {version = "*", markers = "platform_system == \"Windows\""} + [[package]] name = "colorama" version = "0.4.5" @@ -169,20 +180,6 @@ category = "main" optional = false python-versions = ">=3.7" -[[package]] -name = "gunicorn" -version = "20.1.0" -description = "WSGI HTTP Server for UNIX" -category = "main" -optional = false -python-versions = ">=3.5" - -[package.extras] -eventlet = ["eventlet (>=0.24.1)"] -gevent = ["gevent (>=1.4.0)"] -setproctitle = ["setproctitle"] -tornado = ["tornado (>=0.2)"] - [[package]] name = "h11" version = "0.12.0" @@ -411,6 +408,21 @@ brotli = ["brotlicffi (>=0.8.0)", "brotli (>=1.0.9)", "brotlipy (>=0.6.0)"] secure = ["pyOpenSSL (>=0.14)", "cryptography (>=1.3.4)", "idna (>=2.0.0)", "certifi", "ipaddress"] socks = ["PySocks (>=1.5.6,!=1.5.7,<2.0)"] +[[package]] +name = "uvicorn" +version = "0.18.2" +description = "The lightning-fast ASGI server." +category = "main" +optional = false +python-versions = ">=3.7" + +[package.dependencies] +click = ">=7.0" +h11 = ">=0.8" + +[package.extras] +standard = ["websockets (>=10.0)", "httptools (>=0.4.0)", "watchfiles (>=0.13)", "python-dotenv (>=0.13)", "PyYAML (>=5.1)", "uvloop (>=0.14.0,!=0.15.0,!=0.15.1)", "colorama (>=0.4)"] + [[package]] name = "wget" version = "3.2" @@ -456,7 +468,7 @@ multidict = ">=4.0" [metadata] lock-version = "1.1" python-versions = "^3.10" -content-hash = "5f84bbd6de3c471eb457b9b1d545f69e9bd79cfd08bba2c961498501e6d691aa" +content-hash = "727bcf87b1e26884a1a0eb7b1d43607bca90e3e9676e5b825e77bebc1e7f8f0d" [metadata.files] aiogram = [ @@ -632,6 +644,10 @@ cffi = [ {file = "cffi-1.15.1.tar.gz", hash = "sha256:d400bfb9a37b1351253cb402671cea7e89bdecc294e8016a707f6d1d8ac934f9"}, ] charset-normalizer = [] +click = [ + {file = "click-8.1.3-py3-none-any.whl", hash = "sha256:bb4d8133cb15a609f44e8213d9b391b0809795062913b383c62be0ee95b1db48"}, + {file = "click-8.1.3.tar.gz", hash = "sha256:7682dc8afb30297001674575ea00d1814d808d6a36af415a82bd481d37ba7b8e"}, +] colorama = [ {file = "colorama-0.4.5-py2.py3-none-any.whl", hash = "sha256:854bf444933e37f5824ae7bfc1e98d5bce2ebe4160d46b5edf346a89358e99da"}, {file = "colorama-0.4.5.tar.gz", hash = "sha256:e6c6b4334fc50988a639d9b98aa429a0b57da6e17b9a44f0451f930b6967b7a4"}, @@ -721,10 +737,6 @@ frozenlist = [ {file = "frozenlist-1.3.1-cp39-cp39-win_amd64.whl", hash = "sha256:625d8472c67f2d96f9a4302a947f92a7adbc1e20bedb6aff8dbc8ff039ca6189"}, {file = "frozenlist-1.3.1.tar.gz", hash = "sha256:3a735e4211a04ccfa3f4833547acdf5d2f863bfeb01cfd3edaffbc251f15cec8"}, ] -gunicorn = [ - {file = "gunicorn-20.1.0-py3-none-any.whl", hash = "sha256:9dcc4547dbb1cb284accfb15ab5667a0e5d1881cc443e0677b4882a4067a807e"}, - {file = "gunicorn-20.1.0.tar.gz", hash = "sha256:e0a968b5ba15f8a328fdfd7ab1fcb5af4470c28aaf7e55df02a99bc13138e6e8"}, -] h11 = [ {file = "h11-0.12.0-py3-none-any.whl", hash = "sha256:36a3cb8c0a032f56e2da7084577878a035d3b61d104230d4bd49c0c6b555a9c6"}, {file = "h11-0.12.0.tar.gz", hash = "sha256:47222cb6067e4a307d535814917cd98fd0a57b6788ce715755fa2b6c28b56042"}, @@ -858,6 +870,10 @@ urllib3 = [ {file = "urllib3-1.26.11-py2.py3-none-any.whl", hash = "sha256:c33ccba33c819596124764c23a97d25f32b28433ba0dedeb77d873a38722c9bc"}, {file = "urllib3-1.26.11.tar.gz", hash = "sha256:ea6e8fb210b19d950fab93b60c9009226c63a28808bc8386e05301e25883ac0a"}, ] +uvicorn = [ + {file = "uvicorn-0.18.2-py3-none-any.whl", hash = "sha256:c19a057deb1c5bb060946e2e5c262fc01590c6529c0af2c3d9ce941e89bc30e0"}, + {file = "uvicorn-0.18.2.tar.gz", hash = "sha256:cade07c403c397f9fe275492a48c1b869efd175d5d8a692df649e6e7e2ed8f4e"}, +] wget = [ {file = "wget-3.2.zip", hash = "sha256:35e630eca2aa50ce998b9b1a127bb26b30dfee573702782aa982f875e3f16061"}, ] diff --git a/pyproject.toml b/pyproject.toml index 9826ab2..34bc303 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -12,7 +12,7 @@ httpx = "^0.23.0" wget = "^3.2" loguru = "^0.6.0" python-decouple = "^3.6" -gunicorn = "^20.1" +uvicorn = "^0.18.2" [tool.poetry.dev-dependencies]