fetchmail: print unhandled exceptions, but don't crash

refs #1295
master
Alexander A. Klimov 5 years ago
parent b8b1699f9e
commit 313e98c1a2

@ -8,6 +8,7 @@ import subprocess
import re import re
import requests import requests
import sys import sys
import traceback
FETCHMAIL = """ FETCHMAIL = """
@ -45,47 +46,50 @@ def fetchmail(fetchmailrc):
def run(debug): def run(debug):
fetches = requests.get("http://admin/internal/fetch").json() try:
smtphost, smtpport = extract_host_port(os.environ.get("HOST_SMTP", "smtp"), None) fetches = requests.get("http://admin/internal/fetch").json()
if smtpport is None: smtphost, smtpport = extract_host_port(os.environ.get("HOST_SMTP", "smtp"), None)
smtphostport = smtphost if smtpport is None:
else: smtphostport = smtphost
smtphostport = "%s/%d" % (smtphost, smtpport) else:
for fetch in fetches: smtphostport = "%s/%d" % (smtphost, smtpport)
fetchmailrc = "" for fetch in fetches:
options = "options antispam 501, 504, 550, 553, 554" fetchmailrc = ""
options += " sslmode wrapped" if fetch["tls"] else "" options = "options antispam 501, 504, 550, 553, 554"
options += " keep" if fetch["keep"] else " fetchall" options += " sslmode wrapped" if fetch["tls"] else ""
fetchmailrc += RC_LINE.format( options += " keep" if fetch["keep"] else " fetchall"
user_email=escape_rc_string(fetch["user_email"]), fetchmailrc += RC_LINE.format(
protocol=fetch["protocol"], user_email=escape_rc_string(fetch["user_email"]),
host=escape_rc_string(fetch["host"]), protocol=fetch["protocol"],
port=fetch["port"], host=escape_rc_string(fetch["host"]),
smtphost=smtphostport, port=fetch["port"],
username=escape_rc_string(fetch["username"]), smtphost=smtphostport,
password=escape_rc_string(fetch["password"]), username=escape_rc_string(fetch["username"]),
options=options password=escape_rc_string(fetch["password"]),
) options=options
if debug:
print(fetchmailrc)
try:
print(fetchmail(fetchmailrc))
error_message = ""
except subprocess.CalledProcessError as error:
error_message = error.output.decode("utf8")
# No mail is not an error
if not error_message.startswith("fetchmail: No mail"):
print(error_message)
user_info = "for %s at %s" % (fetch["user_email"], fetch["host"])
# Number of messages seen is not a error as well
if ("messages" in error_message and
"(seen " in error_message and
user_info in error_message):
print(error_message)
finally:
requests.post("http://admin/internal/fetch/{}".format(fetch["id"]),
json=error_message.split("\n")[0]
) )
if debug:
print(fetchmailrc)
try:
print(fetchmail(fetchmailrc))
error_message = ""
except subprocess.CalledProcessError as error:
error_message = error.output.decode("utf8")
# No mail is not an error
if not error_message.startswith("fetchmail: No mail"):
print(error_message)
user_info = "for %s at %s" % (fetch["user_email"], fetch["host"])
# Number of messages seen is not a error as well
if ("messages" in error_message and
"(seen " in error_message and
user_info in error_message):
print(error_message)
finally:
requests.post("http://admin/internal/fetch/{}".format(fetch["id"]),
json=error_message.split("\n")[0]
)
except Exception:
traceback.print_exc()
if __name__ == "__main__": if __name__ == "__main__":

Loading…
Cancel
Save