Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions fastasyncpg/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -736,10 +736,14 @@ async def claim_one(self:Table, status_col='status', pending='pending', complete
tbl = txn.t[self.name]
p.db = txn
p.evt = await tbl.claim(where=f'"{status_col}"=$1', where_args=[pending], order_by=order_by)
try: yield p
try:
async with txn.conn.transaction(): yield p # return a nested tx so any tx abort does not prevent us from marking the row as failed
except Exception as e: p.failed, p.exc, p.tb = True, e, traceback.format_exc()
if p.evt is None: return
pk = self.pks[0]
new = failed if p.failed else (None if p.retry else completed)
stmt = f'UPDATE {self} SET "{status_col}"=$1 WHERE "{pk}"=$2'
if new: await txn.execute(stmt, new, get_field(p.evt, pk))
if new:
await txn.execute(stmt, new, get_field(p.evt, pk))
p.evt = await tbl.selectone(f'"{pk}"=$1', [get_field(p.evt, pk)])

32 changes: 30 additions & 2 deletions nbs/00_core.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -5093,13 +5093,16 @@
" tbl = txn.t[self.name]\n",
" p.db = txn\n",
" p.evt = await tbl.claim(where=f'\"{status_col}\"=$1', where_args=[pending], order_by=order_by)\n",
" try: yield p\n",
" try:\n",
" async with txn.conn.transaction(): yield p # return a nested tx so any tx abort does not prevent us from marking the row as failed\n",
" except Exception as e: p.failed, p.exc, p.tb = True, e, traceback.format_exc()\n",
" if p.evt is None: return\n",
" pk = self.pks[0]\n",
" new = failed if p.failed else (None if p.retry else completed)\n",
" stmt = f'UPDATE {self} SET \"{status_col}\"=$1 WHERE \"{pk}\"=$2'\n",
" if new: await txn.execute(stmt, new, get_field(p.evt, pk))"
" if new: \n",
" await txn.execute(stmt, new, get_field(p.evt, pk))\n",
" p.evt = await tbl.selectone(f'\"{pk}\"=$1', [get_field(p.evt, pk)])\n"
]
},
{
Expand Down Expand Up @@ -5293,6 +5296,31 @@
"await jobs(\"payload=$1\", [pl])"
]
},
{
"cell_type": "markdown",
"id": "a866bdaa",
"metadata": {},
"source": [
"Test that when the transaction is aborted the claimed event row is still set to failed."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e1e45f08",
"metadata": {},
"outputs": [],
"source": [
"await jobs.delete_where()\n",
"await jobs.inserts([Job(payload='repro', status='pending')])\n",
"\n",
"async with jobs.claim_one(order_by='id') as p:\n",
" await p.db.execute('select definitely_not_a_real_function()')\n",
"\n",
"test_eq(p.evt.status, 'failed')\n",
"test_eq(p.failed, True)"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down