task.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. # Copyright 2021-2024 Avaiga Private Limited
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  4. # the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  9. # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  10. # specific language governing permissions and limitations under the License.
  11. from flask import request
  12. from flask_restful import Resource
  13. from taipy.config.config import Config
  14. from taipy.core import Task
  15. from taipy.core.exceptions.exceptions import NonExistingTask, NonExistingTaskConfig
  16. from taipy.core.task._task_manager_factory import _TaskManagerFactory
  17. from ...commons.to_from_model import _to_model
  18. from ..exceptions.exceptions import ConfigIdMissingException
  19. from ..middlewares._middleware import _middleware
  20. from ..schemas import TaskSchema
  21. def _get_or_raise(task_id: str) -> Task:
  22. manager = _TaskManagerFactory._build_manager()
  23. task = manager._get(task_id)
  24. if task is None:
  25. raise NonExistingTask(task_id)
  26. return task
  27. REPOSITORY = "task"
  28. class TaskResource(Resource):
  29. """Single object resource
  30. ---
  31. get:
  32. tags:
  33. - api
  34. summary: Get a task.
  35. description: |
  36. Return a single task by *task_id*. If the task does not exist, a 404 error is returned.
  37. !!! Note
  38. When the authorization feature is activated (available in the **Enterprise** edition only), this endpoint
  39. requires `TAIPY_READER` role.
  40. Code example:
  41. ```shell
  42. curl -X GET http://localhost:5000/api/v1/tasks/TASK_my_config_75750ed8-4e09-4e00-958d-e352ee426cc9
  43. ```
  44. parameters:
  45. - in: path
  46. name: task_id
  47. schema:
  48. type: string
  49. description: The identifier of the task.
  50. responses:
  51. 200:
  52. content:
  53. application/json:
  54. schema:
  55. type: object
  56. properties:
  57. task: TaskSchema
  58. 404:
  59. description: No task has the *task_id* identifier.
  60. delete:
  61. tags:
  62. - api
  63. summary: Delete a task.
  64. description: |
  65. Delete a task. If the task does not exist, a 404 error is returned.
  66. !!! Note
  67. When the authorization feature is activated (available in the **Enterprise** edition only), this endpoint
  68. requires `TAIPY_EDITOR` role.
  69. Code example:
  70. ```shell
  71. curl -X DELETE http://localhost:5000/api/v1/tasks/TASK_my_config_75750ed8-4e09-4e00-958d-e352ee426cc9
  72. ```
  73. parameters:
  74. - in: path
  75. name: task_id
  76. schema:
  77. type: string
  78. description: The identifier of the task.
  79. responses:
  80. 200:
  81. content:
  82. application/json:
  83. schema:
  84. type: object
  85. properties:
  86. message:
  87. type: string
  88. description: Status message.
  89. 404:
  90. description: No task has the *task_id* identifier.
  91. """
  92. def __init__(self, **kwargs):
  93. self.logger = kwargs.get("logger")
  94. @_middleware
  95. def get(self, task_id):
  96. schema = TaskSchema()
  97. task = _get_or_raise(task_id)
  98. return {"task": schema.dump(_to_model(REPOSITORY, task))}
  99. @_middleware
  100. def delete(self, task_id):
  101. manager = _TaskManagerFactory._build_manager()
  102. _get_or_raise(task_id)
  103. manager._delete(task_id)
  104. return {"message": f"Task {task_id} was deleted."}
  105. class TaskList(Resource):
  106. """Creation and get_all
  107. ---
  108. get:
  109. tags:
  110. - api
  111. summary: Get all tasks.
  112. description: |
  113. Return an array of all tasks.
  114. !!! Note
  115. When the authorization feature is activated (available in the **Enterprise** edition only), this endpoint
  116. requires `TAIPY_READER` role.
  117. Code example:
  118. ```shell
  119. curl -X GET http://localhost:5000/api/v1/tasks
  120. ```
  121. responses:
  122. 200:
  123. content:
  124. application/json:
  125. schema:
  126. allOf:
  127. - type: object
  128. properties:
  129. results:
  130. type: array
  131. items:
  132. $ref: '#/components/schemas/TaskSchema'
  133. post:
  134. tags:
  135. - api
  136. summary: Create a task.
  137. description: |
  138. Create a new task from its *config_id*. If the config does not exist, a 404 error is returned.
  139. !!! Note
  140. When the authorization feature is activated (available in the **Enterprise** edition only), this endpoint
  141. requires `TAIPY_EDITOR` role.
  142. Code example:
  143. ```shell
  144. curl -X POST http://localhost:5000/api/v1/tasks?config_id=my_task_config
  145. ```
  146. parameters:
  147. - in: query
  148. name: config_id
  149. schema:
  150. type: string
  151. description: The identifier of the task configuration.
  152. responses:
  153. 201:
  154. content:
  155. application/json:
  156. schema:
  157. type: object
  158. properties:
  159. message:
  160. type: string
  161. description: Status message.
  162. task: TaskSchema
  163. """
  164. def __init__(self, **kwargs):
  165. self.logger = kwargs.get("logger")
  166. def fetch_config(self, config_id):
  167. config = Config.tasks.get(config_id)
  168. if not config:
  169. raise NonExistingTaskConfig(config_id)
  170. return config
  171. @_middleware
  172. def get(self):
  173. schema = TaskSchema(many=True)
  174. manager = _TaskManagerFactory._build_manager()
  175. tasks = [_to_model(REPOSITORY, task) for task in manager._get_all()]
  176. return schema.dump(tasks)
  177. @_middleware
  178. def post(self):
  179. args = request.args
  180. config_id = args.get("config_id")
  181. schema = TaskSchema()
  182. manager = _TaskManagerFactory._build_manager()
  183. if not config_id:
  184. raise ConfigIdMissingException
  185. config = self.fetch_config(config_id)
  186. task = manager._bulk_get_or_create([config])[0]
  187. return {
  188. "message": "Task was created.",
  189. "task": schema.dump(_to_model(REPOSITORY, task)),
  190. }, 201
  191. class TaskExecutor(Resource):
  192. """Execute a task
  193. ---
  194. post:
  195. tags:
  196. - api
  197. summary: Execute a task.
  198. description: |
  199. Execute a task by *task_id*. If the task does not exist, a 404 error is returned.
  200. !!! Note
  201. When the authorization feature is activated (available in the **Enterprise** edition only), this endpoint
  202. requires `TAIPY_EXECUTOR` role.
  203. Code example:
  204. ```shell
  205. curl -X POST http://localhost:5000/api/v1/tasks/submit/TASK_my_config_75750ed8-4e09-4e00-958d-e352ee426cc9
  206. ```
  207. parameters:
  208. - in: path
  209. name: task_id
  210. schema:
  211. type: string
  212. responses:
  213. 204:
  214. content:
  215. application/json:
  216. schema:
  217. type: object
  218. properties:
  219. message:
  220. type: string
  221. description: Status message.
  222. task: TaskSchema
  223. 404:
  224. description: No task has the *task_id* identifier.
  225. """
  226. def __init__(self, **kwargs):
  227. self.logger = kwargs.get("logger")
  228. @_middleware
  229. def post(self, task_id):
  230. manager = _TaskManagerFactory._build_manager()
  231. task = _get_or_raise(task_id)
  232. manager._orchestrator().submit_task(task)
  233. return {"message": f"Task {task_id} was submitted."}