perf: avoid checking success tasks when save limit is disabled (#255)

- avoid extra queries when checking the count of success in db
This commit is contained in:
Anthony Hivert
2025-01-04 02:31:30 +01:00
committed by GitHub
parent 351bf66d71
commit ddc1aa2de1
3 changed files with 17 additions and 13 deletions
+4 -4
View File
@@ -2,16 +2,16 @@ dev:
docker compose -f web-docker-compose.yaml up docker compose -f web-docker-compose.yaml up
test: 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: 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: 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: 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: createsuperuser:
docker compose -f web-docker-compose.yaml run --rm web python manage.py createsuperuser docker compose -f web-docker-compose.yaml run --rm web python manage.py createsuperuser
+10 -7
View File
@@ -107,13 +107,16 @@ def save_task(task, broker: Broker):
value = get_func_repr(value) value = get_func_repr(value)
filters[Conf.SAVE_LIMIT_PER] = value filters[Conf.SAVE_LIMIT_PER] = value
with db.transaction.atomic(using=db.router.db_for_write(Success)): # check if we should clean the success tasks
list(Success.objects.filter(**filters).select_for_update()) if Conf.SAVE_LIMIT > 0:
if ( with db.transaction.atomic(using=db.router.db_for_write(Success)):
task["success"] success_tasks_qs = Success.objects.filter(**filters)
and 0 < Conf.SAVE_LIMIT <= Success.objects.filter(**filters).count() success_tasks_pks = [
): success_task.pk
Success.objects.filter(**filters).last().delete() 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 # check if this task has previous results
try: try:
+3 -2
View File
@@ -399,7 +399,7 @@ def test_timeout_task_finishes(broker, cluster_config_timeout, async_task_kwargs
@pytest.mark.django_db @pytest.mark.django_db
def test_recycle(broker, monkeypatch): def test_recycle(broker, monkeypatch, django_assert_num_queries):
# set up the Sentinel # set up the Sentinel
broker.list_key = "test_recycle_test:q" broker.list_key = "test_recycle_test:q"
async_task("django_q.tests.tasks.multiply", 2, 2, broker=broker) 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) monkeypatch.setattr(Conf, "SAVE_LIMIT", 1)
result_queue.put("STOP") result_queue.put("STOP")
# run monitor # run monitor
monitor(result_queue) with django_assert_num_queries(12):
monitor(result_queue)
assert Success.objects.count() == Conf.SAVE_LIMIT assert Success.objects.count() == Conf.SAVE_LIMIT
broker.delete_queue() broker.delete_queue()