Reuse the transaction before getting messages

The message get helper for claims used its own connection and ran
outside the transaction where the 'claiming' happened. This caused the
get to fail at retrieving the messages because the query was being
executed 'before' the transaction.

This patch fixes the aforementioned issue and also ensures that records
ids are 'int' before calling `message_encode`.

Change-Id: I076ffc171adf02fdb15160b6c7b29646ee3fb280
Closes-bug: #1288820
This commit is contained in:
Flavio Percoco 2014-03-06 18:10:54 +01:00
parent 477bc46bfe
commit 364945f990
2 changed files with 7 additions and 8 deletions

View File

@ -25,7 +25,7 @@ from marconi.queues.storage.sqlalchemy import utils
class ClaimController(storage.Claim):
def __get(self, cid):
def __get(self, cid, trans):
# NOTE(flaper87): This probably needs to
# join on `Claim` to check the claim ttl.
sel = sa.sql.select([tables.Messages.c.id,
@ -39,11 +39,11 @@ class ClaimController(storage.Claim):
#utils.get_age(tables.Claims.c.created),
tables.Messages.c.cid == cid
))
records = self.driver.run(sel)
records = trans.execute(sel)
for id, body, ttl, created in records:
yield {
'id': utils.msgid_encode(id),
'id': utils.msgid_encode(int(id)),
'ttl': ttl,
'age': (timeutils.utcnow() - created).seconds,
'body': body,
@ -77,7 +77,7 @@ class ClaimController(storage.Claim):
{'id': claim_id,
'ttl': ttl,
'age': (timeutils.utcnow() - created).seconds},
self.__get(cid)
list(self.__get(cid, trans))
)
def create(self, queue, metadata, project=None,
@ -93,7 +93,6 @@ class ClaimController(storage.Claim):
return None, iter([])
# Clean up all expired claims in this queue
dlt = tables.Claims.delete().where(sa.and_(
tables.Claims.c.ttl <=
utils.get_age(tables.Claims.c.created),
@ -125,7 +124,7 @@ class ClaimController(storage.Claim):
tables.Messages.c.cid == cid)))
trans.execute(update)
return (utils.cid_encode(cid), self.__get(cid))
return (utils.cid_encode(int(cid)), list(self.__get(cid, trans)))
def update(self, queue, claim_id, metadata, project=None):
if project is None:

View File

@ -107,7 +107,7 @@ class MessageController(storage.Message):
records = self.driver.run(statement)
for id, body, ttl, created in records:
yield {
'id': utils.msgid_encode(id),
'id': utils.msgid_encode(int(id)),
'ttl': ttl,
'age': now - calendar.timegm(created.timetuple()),
'body': json.loads(body),
@ -144,7 +144,7 @@ class MessageController(storage.Message):
created_iso = timeutils.isotime(created)
return {
'id': utils.msgid_encode(id),
'id': utils.msgid_encode(int(id)),
'ttl': ttl,
'created': created_iso,
'age': int((timeutils.utcnow() - created).seconds),