From 85278ea96a0196c2af6669a41a623033563bc1db Mon Sep 17 00:00:00 2001 From: zk Date: Tue, 2 Jun 2026 19:20:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=84=E7=90=86=E5=8F=AF=E8=83=BD=E5=87=BA?= =?UTF-8?q?=E7=8E=B0=E7=9A=84=E9=94=81=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/services/job_clean_service.py | 47 ++++++++++++++++++------------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/app/services/job_clean_service.py b/app/services/job_clean_service.py index 4c872e2..db6b95d 100644 --- a/app/services/job_clean_service.py +++ b/app/services/job_clean_service.py @@ -228,35 +228,42 @@ async def _extract_skill_tags(job_id: int, result: dict) -> None: now = datetime.now() tag_ids = [] - async with MysqlSession() as mysql: - for name in skills: - name = str(name).strip().lower() - if not name or len(name) > 50: - continue + for name in skills: + name = str(name).strip().lower() + if not name or len(name) > 50: + continue - tag_id = next(_id_gen) - # INSERT IGNORE - await mysql.execute( - text("INSERT IGNORE INTO bg_skill_tag (id, name) VALUES (:id, :name)"), - {"id": tag_id, "name": name}, - ) - # 查回真实ID - row = await mysql.execute( - text("SELECT id FROM bg_skill_tag WHERE name = :name LIMIT 1"), - {"name": name}, - ) - real_id = row.scalar() - if real_id and real_id not in tag_ids: - tag_ids.append(real_id) + # 每个 tag 单独 session,避免死锁 + real_id = await _find_or_create_skill_tag(name) + if real_id and real_id not in tag_ids: + tag_ids.append(real_id) - if tag_ids: + if tag_ids: + async with MysqlSession() as mysql: await mysql.execute( insert(JobSkillTagRelation), [{"id": next(_id_gen), "job_id": job_id, "skill_tag_id": tid, "create_time": now} for tid in tag_ids], ) + await mysql.commit() + +async def _find_or_create_skill_tag(name: str) -> int | None: + """查找或创建技能标签(单独事务,避免死锁)""" + async with MysqlSession() as mysql: + tag_id = next(_id_gen) + await mysql.execute( + text("INSERT IGNORE INTO bg_skill_tag (id, name) VALUES (:id, :name)"), + {"id": tag_id, "name": name}, + ) await mysql.commit() + async with MysqlSession() as mysql: + row = await mysql.execute( + text("SELECT id FROM bg_skill_tag WHERE name = :name LIMIT 1"), + {"name": name}, + ) + return row.scalar() + async def _find_or_create_company(short_name: str) -> int: """查找或创建公司(加锁防并发重复)"""