Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

task_service.py 3.6KB

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