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: """查找或创建公司(加锁防并发重复)"""