flush the prints in a separate thread
This commit is contained in:
15
app.py
15
app.py
@@ -156,7 +156,7 @@ class WebPushService():
|
||||
except WebPushException as ex:
|
||||
now = datetime.datetime.now().isoformat()
|
||||
endpoint = subscription_info['endpoint']
|
||||
print(f"{now}: Failed to send push notification for {endpoint} -- {ex}")
|
||||
print(f"{now}: Failed to send push notification for {endpoint} -- {ex}", flush=True)
|
||||
|
||||
unsubscribed_msg = '410 Gone'
|
||||
if unsubscribed_msg in ex.args[0]:
|
||||
@@ -166,11 +166,13 @@ class WebPushService():
|
||||
if subscription:
|
||||
db.session.delete(subscription)
|
||||
db.session.commit()
|
||||
print(f"Committed delete of {subscription_info}")
|
||||
print(f"Committed delete of {subscription_info}", flush=True)
|
||||
else:
|
||||
print(f"Could not find subscription at: {endpoint}")
|
||||
print(f"Could not find subscription at: {endpoint}", flush=True)
|
||||
else:
|
||||
print("Wrong error.", ex.args[0])
|
||||
print("Error other than unsubscribed/expired.", ex.args[0], flush=True)
|
||||
|
||||
return False
|
||||
|
||||
|
||||
def _send_daily_notifications(self) -> None:
|
||||
@@ -193,7 +195,7 @@ class WebPushService():
|
||||
while True:
|
||||
|
||||
now = datetime.datetime.now().isoformat()
|
||||
print(f"{now} - Starting to send subscriptions...")
|
||||
print(f"{now} - Starting to send subscriptions...", flush=True)
|
||||
|
||||
# Creating a context for the application to enable database operations
|
||||
with self.app.app_context():
|
||||
@@ -217,7 +219,7 @@ class WebPushService():
|
||||
}
|
||||
}
|
||||
WebPushService._send_push_notification(subscription_info, message, vapid_key)
|
||||
print(f"{now} - Finished sending {len(all_subscriptions)} subscriptions.")
|
||||
print(f"{now} - Finished sending {len(all_subscriptions)} subscriptions.", flush=True)
|
||||
|
||||
# Sleeping for 24 hours before sending the next set of notifications
|
||||
time.sleep(24 * 60 * 60)
|
||||
@@ -406,4 +408,3 @@ class WebPushService():
|
||||
|
||||
|
||||
web_push_service = WebPushService(app, "app")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user