Skip to content

Commit

Permalink
allow atomic on external db (Koed00#14)
Browse files Browse the repository at this point in the history
Co-authored-by: Rajeev KL <[email protected]>
  • Loading branch information
GDay and Rajeev KL authored Oct 15, 2022
1 parent 00fe611 commit ce98aaa
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions django_q/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,9 @@ def save_task(task, broker: Broker):
if Conf.SAVE_LIMIT_PER == "func":
value = get_func_repr(value)
filters[Conf.SAVE_LIMIT_PER] = value
with db.transaction.atomic():

database_to_use = {"using": Conf.ORM if Conf.ORM else Schedule.objects.db} if not Conf.HAS_REPLICA else {}
with db.transaction.atomic(**database_to_use):
last = Success.objects.filter(**filters).select_for_update().last()
if task["success"] and 0 < Conf.SAVE_LIMIT <= Success.objects.filter(**filters).count():
last.delete()
Expand Down Expand Up @@ -599,7 +601,7 @@ def scheduler(broker: Broker = None):
broker = get_broker()
close_old_django_connections()
try:
database_to_use = {"using": Conf.ORM} if not Conf.HAS_REPLICA else {}
database_to_use = {"using": Conf.ORM if Conf.ORM else Schedule.objects.db} if not Conf.HAS_REPLICA else {}
with db.transaction.atomic(**database_to_use):
for s in (
Schedule.objects.select_for_update()
Expand Down

0 comments on commit ce98aaa

Please sign in to comment.