[FIX] bus: delay the postgres notification until the transaction is committed

This is much better than committing the current transaction without notice.
This commit is contained in:
Raphael Collet
2016-01-15 17:04:45 +01:00
parent 20cfdc4604
commit 18f3de2f19
+9 -9
View File
@@ -55,15 +55,15 @@ class ImBus(models.Model):
if random.random() < 0.01:
self.gc()
if channels:
# The notifications must be commited in database because when calling `NOTIFY imbus`, some pertinent
# threads will be awakened and will fetch the notification in the bus table, but since the transaction
# is not commited, there will be nothing to fetch, the longpolling will return empty list of notification.
# For some reason, this happen when `sendmany` is called more than once on the same request.
# `self._cr.commit()` is prevented in a test environement, to allow test rollback.
if not openerp.tools.config['test_enable']:
self._cr.commit()
with openerp.sql_db.db_connect('postgres').cursor() as cr2:
cr2.execute("notify imbus, %s", (json_dump(list(channels)),))
# We have to wait until the notifications are commited in database.
# When calling `NOTIFY imbus`, some concurrent threads will be
# awakened and will fetch the notification in the bus table. If the
# transaction is not commited yet, there will be nothing to fetch,
# and the longpolling will return no notification.
def notify():
with openerp.sql_db.db_connect('postgres').cursor() as cr:
cr.execute("notify imbus, %s", (json_dump(list(channels)),))
self._cr.after('commit', notify)
@api.model
def sendone(self, channel, message):