diff --git a/litellm/proxy/scheduled_tasks/store.py b/litellm/proxy/scheduled_tasks/store.py index e2078765f4c..31bad2a0ae9 100644 --- a/litellm/proxy/scheduled_tasks/store.py +++ b/litellm/proxy/scheduled_tasks/store.py @@ -174,21 +174,36 @@ async def report_task_result( return None if result == "success": - return await prisma_client.db.litellm_scheduledtasktable.update( - where={"task_id": task_id}, + affected = await prisma_client.db.litellm_scheduledtasktable.update_many( + where={"task_id": task_id, "owner_token": owner_token}, data={"consecutive_errors": 0, "last_error": None}, ) + if affected == 0: + return None + return await prisma_client.db.litellm_scheduledtasktable.find_first( + where={"task_id": task_id, "owner_token": owner_token}, + ) new_count = (existing.consecutive_errors or 0) + 1 - update: Dict[str, Any] = { + update_data: Dict[str, Any] = { "consecutive_errors": new_count, "last_error": reason, } - if new_count >= MAX_CONSECUTIVE_ERRORS and existing.status == "pending": - update["status"] = "failed" - return await prisma_client.db.litellm_scheduledtasktable.update( - where={"task_id": task_id}, - data=update, + where_clause: Dict[str, Any] = { + "task_id": task_id, + "owner_token": owner_token, + } + if new_count >= MAX_CONSECUTIVE_ERRORS: + update_data["status"] = "failed" + where_clause["status"] = "pending" + affected = await prisma_client.db.litellm_scheduledtasktable.update_many( + where=where_clause, + data=update_data, + ) + if affected == 0: + return None + return await prisma_client.db.litellm_scheduledtasktable.find_first( + where={"task_id": task_id, "owner_token": owner_token}, ) diff --git a/tests/test_litellm/proxy/scheduled_tasks/test_endpoints.py b/tests/test_litellm/proxy/scheduled_tasks/test_endpoints.py index d9e87e3cc74..4c21bd50ae7 100644 --- a/tests/test_litellm/proxy/scheduled_tasks/test_endpoints.py +++ b/tests/test_litellm/proxy/scheduled_tasks/test_endpoints.py @@ -205,7 +205,10 @@ class _FakeTx: continue if r.owner_token != owner_token: continue - if agent_id is not None and r.agent_id != agent_id: + if agent_id is None: + if r.agent_id is not None: + continue + elif r.agent_id != agent_id: continue if actions is not None and r.action not in actions: continue