Skip to content

Commit 379a8f3

Browse files
committed
New feature allowing to suppress repeating API or network related errors
1 parent d0d037c commit 379a8f3

File tree

1 file changed

+70
-11
lines changed

1 file changed

+70
-11
lines changed

lastfm_monitor.py

Lines changed: 70 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,16 @@
131131
# Value used by signal handlers increasing/decreasing the inactivity check (LASTFM_INACTIVITY_CHECK); in seconds
132132
LASTFM_INACTIVITY_CHECK_SIGNAL_VALUE = 30 # 30 seconds
133133

134+
# How many 50x errors need to show up in the defined time to display error message in the console - it is to suppress sporadic issues with Last.fm API endpoint; adjust the parameters according to the LASTFM_CHECK_INTERVAL & LASTFM_ACTIVE_CHECK_INTERVAL timers
135+
# If more than 15 Last.fm API related issues in 2 mins - we will show the error message
136+
ERROR_500_NUMBER_LIMIT = 15
137+
ERROR_500_TIME_LIMIT = 120 # 2 min
138+
139+
# How many network related errors need to show up in the defined time to display error message in the console - it is to suppress sporadic issues with internet connectivity; adjust the parameters according to the LASTFM_CHECK_INTERVAL & LASTFM_ACTIVE_CHECK_INTERVAL timers
140+
# If more than 15 network related issues in 2 mins - we will show the error message
141+
ERROR_NETWORK_ISSUES_NUMBER_LIMIT = 15
142+
ERROR_NETWORK_ISSUES_TIME_LIMIT = 120 # 2 min
143+
134144
# -------------------------
135145
# CONFIGURATION SECTION END
136146
# -------------------------
@@ -787,6 +797,10 @@ def lastfm_monitor_user(user, network, username, tracks, error_notification, csv
787797
sp_track_duration = 0
788798
duration_mark = ""
789799
pauses_number = 0
800+
error_500_counter = 0
801+
error_500_start_ts = 0
802+
error_network_issue_counter = 0
803+
error_network_issue_start_ts = 0
790804

791805
try:
792806
if csv_file_name:
@@ -1482,18 +1496,63 @@ def lastfm_monitor_user(user, network, username, tracks, error_notification, csv
14821496
if last_track_start_ts > 0:
14831497
last_track_start_ts_old2 = last_track_start_ts
14841498

1499+
ERROR_500_ZERO_TIME_LIMIT = ERROR_500_TIME_LIMIT + LASTFM_CHECK_INTERVAL
1500+
if LASTFM_CHECK_INTERVAL * ERROR_500_NUMBER_LIMIT > ERROR_500_ZERO_TIME_LIMIT:
1501+
ERROR_500_ZERO_TIME_LIMIT = LASTFM_CHECK_INTERVAL * (ERROR_500_NUMBER_LIMIT + 1)
1502+
1503+
if error_500_start_ts and ((int(time.time()) - error_500_start_ts) >= ERROR_500_ZERO_TIME_LIMIT):
1504+
error_500_start_ts = 0
1505+
error_500_counter = 0
1506+
1507+
ERROR_NETWORK_ZERO_TIME_LIMIT = ERROR_NETWORK_ISSUES_TIME_LIMIT + LASTFM_CHECK_INTERVAL
1508+
if LASTFM_CHECK_INTERVAL * ERROR_NETWORK_ISSUES_NUMBER_LIMIT > ERROR_NETWORK_ZERO_TIME_LIMIT:
1509+
ERROR_NETWORK_ZERO_TIME_LIMIT = LASTFM_CHECK_INTERVAL * (ERROR_NETWORK_ISSUES_NUMBER_LIMIT + 1)
1510+
1511+
if error_network_issue_start_ts and ((int(time.time()) - error_network_issue_start_ts) >= ERROR_NETWORK_ZERO_TIME_LIMIT):
1512+
error_network_issue_start_ts = 0
1513+
error_network_issue_counter = 0
1514+
14851515
except Exception as e:
1486-
print(f"Error - {e}")
1487-
if 'Invalid API key' in str(e) or 'API Key Suspended' in str(e):
1488-
print("* API key might not be valid anymore!")
1489-
if error_notification and not email_sent:
1490-
m_subject = f"lastfm_monitor: API key error! (user: {username})"
1491-
m_body = f"API key might not be valid anymore: {e}{get_cur_ts("\n\nTimestamp: ")}"
1492-
m_body_html = f"<html><head></head><body>API key might not be valid anymore: {escape(e)}{get_cur_ts("<br><br>Timestamp: ")}</body></html>"
1493-
print(f"Sending email notification to {RECEIVER_EMAIL}")
1494-
send_email(m_subject, m_body, m_body_html, SMTP_SSL)
1495-
email_sent = True
1496-
print_cur_ts("Timestamp:\t\t")
1516+
1517+
if 'HTTP code 500' in str(e) or 'HTTP code 504' in str(e) or 'HTTP code 503' in str(e) or 'HTTP code 502' in str(e):
1518+
if not error_500_start_ts:
1519+
error_500_start_ts = int(time.time())
1520+
error_500_counter = 1
1521+
else:
1522+
error_500_counter += 1
1523+
1524+
if 'timed out' in str(e) or 'name resolution' in str(e) or 'family not supported' in str(e) or str(e) == '':
1525+
if not error_network_issue_start_ts:
1526+
error_network_issue_start_ts = int(time.time())
1527+
error_network_issue_counter = 1
1528+
else:
1529+
error_network_issue_counter += 1
1530+
1531+
if error_500_start_ts and (error_500_counter >= ERROR_500_NUMBER_LIMIT and (int(time.time()) - error_500_start_ts) >= ERROR_500_TIME_LIMIT):
1532+
print(f"Error 50x ({error_500_counter}x times in the last {display_time((int(time.time()) - error_500_start_ts))}) - '{e}'")
1533+
print_cur_ts("Timestamp:\t\t")
1534+
error_500_start_ts = 0
1535+
error_500_counter = 0
1536+
1537+
elif error_network_issue_start_ts and (error_network_issue_counter >= ERROR_NETWORK_ISSUES_NUMBER_LIMIT and (int(time.time()) - error_network_issue_start_ts) >= ERROR_NETWORK_ISSUES_TIME_LIMIT):
1538+
print(f"Error with network ({error_network_issue_counter}x times in the last {display_time((int(time.time()) - error_network_issue_start_ts))}) - '{e}'")
1539+
print_cur_ts("Timestamp:\t\t")
1540+
error_network_issue_start_ts = 0
1541+
error_network_issue_counter = 0
1542+
1543+
elif not error_500_start_ts and not error_network_issue_start_ts:
1544+
print(f"Error - '{e}'")
1545+
1546+
if 'Invalid API key' in str(e) or 'API Key Suspended' in str(e):
1547+
print("* API key might not be valid anymore!")
1548+
if error_notification and not email_sent:
1549+
m_subject = f"lastfm_monitor: API key error! (user: {username})"
1550+
m_body = f"API key might not be valid anymore: {e}{get_cur_ts("\n\nTimestamp: ")}"
1551+
m_body_html = f"<html><head></head><body>API key might not be valid anymore: {escape(e)}{get_cur_ts("<br><br>Timestamp: ")}</body></html>"
1552+
print(f"Sending email notification to {RECEIVER_EMAIL}")
1553+
send_email(m_subject, m_body, m_body_html, SMTP_SSL)
1554+
email_sent = True
1555+
print_cur_ts("Timestamp:\t\t")
14971556

14981557
if lf_user_online:
14991558
time.sleep(LASTFM_ACTIVE_CHECK_INTERVAL)

0 commit comments

Comments
 (0)