import fcntl import hashlib import os from tg_mainbot import WEBHOOK_SECRET, WEBHOOK_URL, app, setup_webhook def _ensure_webhook(): """ Один раз на старте gunicorn регистрируем webhook с secret_token. Блокирующий flock — без гонки 4 воркеров и без 429. """ digest = hashlib.sha256( f"{WEBHOOK_URL}|{WEBHOOK_SECRET or ''}|allowed_v2".encode("utf-8") ).hexdigest()[:16] marker_path = f"/tmp/zilant_webhook_ok_{digest}" lock_path = "/tmp/zilant_webhook_setup.lock" with open(lock_path, "w") as lock_file: fcntl.flock(lock_file.fileno(), fcntl.LOCK_EX) if os.path.exists(marker_path): return setup_webhook() with open(marker_path, "w") as marker: marker.write("ok\n") _ensure_webhook() if __name__ == "__main__": setup_webhook() app.run()