diff --git a/Makefile b/Makefile index 0a99c8d..03cfe07 100644 --- a/Makefile +++ b/Makefile @@ -2,16 +2,16 @@ dev: docker compose -f web-docker-compose.yaml up test: - docker-compose -f test-services-docker-compose.yaml run --rm django-q2 poetry run pytest + docker compose -f test-services-docker-compose.yaml run --rm django-q2 poetry run pytest shell: - docker-compose -f test-services-docker-compose.yaml run --rm django-q2 poetry run python manage.py shell + docker compose -f test-services-docker-compose.yaml run --rm django-q2 poetry run python manage.py shell makemigrations: - docker-compose -f test-services-docker-compose.yaml run --rm django-q2 poetry run python manage.py makemigrations + docker compose -f test-services-docker-compose.yaml run --rm django-q2 poetry run python manage.py makemigrations migrate: - docker-compose -f test-services-docker-compose.yaml run --rm django-q2 poetry run python manage.py migrate + docker compose -f test-services-docker-compose.yaml run --rm django-q2 poetry run python manage.py migrate createsuperuser: docker compose -f web-docker-compose.yaml run --rm web python manage.py createsuperuser diff --git a/django_q/monitor.py b/django_q/monitor.py index b6b0b43..3f13098 100644 --- a/django_q/monitor.py +++ b/django_q/monitor.py @@ -107,13 +107,16 @@ def save_task(task, broker: Broker): value = get_func_repr(value) filters[Conf.SAVE_LIMIT_PER] = value - with db.transaction.atomic(using=db.router.db_for_write(Success)): - list(Success.objects.filter(**filters).select_for_update()) - if ( - task["success"] - and 0 < Conf.SAVE_LIMIT <= Success.objects.filter(**filters).count() - ): - Success.objects.filter(**filters).last().delete() + # check if we should clean the success tasks + if Conf.SAVE_LIMIT > 0: + with db.transaction.atomic(using=db.router.db_for_write(Success)): + success_tasks_qs = Success.objects.filter(**filters) + success_tasks_pks = [ + success_task.pk + for success_task in success_tasks_qs.select_for_update() + ] + if task["success"] and len(success_tasks_pks) >= Conf.SAVE_LIMIT: + success_tasks_qs.last().delete() # check if this task has previous results try: diff --git a/django_q/tests/test_cluster.py b/django_q/tests/test_cluster.py index 915407f..b61b0db 100644 --- a/django_q/tests/test_cluster.py +++ b/django_q/tests/test_cluster.py @@ -399,7 +399,7 @@ def test_timeout_task_finishes(broker, cluster_config_timeout, async_task_kwargs @pytest.mark.django_db -def test_recycle(broker, monkeypatch): +def test_recycle(broker, monkeypatch, django_assert_num_queries): # set up the Sentinel broker.list_key = "test_recycle_test:q" async_task("django_q.tests.tasks.multiply", 2, 2, broker=broker) @@ -432,7 +432,8 @@ def test_recycle(broker, monkeypatch): monkeypatch.setattr(Conf, "SAVE_LIMIT", 1) result_queue.put("STOP") # run monitor - monitor(result_queue) + with django_assert_num_queries(12): + monitor(result_queue) assert Success.objects.count() == Conf.SAVE_LIMIT broker.delete_queue()