fix(api): return cleanly on lock contention in clean_dataset_queries_task

Re-raise LockError after printing a skip message caused false task
failures for normal lock contention. Return instead to exit cleanly.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
echooffx 2026-05-07 14:52:37 +08:00
parent 34793e0d92
commit ca8a742853
2 changed files with 9 additions and 7 deletions

View File

@ -88,14 +88,17 @@ def clean_dataset_queries_task() -> None:
except LockError:
end_at = time.perf_counter()
logger.exception("clean_dataset_queries_task: acquire task lock failed, skip current execution")
logger.warning(
"clean_dataset_queries_task: lock already held, skip current execution"
)
click.echo(
click.style(
f"clean_dataset_queries_task: skipped (lock already held), latency: {end_at - start_at:.2f}s",
f"clean_dataset_queries_task: skipped (lock already held), "
f"latency: {end_at - start_at:.2f}s",
fg="yellow",
)
)
raise
return
except Exception:
end_at = time.perf_counter()
logger.exception("clean_dataset_queries_task failed")

View File

@ -58,8 +58,8 @@ class TestCleanDatasetQueriesTask:
@patch("schedule.clean_dataset_queries_task.db")
@patch("schedule.clean_dataset_queries_task.dify_config")
def test_lock_held_skips(self, mock_cfg, mock_db, mock_redis):
"""When the Redis lock is already held, the task raises LockError and
makes no database calls."""
"""When the Redis lock is already held, the task exits cleanly without
database calls or raising an error."""
mock_cfg.CLEAN_DATASET_QUERIES_RETENTION_DAYS = 60
mock_cfg.PLAN_SANDBOX_CLEAN_DAY_SETTING = 30
@ -71,8 +71,7 @@ class TestCleanDatasetQueriesTask:
session = MagicMock()
mock_db.session = session
with pytest.raises(LockError):
clean_dataset_queries_task()
clean_dataset_queries_task()
session.scalars.assert_not_called()
session.execute.assert_not_called()