Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

task_service.py 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #
  2. # Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import random
  17. from peewee import Expression, JOIN
  18. from api.db.db_models import DB, File2Document, File
  19. from api.db import StatusEnum, FileType, TaskStatus
  20. from api.db.db_models import Task, Document, Knowledgebase, Tenant
  21. from api.db.services.common_service import CommonService
  22. from api.db.services.document_service import DocumentService
  23. from api.utils import current_timestamp
  24. class TaskService(CommonService):
  25. model = Task
  26. @classmethod
  27. @DB.connection_context()
  28. def get_tasks(cls, tm, mod=0, comm=1, items_per_page=1, takeit=True):
  29. fields = [
  30. cls.model.id,
  31. cls.model.doc_id,
  32. cls.model.from_page,
  33. cls.model.to_page,
  34. Document.kb_id,
  35. Document.parser_id,
  36. Document.parser_config,
  37. Document.name,
  38. Document.type,
  39. Document.location,
  40. Document.size,
  41. Knowledgebase.tenant_id,
  42. Knowledgebase.language,
  43. Knowledgebase.embd_id,
  44. Tenant.img2txt_id,
  45. Tenant.asr_id,
  46. cls.model.update_time]
  47. with DB.lock("get_task", -1):
  48. docs = cls.model.select(*fields) \
  49. .join(Document, on=(cls.model.doc_id == Document.id)) \
  50. .join(Knowledgebase, on=(Document.kb_id == Knowledgebase.id)) \
  51. .join(Tenant, on=(Knowledgebase.tenant_id == Tenant.id))\
  52. .where(
  53. Document.status == StatusEnum.VALID.value,
  54. Document.run == TaskStatus.RUNNING.value,
  55. ~(Document.type == FileType.VIRTUAL.value),
  56. cls.model.progress == 0,
  57. #cls.model.update_time >= tm,
  58. #(Expression(cls.model.create_time, "%%", comm) == mod)
  59. )\
  60. .order_by(cls.model.update_time.asc())\
  61. .paginate(0, items_per_page)
  62. docs = list(docs.dicts())
  63. if not docs: return []
  64. if not takeit: return docs
  65. cls.model.update(progress_msg=cls.model.progress_msg + "\n" + "Task has been received.", progress=random.random()/10.).where(
  66. cls.model.id == docs[0]["id"]).execute()
  67. return docs
  68. @classmethod
  69. @DB.connection_context()
  70. def get_ongoing_doc_name(cls):
  71. with DB.lock("get_task", -1):
  72. docs = cls.model.select(*[Document.id, Document.kb_id, Document.location, File.parent_id]) \
  73. .join(Document, on=(cls.model.doc_id == Document.id)) \
  74. .join(File2Document, on=(File2Document.document_id == Document.id), join_type=JOIN.LEFT_OUTER) \
  75. .join(File, on=(File2Document.file_id == File.id)) \
  76. .where(
  77. Document.status == StatusEnum.VALID.value,
  78. Document.run == TaskStatus.RUNNING.value,
  79. ~(Document.type == FileType.VIRTUAL.value),
  80. cls.model.progress >= 0,
  81. cls.model.progress < 1,
  82. cls.model.create_time >= current_timestamp() - 180000
  83. )
  84. docs = list(docs.dicts())
  85. if not docs: return []
  86. return list(set([(d["parent_id"] if d["parent_id"] else d["kb_id"], d["location"]) for d in docs]))
  87. @classmethod
  88. @DB.connection_context()
  89. def do_cancel(cls, id):
  90. try:
  91. task = cls.model.get_by_id(id)
  92. _, doc = DocumentService.get_by_id(task.doc_id)
  93. return doc.run == TaskStatus.CANCEL.value or doc.progress < 0
  94. except Exception as e:
  95. pass
  96. return True
  97. @classmethod
  98. @DB.connection_context()
  99. def update_progress(cls, id, info):
  100. with DB.lock("update_progress", -1):
  101. if info["progress_msg"]:
  102. cls.model.update(progress_msg=cls.model.progress_msg + "\n" + info["progress_msg"]).where(
  103. cls.model.id == id).execute()
  104. if "progress" in info:
  105. cls.model.update(progress=info["progress"]).where(
  106. cls.model.id == id).execute()