You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

task_service.py 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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
  18. from api.db.db_models import DB
  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.kb_id, Document.location]) \
  73. .join(Document, on=(cls.model.doc_id == Document.id)) \
  74. .where(
  75. Document.status == StatusEnum.VALID.value,
  76. Document.run == TaskStatus.RUNNING.value,
  77. ~(Document.type == FileType.VIRTUAL.value),
  78. cls.model.progress >= 0,
  79. cls.model.progress < 1,
  80. cls.model.create_time >= current_timestamp() - 180000
  81. )
  82. docs = list(docs.dicts())
  83. if not docs: return []
  84. return list(set([(d["kb_id"], d["location"]) for d in docs]))
  85. @classmethod
  86. @DB.connection_context()
  87. def do_cancel(cls, id):
  88. try:
  89. task = cls.model.get_by_id(id)
  90. _, doc = DocumentService.get_by_id(task.doc_id)
  91. return doc.run == TaskStatus.CANCEL.value or doc.progress < 0
  92. except Exception as e:
  93. pass
  94. return True
  95. @classmethod
  96. @DB.connection_context()
  97. def update_progress(cls, id, info):
  98. with DB.lock("update_progress", -1):
  99. if info["progress_msg"]:
  100. cls.model.update(progress_msg=cls.model.progress_msg + "\n" + info["progress_msg"]).where(
  101. cls.model.id == id).execute()
  102. if "progress" in info:
  103. cls.model.update(progress=info["progress"]).where(
  104. cls.model.id == id).execute()