refactor: update locking mechanism to use a consistent key format and set default timeout

This commit is contained in:
CaptainB 2025-07-02 12:36:43 +08:00
parent 0ad38e2540
commit 6068530cdd
2 changed files with 5 additions and 3 deletions

View File

@ -253,7 +253,7 @@ class ListenerManagement:
"""
if state_list is None:
state_list = [State.PENDING, State.SUCCESS, State.FAILURE, State.REVOKE, State.REVOKED]
if not try_lock('embedding' + str(document_id)):
if not try_lock('embedding:' + str(document_id)):
return
try:
def is_the_task_interrupted():
@ -290,7 +290,7 @@ class ListenerManagement:
ListenerManagement.post_update_document_status(document_id, TaskType.EMBEDDING)
ListenerManagement.get_aggregation_document_status(document_id)()
maxkb_logger.info(_('End--->Embedding document: {document_id}').format(document_id=document_id))
un_lock('embedding' + str(document_id))
un_lock('embedding:' + str(document_id))
@staticmethod
def embedding_by_knowledge(knowledge_id, embedding_model: Embeddings):

View File

@ -20,7 +20,9 @@ def try_lock(key: str, timeout=None):
:param timeout 超时时间
:return: 是否获取到锁
"""
return memory_cache.add(key, 'lock', timeout=timedelta(hours=1).total_seconds() if timeout is not None else timeout)
if timeout is None:
timeout = 3600 # 默认超时时间为3600秒
return memory_cache.add(key, 'lock', timeout=timeout)
def un_lock(key: str):