旧验收项目自动补齐Cookie资源
This commit is contained in:
@@ -1640,6 +1640,40 @@ def _task_dict(conn, task) -> dict[str, Any]:
|
||||
FROM acceptance_task_resources WHERE task_id=? ORDER BY resource_order""",
|
||||
(task["id"],),
|
||||
).fetchall()
|
||||
has_custom_cookie = conn.execute(
|
||||
"""SELECT 1 FROM acceptance_task_secrets
|
||||
WHERE task_id=? AND secret_type='custom_cookie' LIMIT 1""",
|
||||
(task["id"],),
|
||||
).fetchone()
|
||||
if (not task_resources and not has_custom_cookie
|
||||
and task["scheduled_from_task_id"] is None
|
||||
and task["acceptance_method"] in {"data-analysis", "combined"}):
|
||||
try:
|
||||
current_resources = get_wen_resources()
|
||||
credential_ids = list(dict.fromkeys(
|
||||
str(item).strip() for item in current_resources.get("credential_ids", [])
|
||||
if str(item).strip()
|
||||
))
|
||||
conn.executemany(
|
||||
"""INSERT OR IGNORE INTO acceptance_task_resources
|
||||
(task_id,resource_type,resource_id,resource_order,created_at)
|
||||
VALUES (?,'cookie',?,?,?)""",
|
||||
[(task["id"], credential_id, index, now_iso())
|
||||
for index, credential_id in enumerate(credential_ids)],
|
||||
)
|
||||
if credential_ids:
|
||||
conn.execute(
|
||||
"UPDATE acceptance_tasks SET cookie_count=?,updated_at=? WHERE id=?",
|
||||
(len(credential_ids), now_iso(), task["id"]),
|
||||
)
|
||||
task_resources = conn.execute(
|
||||
"""SELECT resource_type,resource_id,resource_order
|
||||
FROM acceptance_task_resources WHERE task_id=? ORDER BY resource_order""",
|
||||
(task["id"],),
|
||||
).fetchall()
|
||||
except Exception:
|
||||
# 详情读取不能因为 Cookie 池短暂不可用而整体失败。
|
||||
pass
|
||||
cookie_attempts = conn.execute(
|
||||
"""SELECT credential_id,keyword,status,attempt_number,error_message,
|
||||
started_at,completed_at
|
||||
|
||||
@@ -80,6 +80,34 @@ class AcceptanceCreationTest(unittest.TestCase):
|
||||
self.assertEqual(task["schedule_start_time"], "20:00")
|
||||
self.assertIsNotNone(task["next_run_at"])
|
||||
self.assertEqual(task["keyword_runs"], [])
|
||||
self.assertEqual(
|
||||
[item["resource_id"] for item in task["resource_progress"] if item["resource_type"] == "cookie"],
|
||||
["c1", "c2"],
|
||||
)
|
||||
|
||||
@patch("acceptance_service.get_wen_resources")
|
||||
def test_legacy_root_task_backfills_current_cookie_pool(self, resources):
|
||||
resources.return_value = {"cookie_count": 2, "device_count": 3, "credential_ids": ["pool-1", "pool-2"]}
|
||||
now = now_iso()
|
||||
with connection() as conn:
|
||||
task_id = conn.execute(
|
||||
"""INSERT INTO acceptance_tasks
|
||||
(public_task_id,name,acceptance_method,status,progress_percent,device_count,
|
||||
created_by_id,created_by_name,created_at,updated_at,cookie_count)
|
||||
VALUES ('legacy-root','旧项目','combined','completed',100,3,'u','u',?,?,2)""",
|
||||
(now, now),
|
||||
).lastrowid
|
||||
conn.execute(
|
||||
"INSERT INTO acceptance_keywords(task_id,keyword_order,keyword,created_at) VALUES (?,0,'山姆会员卡',?)",
|
||||
(task_id, now),
|
||||
)
|
||||
|
||||
task = get_acceptance_task("legacy-root")
|
||||
|
||||
self.assertEqual(
|
||||
[item["resource_id"] for item in task["resource_progress"] if item["resource_type"] == "cookie"],
|
||||
["pool-1", "pool-2"],
|
||||
)
|
||||
|
||||
@patch("acceptance_service.threading.Thread", _ThreadWithoutStart)
|
||||
@patch("acceptance_service.sync_dashboard_acceptance_project")
|
||||
|
||||
Reference in New Issue
Block a user