0

I have a Telegram bot built with Python and Program, which is deployed on an Azure Web App. Initially, the bot worked fine, but it stopped responding after some time. The Azure Web App logs indicate that the container failed to start within the expected time limit (WEBSITES_CONTAINER_START_TIME_LIMIT). The logs show that the Gunicorn server, running the Flask app, started successfully but encountered timeout errors, terminating the worker process after around 10 minutes. This cycle repeated until the Azure Web App timed out after 30 minutes, indicating that the app wasn't responding to HTTP requests within the configured time limit. Strangely, about 30 minutes after encountering these timeouts, the bot started working again without any changes to the code or configuration.

I added a startup command to the Azure Web App settings to ensure that Gunicorn is properly configured to run the Flask app hosting the Telegram bot. The command I used was:

gunicorn -b 0.0.0.0:8000 app:app

This command specifies the host and port for Gunicorn to listen on (0.0.0.0:8000) and the Flask app instance (app:app).

I also adjusted the environment variables in the Azure Web App settings, setting:

WEBSITES_CONTAINER_START_TIME_LIMIT=1800
WEBSITES_PORT=8080

I expected that by increasing the WEBSITES_CONTAINER_START_TIME_LIMIT value and specifying the WEBSITES_PORT, the bot would have enough time to start up and handle incoming requests without timing out. Additionally, by adding the Gunicorn startup command, I expected the bot to start and run reliably on the Azure Web App without encountering startup timeout errors.

After adding the startup command and environment variables, the logs showed:

2024-05-24T04:54:08.914Z INFO  - Waiting for response to warmup request for container telegram-bot-dignified-india_0_1a331a3c. Elapsed time = 1786.2027409 sec
2024-05-24T04:54:23.007Z ERROR - Container telegram-bot for site telegram-bot-dignified-india did not start within expected time limit. Elapsed time = 1800.2955472 sec
2024-05-24T04:54:23.013Z ERROR - Container telegram-bot didn't respond to HTTP pings on port: 8000, failing site start. See container logs for debugging.
2024-05-24T04:54:23.018Z INFO  - Stopping site telegram-bot-dignified-india because it failed during startup.

also wanted to mention that below log repeated multiple times at once.

2024-05-24T04:54:08.914Z INFO  - Waiting for response to warmup request for container telegram-bot. Elapsed time = 1786.2027409 sec
1
  • Refence Code :- from pyrogram import Client, filters app = Client("abc_bot", api_id=api_id, api_hash=api_hash, bot_token=bot_token) #Define a function to handle the /start command @app.on_message(filters.command("start")) def start_command(client, message): #Send "Hello" as a response to the /start command message.reply_text("Hello!") #Run the bot app.run() Commented May 24, 2024 at 5:26

1 Answer 1

0

Gunicorn is configured properly to handle incoming requests efficiently. You may need to tweak the number of worker processes and threads. By default, Gunicorn uses one worker process. Depending on the application’s requirements, you might need more.

  • Implement a lightweight health check endpoint in the Flask app by this app responds quickly to Azure's warmup requests.
@app.route('/health')
def health_check():
    return 'OK', 200

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=8080)

Check my full code for the reference. bot.py:

import os
from flask import Flask, request
from telegram import Update, Bot
from telegram.ext import Dispatcher, CommandHandler, MessageHandler, Filters

app = Flask(__name__)

# Telegram Bot Token from environment variable
TOKEN = os.getenv('TELEGRAM_BOT_TOKEN')

# Create Bot instance
bot = Bot(token=TOKEN)
dispatcher = Dispatcher(bot, None, use_context=True)

# Command handler for /start
def start(update, context):
    update.message.reply_text('Hello! This is your bot.')

dispatcher.add_handler(CommandHandler('start', start))

# Health check endpoint
@app.route('/health', methods=['GET'])
def health():
    return 'OK', 200

# Webhook endpoint to receive updates from Telegram
@app.route('/webhook', methods=['POST'])
def webhook():
    update = Update.de_json(request.get_json(force=True), bot)
    dispatcher.process_update(update)
    return 'OK', 200

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8080)

Dockerfile:

# Use an official Python runtime as a parent image
FROM python:3.9-slim

# Set the working directory
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Make port 8080 available to the world outside this container
EXPOSE 8080

# Run gunicorn.sh when the container launches
CMD ["./gunicorn.sh"]

Container Registry: enter image description here

Console Output:

2024-05-27T00:00:00.000Z INFO  - Starting container for site
2024-05-27T00:00:00.000Z INFO  - docker run -d -p 8080:8080 --name telegram-bot -e WEBSITE_SITE_NAME=telegram-bot -e WEBSITE_AUTH_ENABLED=False -e WEBSITE_ROLE_INSTANCE_ID=0 -e WEBSITE_HOSTNAME=telegram-bot.azurewebsites.net -e WEBSITE_INSTANCE_ID=0000000000000000000000000000000000000000000000000000 -e HTTP_LOGGING_ENABLED=1 <your-registry-name>.azurecr.io/telegram-bot:latest
2024-05-27T00:00:00.000Z INFO  - Pulling image from Docker hub: testregmay28.azurecr.io/telegram-bot:latest
2024-05-27T00:00:00.000Z INFO  - Successfully pulled image ## testregmay28.azurecr.io/telegram-bot:latest
2024-05-27T00:00:00.000Z INFO  - Starting container
[2024-05-27 00:00:00 +0000] [1] [INFO] Starting gunicorn 20.x.x
[2024-05-27 00:00:00 +0000] [1] [INFO] Listening at: http://0.0.0.0:8080 (1)
[2024-05-27 00:00:00 +0000] [1] [INFO] Using worker: sync
[2024-05-27 00:00:00 +0000] [10] [INFO] Booting worker with pid: 10
[2024-05-27 00:00:00 +0000] [11] [INFO] Booting worker with pid: 11
[2024-05-27 00:00:00 +0000] [12] [INFO] Booting worker with pid: 12
[2024-05-27 00:00:00 +0000] [13] [INFO] Booting worker with pid: 13
[2024-05-27 00:00:01,000] INFO in bot: Flask app initialized.

indicating that the app wasn't responding to HTTP requests within the configured time limit.

enter image description here

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

for me i am using pyrogram library and i tried the same thing but i think running both the client is conflicting so that the container failed during startup was not coming, the logs was showing that the container initialized successfully and is ready to take request but the bot wasnt working at all. and i tried changing the startup command to explicitly run the bot as "python bot.py" so the bot started working until in the logs it showed container failed during startup.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.