Close sessions between loops to make sure we leave no lock (#2162)

* Close sessions between loops to make sure we leave no lock

* Close at the end

* Close before sleeps

* Use python generic empty list in case the events is an iterator
This commit is contained in:
Adrià Casajús 2024-07-24 16:49:55 +02:00 committed by GitHub
parent 666bf86441
commit 8262390bf0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -4,6 +4,8 @@ import psycopg2
import select
from abc import ABC, abstractmethod
from app.db import Session
from app.log import LOG
from app.models import SyncEvent
from app.events.event_dispatcher import NOTIFICATION_CHANNEL
@ -66,6 +68,7 @@ class PostgresEventSource(EventSource):
LOG.info(f"Could not find event with id={notify.payload}")
except Exception as e:
LOG.warn(f"Error getting event: {e}")
Session.close() # Ensure we get a new connection and we don't leave a dangling tx
def __connect(self):
self.__connection = psycopg2.connect(self.__connection_string)
@ -97,7 +100,8 @@ class DeadLetterEventSource(EventSource):
)
for event in events:
on_event(event)
else:
Session.close() # Ensure that we have a new connection and we don't have a dangling tx with a lock
if not events:
LOG.debug("No dead letter events")
sleep(_DEAD_LETTER_INTERVAL_SECONDS)
except Exception as e: