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 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. from peewee import Expression
  17. from api.db.db_models import DB
  18. from api.db import StatusEnum, FileType, TaskStatus
  19. from api.db.db_models import Task, Document, Knowledgebase, Tenant
  20. from api.db.services.common_service import CommonService
  21. from api.db.services.document_service import DocumentService
  22. class TaskService(CommonService):
  23. model = Task
  24. @classmethod
  25. @DB.connection_context()
  26. def get_tasks(cls, tm, mod=0, comm=1, items_per_page=64):
  27. fields = [
  28. cls.model.id,
  29. cls.model.doc_id,
  30. cls.model.from_page,
  31. cls.model.to_page,
  32. Document.kb_id,
  33. Document.parser_id,
  34. Document.parser_config,
  35. Document.name,
  36. Document.type,
  37. Document.location,
  38. Document.size,
  39. Knowledgebase.tenant_id,
  40. Knowledgebase.language,
  41. Knowledgebase.embd_id,
  42. Tenant.img2txt_id,
  43. Tenant.asr_id,
  44. cls.model.update_time]
  45. docs = cls.model.select(*fields) \
  46. .join(Document, on=(cls.model.doc_id == Document.id)) \
  47. .join(Knowledgebase, on=(Document.kb_id == Knowledgebase.id)) \
  48. .join(Tenant, on=(Knowledgebase.tenant_id == Tenant.id))\
  49. .where(
  50. Document.status == StatusEnum.VALID.value,
  51. Document.run == TaskStatus.RUNNING.value,
  52. ~(Document.type == FileType.VIRTUAL.value),
  53. cls.model.progress == 0,
  54. cls.model.update_time >= tm,
  55. (Expression(cls.model.create_time, "%%", comm) == mod))\
  56. .order_by(cls.model.update_time.asc())\
  57. .paginate(1, items_per_page)
  58. return list(docs.dicts())
  59. @classmethod
  60. @DB.connection_context()
  61. def do_cancel(cls, id):
  62. try:
  63. task = cls.model.get_by_id(id)
  64. _, doc = DocumentService.get_by_id(task.doc_id)
  65. return doc.run == TaskStatus.CANCEL.value or doc.progress < 0
  66. except Exception as e:
  67. pass
  68. return True
  69. @classmethod
  70. @DB.connection_context()
  71. def update_progress(cls, id, info):
  72. if info["progress_msg"]:
  73. cls.model.update(progress_msg=cls.model.progress_msg + "\n" + info["progress_msg"]).where(
  74. cls.model.id == id).execute()
  75. if "progress" in info:
  76. cls.model.update(progress=info["progress"]).where(
  77. cls.model.id == id).execute()