Update send_node_digest_notifications.py
This commit is contained in:
parent
032498a868
commit
2e6d88f191
1 changed files with 44 additions and 28 deletions
|
|
@ -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
|
||||
print(f"Error: {e}")
|
||||
raise
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue