diff --git a/remarkbox/scripts/send_node_digest_notifications.py b/remarkbox/scripts/send_node_digest_notifications.py index 7eb559b..f9bb88b 100644 --- a/remarkbox/scripts/send_node_digest_notifications.py +++ b/remarkbox/scripts/send_node_digest_notifications.py @@ -2,6 +2,7 @@ from pyramid.paster import bootstrap, setup_logging import transaction from ..lib.notify import deliver_scheduled_notifications +from ..models import get_tm_session from ..models.notification import NodeEventNotification from ..models.meta import now_timestamp @@ -21,36 +22,51 @@ def main(): with bootstrap(args.config) as env: request = env["request"] + # Check before with fresh session + with transaction.manager: + dbsession = get_tm_session( + request.registry["dbsession_factory"], transaction.manager + ) + before_count = dbsession.query(NodeEventNotification).filter( + NodeEventNotification.sent == False + ).count() + print(f"Unsent notifications before: {before_count}") + + # Let deliver_scheduled_notifications manage its own transaction + deliver_scheduled_notifications(request) + + # Check after and mark as sent with fresh session try: - with request.tm: - # Check before - dbsession = request.dbsession - before_count = dbsession.query(NodeEventNotification).filter( + with transaction.manager: + dbsession = get_tm_session( + request.registry["dbsession_factory"], transaction.manager + ) + + after_count = dbsession.query(NodeEventNotification).filter( NodeEventNotification.sent == False ).count() - print(f"Unsent notifications before: {before_count}") - - deliver_scheduled_notifications(request) - - # Check after deliver function - after_deliver_count = dbsession.query(NodeEventNotification).filter( - NodeEventNotification.sent == False - ).count() - print(f"Unsent notifications after deliver: {after_deliver_count}") - - # Mark remaining as sent - updated = dbsession.query(NodeEventNotification).filter( - NodeEventNotification.sent == False - ).update({'sent': True, 'updated_timestamp': now_timestamp()}) - print(f"Updated {updated} notifications to sent=True") - - # Final check - final_count = dbsession.query(NodeEventNotification).filter( - NodeEventNotification.sent == False - ).count() - print(f"Unsent notifications after update: {final_count}") + print(f"Unsent notifications after deliver: {after_count}") + if after_count > 0: + updated = dbsession.query(NodeEventNotification).filter( + NodeEventNotification.sent == False + ).update({ + 'sent': True, + 'updated_timestamp': now_timestamp() + }) + + print(f"Marked {updated} remaining notifications as sent") + else: + print("All notifications already marked as sent by deliver function") + + dbsession.flush() + transaction.commit() + + + except Exception as e: - print(f"Error occurred: {e}") - transaction.abort() - raise \ No newline at end of file + print(f"Error: {e}") + raise + +if __name__ == "__main__": + main() \ No newline at end of file