flush the prints in a separate thread
This commit is contained in:
21
app.py
21
app.py
@@ -41,7 +41,7 @@ class WebPushService():
|
|||||||
- daily_notification_thread (threading.Thread): A thread to send daily notifications.
|
- daily_notification_thread (threading.Thread): A thread to send daily notifications.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Setting the application instance
|
# Setting the application instance
|
||||||
self.app = app
|
self.app = app
|
||||||
self.app.add_url_rule('/web-push/regenerate_vapid', view_func=self.regenerate_vapid, methods=['POST'])
|
self.app.add_url_rule('/web-push/regenerate_vapid', view_func=self.regenerate_vapid, methods=['POST'])
|
||||||
|
|
||||||
@@ -156,7 +156,7 @@ class WebPushService():
|
|||||||
except WebPushException as ex:
|
except WebPushException as ex:
|
||||||
now = datetime.datetime.now().isoformat()
|
now = datetime.datetime.now().isoformat()
|
||||||
endpoint = subscription_info['endpoint']
|
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'
|
unsubscribed_msg = '410 Gone'
|
||||||
if unsubscribed_msg in ex.args[0]:
|
if unsubscribed_msg in ex.args[0]:
|
||||||
@@ -166,11 +166,13 @@ class WebPushService():
|
|||||||
if subscription:
|
if subscription:
|
||||||
db.session.delete(subscription)
|
db.session.delete(subscription)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
print(f"Committed delete of {subscription_info}")
|
print(f"Committed delete of {subscription_info}", flush=True)
|
||||||
else:
|
else:
|
||||||
print(f"Could not find subscription at: {endpoint}")
|
print(f"Could not find subscription at: {endpoint}", flush=True)
|
||||||
else:
|
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:
|
def _send_daily_notifications(self) -> None:
|
||||||
@@ -193,7 +195,7 @@ class WebPushService():
|
|||||||
while True:
|
while True:
|
||||||
|
|
||||||
now = datetime.datetime.now().isoformat()
|
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
|
# Creating a context for the application to enable database operations
|
||||||
with self.app.app_context():
|
with self.app.app_context():
|
||||||
@@ -217,7 +219,7 @@ class WebPushService():
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
WebPushService._send_push_notification(subscription_info, message, vapid_key)
|
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
|
# Sleeping for 24 hours before sending the next set of notifications
|
||||||
time.sleep(24 * 60 * 60)
|
time.sleep(24 * 60 * 60)
|
||||||
@@ -387,11 +389,11 @@ class WebPushService():
|
|||||||
- If the subscription is not found in the database, an error message is returned with a 404 status code.
|
- If the subscription is not found in the database, an error message is returned with a 404 status code.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Retrieving the endpoint from the incoming request
|
# Retrieving the endpoint from the incoming request
|
||||||
content = request.json
|
content = request.json
|
||||||
endpoint = content['endpoint']
|
endpoint = content['endpoint']
|
||||||
|
|
||||||
# Searching for the subscription in the database using the endpoint
|
# Searching for the subscription in the database using the endpoint
|
||||||
subscription = Subscription.query.filter_by(endpoint=endpoint).first()
|
subscription = Subscription.query.filter_by(endpoint=endpoint).first()
|
||||||
|
|
||||||
# If the subscription is found, delete it from the database
|
# If the subscription is found, delete it from the database
|
||||||
@@ -406,4 +408,3 @@ class WebPushService():
|
|||||||
|
|
||||||
|
|
||||||
web_push_service = WebPushService(app, "app")
|
web_push_service = WebPushService(app, "app")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user