fix memory error, deleted user when sending newsletter (#1199)

This commit is contained in:
Son Nguyen Kim 2022-08-01 20:38:13 +02:00 committed by GitHub
parent a04152a37f
commit abe0e0fc46
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 3 deletions

View File

@ -810,13 +810,26 @@ def register_custom_commands(app):
)
# only send newsletter to those who haven't received it
user_query = (
User.order_by(User.id)
# not query users directly here as we can run out of memory
user_ids = (
Session.query(User.id)
.order_by(User.id)
.filter(User.id.notin_(user_received_newsletter))
.all()
)
for user in user_query:
# user_ids is an array of tuple (user_id,)
user_ids = [user_id[0] for user_id in user_ids]
for user_id in user_ids:
user = User.get(user_id)
# refetch newsletter
newsletter = Newsletter.get(newsletter_id)
if not user:
LOG.i(f"User {user_id} was maybe deleted in the meantime")
continue
to_email, unsubscribe_link, via_email = user.get_communication_email()
if not to_email:
continue