Skip to content

Commit

Permalink
Fix: remove transaction wrap (#5)
Browse files Browse the repository at this point in the history
* Remove transaction wrapping tasks which prevents transactions from being used within the task

* Remove test

* Fix formatting

* unused import
  • Loading branch information
benhowes authored Dec 13, 2023
1 parent 575279e commit 14226f1
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 28 deletions.
7 changes: 1 addition & 6 deletions pgq/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
from dataclasses import dataclass
from typing import TYPE_CHECKING, Any, Callable, Dict, Optional, Type

from django.db import transaction

if TYPE_CHECKING:
from .models import BaseJob
from .queue import Queue
Expand Down Expand Up @@ -84,10 +82,7 @@ def inner(queue, job):

try:
args = copy.deepcopy(job.args)
with transaction.atomic():
result = fn(
queue, job, args["func_args"], JobMetaType(**args["meta"])
)
result = fn(queue, job, args["func_args"], JobMetaType(**args["meta"]))
except Exc as e:
retries = job.args["meta"].get("retries", 0)
if retries < max_retries:
Expand Down
22 changes: 0 additions & 22 deletions testproj/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,3 @@ def demotask(queue: Queue, job: Job, args: Any, meta: JobMeta) -> int:
demotask.enqueue({"count": 5})
self.assertIn("demotask", queue.tasks)
queue.run_once()

def test_atleastonce_retry_during_database_failure(self) -> None:
"""
Force a database error in the task. Check that it was retried.
"""

queue = AtLeastOnceQueue(tasks={})

@task(queue, max_retries=2)
def failuretask(queue: Queue, job: Job, args: Any, meta: JobMeta) -> None:
# group has max 150 chars for its name.
Group.objects.create(name="!" * 151)
return None

failuretask.enqueue({})
originaljob = Job.objects.all()[0]

queue.run_once()

retryjob = Job.objects.all()[0]
self.assertNotEqual(originaljob.id, retryjob.id)
self.assertEqual(retryjob.args["meta"]["retries"], 1)

0 comments on commit 14226f1

Please sign in to comment.