From 04757a3f6ad26fd1de8ac0cb5550288ddb438a06 Mon Sep 17 00:00:00 2001 From: Finn Christiansen Date: Thu, 4 Jul 2024 21:55:10 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20reconnect=20after=20disconnect=20an?= =?UTF-8?q?d=20terminate=20on=20CTRL+C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- matrix_bot_invitation_mailer/__main__.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/matrix_bot_invitation_mailer/__main__.py b/matrix_bot_invitation_mailer/__main__.py index b59c4af..0f4471e 100644 --- a/matrix_bot_invitation_mailer/__main__.py +++ b/matrix_bot_invitation_mailer/__main__.py @@ -1,9 +1,12 @@ import simplematrixbotlib as botlib import requests import os +import sys import logging import smtplib import datetime +import time +from aiohttp.client_exceptions import ServerDisconnectedError from email.mime.text import MIMEText from dotenv import load_dotenv @@ -110,4 +113,12 @@ The link will be valid for 7 days.""".format(os.getenv("REGISTRATION_URL") + res server.quit() -bot.run() +while True: + try: + bot.run() + except ServerDisconnectedError: + time.sleep(5) + print("connection lost, reconnecting in 5 seconds...") + except KeyboardInterrupt: + print("exiting...") + sys.exit(0)