@webiny/tasks 0.0.0-unstable.06b2ede40f

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (171) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +10 -0
  3. package/context.d.ts +2 -0
  4. package/context.js +33 -0
  5. package/context.js.map +1 -0
  6. package/crud/crud.tasks.d.ts +2 -0
  7. package/crud/crud.tasks.js +273 -0
  8. package/crud/crud.tasks.js.map +1 -0
  9. package/crud/definition.tasks.d.ts +2 -0
  10. package/crud/definition.tasks.js +29 -0
  11. package/crud/definition.tasks.js.map +1 -0
  12. package/crud/model.d.ts +5 -0
  13. package/crud/model.js +216 -0
  14. package/crud/model.js.map +1 -0
  15. package/crud/service.tasks.d.ts +2 -0
  16. package/crud/service.tasks.js +171 -0
  17. package/crud/service.tasks.js.map +1 -0
  18. package/crud/where.d.ts +1 -0
  19. package/crud/where.js +33 -0
  20. package/crud/where.js.map +1 -0
  21. package/graphql/checkPermissions.d.ts +7 -0
  22. package/graphql/checkPermissions.js +46 -0
  23. package/graphql/checkPermissions.js.map +1 -0
  24. package/graphql/index.d.ts +2 -0
  25. package/graphql/index.js +351 -0
  26. package/graphql/index.js.map +1 -0
  27. package/graphql/utils.d.ts +9 -0
  28. package/graphql/utils.js +28 -0
  29. package/graphql/utils.js.map +1 -0
  30. package/handler/index.d.ts +6 -0
  31. package/handler/index.js +69 -0
  32. package/handler/index.js.map +1 -0
  33. package/handler/register.d.ts +1 -0
  34. package/handler/register.js +25 -0
  35. package/handler/register.js.map +1 -0
  36. package/handler/types.d.ts +25 -0
  37. package/handler/types.js +7 -0
  38. package/handler/types.js.map +1 -0
  39. package/index.d.ts +6 -0
  40. package/index.js +62 -0
  41. package/index.js.map +1 -0
  42. package/package.json +50 -0
  43. package/plugins/TaskServicePlugin.d.ts +20 -0
  44. package/plugins/TaskServicePlugin.js +17 -0
  45. package/plugins/TaskServicePlugin.js.map +1 -0
  46. package/plugins/index.d.ts +1 -0
  47. package/plugins/index.js +18 -0
  48. package/plugins/index.js.map +1 -0
  49. package/response/DatabaseResponse.d.ts +12 -0
  50. package/response/DatabaseResponse.js +152 -0
  51. package/response/DatabaseResponse.js.map +1 -0
  52. package/response/Response.d.ts +13 -0
  53. package/response/Response.js +120 -0
  54. package/response/Response.js.map +1 -0
  55. package/response/ResponseAbortedResult.d.ts +10 -0
  56. package/response/ResponseAbortedResult.js +19 -0
  57. package/response/ResponseAbortedResult.js.map +1 -0
  58. package/response/ResponseContinueResult.d.ts +18 -0
  59. package/response/ResponseContinueResult.js +26 -0
  60. package/response/ResponseContinueResult.js.map +1 -0
  61. package/response/ResponseDoneResult.d.ts +13 -0
  62. package/response/ResponseDoneResult.js +21 -0
  63. package/response/ResponseDoneResult.js.map +1 -0
  64. package/response/ResponseErrorResult.d.ts +11 -0
  65. package/response/ResponseErrorResult.js +20 -0
  66. package/response/ResponseErrorResult.js.map +1 -0
  67. package/response/TaskResponse.d.ts +11 -0
  68. package/response/TaskResponse.js +80 -0
  69. package/response/TaskResponse.js.map +1 -0
  70. package/response/abstractions/Response.d.ts +25 -0
  71. package/response/abstractions/Response.js +7 -0
  72. package/response/abstractions/Response.js.map +1 -0
  73. package/response/abstractions/ResponseAbortedResult.d.ts +5 -0
  74. package/response/abstractions/ResponseAbortedResult.js +7 -0
  75. package/response/abstractions/ResponseAbortedResult.js.map +1 -0
  76. package/response/abstractions/ResponseBaseResult.d.ts +8 -0
  77. package/response/abstractions/ResponseBaseResult.js +7 -0
  78. package/response/abstractions/ResponseBaseResult.js.map +1 -0
  79. package/response/abstractions/ResponseContinueResult.d.ts +21 -0
  80. package/response/abstractions/ResponseContinueResult.js +7 -0
  81. package/response/abstractions/ResponseContinueResult.js.map +1 -0
  82. package/response/abstractions/ResponseDoneResult.d.ts +14 -0
  83. package/response/abstractions/ResponseDoneResult.js +7 -0
  84. package/response/abstractions/ResponseDoneResult.js.map +1 -0
  85. package/response/abstractions/ResponseErrorResult.d.ts +19 -0
  86. package/response/abstractions/ResponseErrorResult.js +7 -0
  87. package/response/abstractions/ResponseErrorResult.js.map +1 -0
  88. package/response/abstractions/TaskResponse.d.ts +38 -0
  89. package/response/abstractions/TaskResponse.js +7 -0
  90. package/response/abstractions/TaskResponse.js.map +1 -0
  91. package/response/abstractions/index.d.ts +7 -0
  92. package/response/abstractions/index.js +84 -0
  93. package/response/abstractions/index.js.map +1 -0
  94. package/response/index.d.ts +7 -0
  95. package/response/index.js +84 -0
  96. package/response/index.js.map +1 -0
  97. package/runner/TaskControl.d.ts +14 -0
  98. package/runner/TaskControl.js +222 -0
  99. package/runner/TaskControl.js.map +1 -0
  100. package/runner/TaskEventValidation.d.ts +5 -0
  101. package/runner/TaskEventValidation.js +30 -0
  102. package/runner/TaskEventValidation.js.map +1 -0
  103. package/runner/TaskManager.d.ts +14 -0
  104. package/runner/TaskManager.js +139 -0
  105. package/runner/TaskManager.js.map +1 -0
  106. package/runner/TaskManagerStore.d.ts +40 -0
  107. package/runner/TaskManagerStore.js +167 -0
  108. package/runner/TaskManagerStore.js.map +1 -0
  109. package/runner/TaskRunner.d.ts +26 -0
  110. package/runner/TaskRunner.js +97 -0
  111. package/runner/TaskRunner.js.map +1 -0
  112. package/runner/abstractions/TaskControl.d.ts +10 -0
  113. package/runner/abstractions/TaskControl.js +7 -0
  114. package/runner/abstractions/TaskControl.js.map +1 -0
  115. package/runner/abstractions/TaskEventValidation.d.ts +5 -0
  116. package/runner/abstractions/TaskEventValidation.js +7 -0
  117. package/runner/abstractions/TaskEventValidation.js.map +1 -0
  118. package/runner/abstractions/TaskManager.d.ts +5 -0
  119. package/runner/abstractions/TaskManager.js +7 -0
  120. package/runner/abstractions/TaskManager.js.map +1 -0
  121. package/runner/abstractions/TaskManagerStore.d.ts +92 -0
  122. package/runner/abstractions/TaskManagerStore.js +7 -0
  123. package/runner/abstractions/TaskManagerStore.js.map +1 -0
  124. package/runner/abstractions/TaskRunner.d.ts +13 -0
  125. package/runner/abstractions/TaskRunner.js +7 -0
  126. package/runner/abstractions/TaskRunner.js.map +1 -0
  127. package/runner/abstractions/index.d.ts +5 -0
  128. package/runner/abstractions/index.js +62 -0
  129. package/runner/abstractions/index.js.map +1 -0
  130. package/runner/index.d.ts +1 -0
  131. package/runner/index.js +18 -0
  132. package/runner/index.js.map +1 -0
  133. package/service/EventBridgeEventTransportPlugin.d.ts +17 -0
  134. package/service/EventBridgeEventTransportPlugin.js +60 -0
  135. package/service/EventBridgeEventTransportPlugin.js.map +1 -0
  136. package/service/StepFunctionServicePlugin.d.ts +26 -0
  137. package/service/StepFunctionServicePlugin.js +88 -0
  138. package/service/StepFunctionServicePlugin.js.map +1 -0
  139. package/service/createService.d.ts +6 -0
  140. package/service/createService.js +30 -0
  141. package/service/createService.js.map +1 -0
  142. package/service/index.d.ts +4 -0
  143. package/service/index.js +31 -0
  144. package/service/index.js.map +1 -0
  145. package/task/index.d.ts +2 -0
  146. package/task/index.js +29 -0
  147. package/task/index.js.map +1 -0
  148. package/task/input.d.ts +15 -0
  149. package/task/input.js +21 -0
  150. package/task/input.js.map +1 -0
  151. package/task/plugin.d.ts +36 -0
  152. package/task/plugin.js +106 -0
  153. package/task/plugin.js.map +1 -0
  154. package/tasks/testingRunTask.d.ts +1 -0
  155. package/tasks/testingRunTask.js +21 -0
  156. package/tasks/testingRunTask.js.map +1 -0
  157. package/types.d.ts +340 -0
  158. package/types.js +69 -0
  159. package/types.js.map +1 -0
  160. package/utils/ObjectUpdater.d.ts +8 -0
  161. package/utils/ObjectUpdater.js +46 -0
  162. package/utils/ObjectUpdater.js.map +1 -0
  163. package/utils/getErrorProperties.d.ts +2 -0
  164. package/utils/getErrorProperties.js +15 -0
  165. package/utils/getErrorProperties.js.map +1 -0
  166. package/utils/getObjectProperties.d.ts +8 -0
  167. package/utils/getObjectProperties.js +25 -0
  168. package/utils/getObjectProperties.js.map +1 -0
  169. package/utils/index.d.ts +3 -0
  170. package/utils/index.js +40 -0
  171. package/utils/index.js.map +1 -0
@@ -0,0 +1,222 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.TaskControl = void 0;
7
+ var _types = require("../types");
8
+ var _TaskManager = require("./TaskManager");
9
+ var _response = require("../response");
10
+ var _TaskManagerStore = require("./TaskManagerStore");
11
+ var _handlerGraphql = require("@webiny/handler-graphql");
12
+ var _getErrorProperties = require("../utils/getErrorProperties");
13
+ class TaskControl {
14
+ constructor(runner, response, context) {
15
+ this.runner = runner;
16
+ this.context = context;
17
+ this.response = response;
18
+ }
19
+ async run(event) {
20
+ const taskId = event.webinyTaskId;
21
+ /**
22
+ * This is the initial getTask idea.
23
+ * We will need to take care of child tasks:
24
+ * * child tasks can be in multiple levels (child task creates a child task, etc...).
25
+ * * child tasks could be executed in parallel.
26
+ */
27
+ let task;
28
+ try {
29
+ task = await this.getTask(taskId);
30
+ this.context.security.setIdentity(task.createdBy);
31
+ } catch (error) {
32
+ /**
33
+ * TODO Refactor error handling.
34
+ */
35
+ // @ts-expect-error
36
+ return this.response.error({
37
+ ...(0, _getErrorProperties.getErrorProperties)(error)
38
+ });
39
+ }
40
+ /**
41
+ * Let's get the task definition.
42
+ */
43
+ const definition = this.context.tasks.getDefinition(task.definitionId);
44
+ if (!definition) {
45
+ return this.response.error({
46
+ error: {
47
+ message: `Task "${task.id}" cannot be executed because there is no "${task.definitionId}" definition plugin.`,
48
+ code: "TASK_DEFINITION_ERROR",
49
+ data: {
50
+ definitionId: task.definitionId
51
+ }
52
+ }
53
+ });
54
+ }
55
+ const disableDatabaseLogs = !!definition.disableDatabaseLogs;
56
+
57
+ /**
58
+ * As this as a run of the task, we need to create a new log entry.
59
+ */
60
+ let taskLog;
61
+ try {
62
+ taskLog = await this.getTaskLog(task, disableDatabaseLogs);
63
+ } catch (error) {
64
+ return this.response.error({
65
+ error
66
+ });
67
+ }
68
+ /**
69
+ * Make sure that task does not run if it is aborted.
70
+ * This will effectively end the Step Function execution with a "success" status.
71
+ */
72
+ if (task.taskStatus === _types.TaskDataStatus.ABORTED) {
73
+ return this.response.aborted();
74
+ }
75
+ /**
76
+ * Do not run if already a success (done).
77
+ */
78
+ //
79
+ else if (task.taskStatus === _types.TaskDataStatus.SUCCESS) {
80
+ return this.response.error({
81
+ error: {
82
+ message: "Task is already done, cannot run it again."
83
+ }
84
+ });
85
+ }
86
+ /**
87
+ * Do not run if already failed.
88
+ */
89
+ //
90
+ else if (task.taskStatus === _types.TaskDataStatus.FAILED) {
91
+ return this.response.error({
92
+ error: {
93
+ message: "Task has failed, cannot run it again."
94
+ }
95
+ });
96
+ }
97
+ const taskResponse = new _response.TaskResponse(this.response);
98
+ const store = new _TaskManagerStore.TaskManagerStore({
99
+ context: this.context,
100
+ task,
101
+ log: taskLog,
102
+ disableDatabaseLogs
103
+ });
104
+ const manager = new _TaskManager.TaskManager(this.runner, this.context, this.response, taskResponse, store);
105
+ const databaseResponse = new _response.DatabaseResponse(this.response, store);
106
+ try {
107
+ const result = await manager.run(definition);
108
+ await this.runEvents(result, definition, task);
109
+ return await databaseResponse.from(result);
110
+ } catch (ex) {
111
+ /**
112
+ * We always want to store the error in the task log.
113
+ */
114
+ return await databaseResponse.from(this.response.error({
115
+ error: {
116
+ message: ex.message,
117
+ code: ex.code || "TASK_ERROR",
118
+ stack: ex.stack,
119
+ data: {
120
+ ...ex.data,
121
+ input: task.input
122
+ }
123
+ }
124
+ }));
125
+ }
126
+ }
127
+ async runEvents(result, definition, task) {
128
+ if (result.status === _types.TaskResponseStatus.ERROR && definition.onError) {
129
+ try {
130
+ await definition.onError({
131
+ task,
132
+ context: this.context
133
+ });
134
+ } catch (ex) {
135
+ console.error(`Error executing onError hook for task "${task.id}".`);
136
+ console.log((0, _getErrorProperties.getErrorProperties)(ex));
137
+ }
138
+ } else if (result.status === _types.TaskResponseStatus.DONE && definition.onDone) {
139
+ try {
140
+ await definition.onDone({
141
+ task,
142
+ context: this.context
143
+ });
144
+ } catch (ex) {
145
+ console.error(`Error executing onDone hook for task "${task.id}".`);
146
+ console.log((0, _getErrorProperties.getErrorProperties)(ex));
147
+ }
148
+ }
149
+ }
150
+ async getTask(id) {
151
+ try {
152
+ const task = await this.runner.context.tasks.getTask(id);
153
+ if (task) {
154
+ return task;
155
+ }
156
+ } catch (ex) {
157
+ throw this.response.error({
158
+ error: {
159
+ message: ex.message,
160
+ code: ex.code || "TASK_ERROR",
161
+ stack: ex.stack,
162
+ data: ex.data
163
+ }
164
+ });
165
+ }
166
+ throw this.response.error({
167
+ error: {
168
+ message: `Task "${id}" cannot be executed because it does not exist.`,
169
+ code: "TASK_NOT_FOUND"
170
+ }
171
+ });
172
+ }
173
+ async getTaskLog(task, disableDatabaseLogs) {
174
+ /**
175
+ * If logs are disabled, let's return a mocked one.
176
+ */
177
+ if (disableDatabaseLogs) {
178
+ return {
179
+ id: `${task.id}-log`,
180
+ createdOn: task.createdOn,
181
+ createdBy: task.createdBy,
182
+ executionName: task.executionName,
183
+ task: task.id,
184
+ iteration: task.iterations,
185
+ items: []
186
+ };
187
+ }
188
+ let taskLog = null;
189
+ /**
190
+ * First we are trying to get existing latest log.
191
+ */
192
+ try {
193
+ taskLog = await this.context.tasks.getLatestLog(task.id);
194
+ } catch (error) {
195
+ /**
196
+ * If error is not the NotFoundError, we need to throw it.
197
+ */
198
+ if (error instanceof _handlerGraphql.NotFoundError === false) {
199
+ throw this.response.error({
200
+ error
201
+ });
202
+ }
203
+ /**
204
+ * Otherwise just continue and create a new log.
205
+ */
206
+ }
207
+ const currentIteration = taskLog?.iteration || 0;
208
+ try {
209
+ return await this.context.tasks.createLog(task, {
210
+ executionName: this.response.event.executionName,
211
+ iteration: currentIteration + 1
212
+ });
213
+ } catch (error) {
214
+ throw this.response.error({
215
+ error
216
+ });
217
+ }
218
+ }
219
+ }
220
+ exports.TaskControl = TaskControl;
221
+
222
+ //# sourceMappingURL=TaskControl.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["_types","require","_TaskManager","_response","_TaskManagerStore","_handlerGraphql","_getErrorProperties","TaskControl","constructor","runner","response","context","run","event","taskId","webinyTaskId","task","getTask","security","setIdentity","createdBy","error","getErrorProperties","definition","tasks","getDefinition","definitionId","message","id","code","data","disableDatabaseLogs","taskLog","getTaskLog","taskStatus","TaskDataStatus","ABORTED","aborted","SUCCESS","FAILED","taskResponse","TaskResponse","store","TaskManagerStore","log","manager","TaskManager","databaseResponse","DatabaseResponse","result","runEvents","from","ex","stack","input","status","TaskResponseStatus","ERROR","onError","console","DONE","onDone","createdOn","executionName","iteration","iterations","items","getLatestLog","NotFoundError","currentIteration","createLog","exports"],"sources":["TaskControl.ts"],"sourcesContent":["import type { ITaskEvent } from \"~/handler/types\";\nimport type { Context, ITask, ITaskDataInput, ITaskDefinition, ITaskLog } from \"~/types\";\nimport { TaskDataStatus, TaskResponseStatus } from \"~/types\";\nimport type { ITaskControl, ITaskRunner } from \"./abstractions\";\nimport { TaskManager } from \"./TaskManager\";\nimport type { IResponse, IResponseResult } from \"~/response/abstractions\";\nimport { DatabaseResponse, TaskResponse } from \"~/response\";\nimport { TaskManagerStore } from \"./TaskManagerStore\";\nimport { NotFoundError } from \"@webiny/handler-graphql\";\nimport { getErrorProperties } from \"~/utils/getErrorProperties\";\n\nexport class TaskControl implements ITaskControl {\n public readonly runner: ITaskRunner;\n public readonly response: IResponse;\n public readonly context: Context;\n\n public constructor(runner: ITaskRunner, response: IResponse, context: Context) {\n this.runner = runner;\n this.context = context;\n this.response = response;\n }\n\n public async run(event: Pick<ITaskEvent, \"webinyTaskId\">): Promise<IResponseResult> {\n const taskId = event.webinyTaskId;\n /**\n * This is the initial getTask idea.\n * We will need to take care of child tasks:\n * * child tasks can be in multiple levels (child task creates a child task, etc...).\n * * child tasks could be executed in parallel.\n */\n let task: ITask<ITaskDataInput>;\n try {\n task = await this.getTask(taskId);\n this.context.security.setIdentity(task.createdBy);\n } catch (error) {\n /**\n * TODO Refactor error handling.\n */\n // @ts-expect-error\n return this.response.error({\n ...getErrorProperties(error)\n });\n }\n /**\n * Let's get the task definition.\n */\n const definition = this.context.tasks.getDefinition(task.definitionId);\n if (!definition) {\n return this.response.error({\n error: {\n message: `Task \"${task.id}\" cannot be executed because there is no \"${task.definitionId}\" definition plugin.`,\n code: \"TASK_DEFINITION_ERROR\",\n data: {\n definitionId: task.definitionId\n }\n }\n });\n }\n const disableDatabaseLogs = !!definition.disableDatabaseLogs;\n\n /**\n * As this as a run of the task, we need to create a new log entry.\n */\n let taskLog: ITaskLog;\n try {\n taskLog = await this.getTaskLog(task, disableDatabaseLogs);\n } catch (error) {\n return this.response.error({\n error\n });\n }\n /**\n * Make sure that task does not run if it is aborted.\n * This will effectively end the Step Function execution with a \"success\" status.\n */\n if (task.taskStatus === TaskDataStatus.ABORTED) {\n return this.response.aborted();\n }\n /**\n * Do not run if already a success (done).\n */\n //\n else if (task.taskStatus === TaskDataStatus.SUCCESS) {\n return this.response.error({\n error: {\n message: \"Task is already done, cannot run it again.\"\n }\n });\n }\n /**\n * Do not run if already failed.\n */\n //\n else if (task.taskStatus === TaskDataStatus.FAILED) {\n return this.response.error({\n error: {\n message: \"Task has failed, cannot run it again.\"\n }\n });\n }\n\n const taskResponse = new TaskResponse(this.response);\n const store = new TaskManagerStore({\n context: this.context,\n task,\n log: taskLog,\n disableDatabaseLogs\n });\n\n const manager = new TaskManager(\n this.runner,\n this.context,\n this.response,\n taskResponse,\n store\n );\n\n const databaseResponse = new DatabaseResponse(this.response, store);\n\n try {\n const result = await manager.run(definition);\n\n await this.runEvents(result, definition, task);\n\n return await databaseResponse.from(result);\n } catch (ex) {\n /**\n * We always want to store the error in the task log.\n */\n return await databaseResponse.from(\n this.response.error({\n error: {\n message: ex.message,\n code: ex.code || \"TASK_ERROR\",\n stack: ex.stack,\n data: {\n ...ex.data,\n input: task.input\n }\n }\n })\n );\n }\n }\n\n private async runEvents(\n result: IResponseResult,\n definition: ITaskDefinition,\n task: ITask\n ): Promise<void> {\n if (result.status === TaskResponseStatus.ERROR && definition.onError) {\n try {\n await definition.onError({\n task,\n context: this.context\n });\n } catch (ex) {\n console.error(`Error executing onError hook for task \"${task.id}\".`);\n console.log(getErrorProperties(ex));\n }\n } else if (result.status === TaskResponseStatus.DONE && definition.onDone) {\n try {\n await definition.onDone({\n task,\n context: this.context\n });\n } catch (ex) {\n console.error(`Error executing onDone hook for task \"${task.id}\".`);\n console.log(getErrorProperties(ex));\n }\n }\n }\n\n private async getTask<T = any>(id: string): Promise<ITask<T>> {\n try {\n const task = await this.runner.context.tasks.getTask<T>(id);\n if (task) {\n return task;\n }\n } catch (ex) {\n throw this.response.error({\n error: {\n message: ex.message,\n code: ex.code || \"TASK_ERROR\",\n stack: ex.stack,\n data: ex.data\n }\n });\n }\n throw this.response.error({\n error: {\n message: `Task \"${id}\" cannot be executed because it does not exist.`,\n code: \"TASK_NOT_FOUND\"\n }\n });\n }\n\n private async getTaskLog(task: ITask, disableDatabaseLogs: boolean): Promise<ITaskLog> {\n /**\n * If logs are disabled, let's return a mocked one.\n */\n if (disableDatabaseLogs) {\n return {\n id: `${task.id}-log`,\n createdOn: task.createdOn,\n createdBy: task.createdBy,\n executionName: task.executionName,\n task: task.id,\n iteration: task.iterations,\n items: []\n };\n }\n let taskLog: ITaskLog | null = null;\n /**\n * First we are trying to get existing latest log.\n */\n try {\n taskLog = await this.context.tasks.getLatestLog(task.id);\n } catch (error) {\n /**\n * If error is not the NotFoundError, we need to throw it.\n */\n if (error instanceof NotFoundError === false) {\n throw this.response.error({\n error\n });\n }\n /**\n * Otherwise just continue and create a new log.\n */\n }\n\n const currentIteration = taskLog?.iteration || 0;\n\n try {\n return await this.context.tasks.createLog(task, {\n executionName: this.response.event.executionName,\n iteration: currentIteration + 1\n });\n } catch (error) {\n throw this.response.error({\n error\n });\n }\n }\n}\n"],"mappings":";;;;;;AAEA,IAAAA,MAAA,GAAAC,OAAA;AAEA,IAAAC,YAAA,GAAAD,OAAA;AAEA,IAAAE,SAAA,GAAAF,OAAA;AACA,IAAA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AI,CAACI,SAAS;QACzB2C,aAAa,EAAE/C,IAAI,CAAC+C,aAAa;QACjC/C,IAAI,EAAEA,IAAI,CAACY,EAAE;QACboC,SAAS,EAAEhD,IAAI,CAACiD,UAAU;QAC1BC,KAAK,EAAE;MACX,CAAC;IACL;IACA,IAAIlC,OAAwB,GAAG,IAAI;IACnC;AACR;AACA;IACQ,IAAI;MACAA,OAAO,GAAG,MAAM,IAAI,CAACrB,OAAO,CAACa,KAAK,CAAC2C,YAAY,CAACnD,IAAI,CAACY,EAAE,CAAC;IAC5D,CAAC,CAAC,OAAOP,KAAK,EAAE;MACZ;AACZ;AACA;MACY,IAAIA,KAAK,YAAY+C,6BAAa,KAAK,KAAK,EAAE;QAC1C,MAAM,IAAI,CAAC1D,QAAQ,CAACW,KAAK,CAAC;UACtBA;QACJ,CAAC,CAAC;MACN;MACA;AACZ;AACA;IACQ;IAEA,MAAMgD,gBAAgB,GAAGrC,OAAO,EAAEgC,SAAS,IAAI,CAAC;IAEhD,IAAI;MACA,OAAO,MAAM,IAAI,CAACrD,OAAO,CAACa,KAAK,CAAC8C,SAAS,CAACtD,IAAI,EAAE;QAC5C+C,aAAa,EAAE,IAAI,CAACrD,QAAQ,CAACG,KAAK,CAACkD,aAAa;QAChDC,SAAS,EAAEK,gBAAgB,GAAG;MAClC,CAAC,CAAC;IACN,CAAC,CAAC,OAAOhD,KAAK,EAAE;MACZ,MAAM,IAAI,CAACX,QAAQ,CAACW,KAAK,CAAC;QACtBA;MACJ,CAAC,CAAC;IACN;EACJ;AACJ;AAACkD,OAAA,CAAAhE,WAAA,GAAAA,WAAA","ignoreList":[]}
@@ -0,0 +1,5 @@
1
+ import type { ITaskEventValidation, ITaskEventValidationResult } from "./abstractions";
2
+ import type { ITaskEvent } from "../handler/types";
3
+ export declare class TaskEventValidation implements ITaskEventValidation {
4
+ validate(event: Partial<ITaskEvent>): ITaskEventValidationResult;
5
+ }
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.TaskEventValidation = void 0;
8
+ var _zod = _interopRequireDefault(require("zod"));
9
+ var _utils = require("@webiny/utils");
10
+ const validation = _zod.default.object({
11
+ webinyTaskId: _zod.default.string(),
12
+ webinyTaskDefinitionId: _zod.default.string(),
13
+ endpoint: _zod.default.string(),
14
+ tenant: _zod.default.string(),
15
+ locale: _zod.default.string(),
16
+ executionName: _zod.default.string(),
17
+ stateMachineId: _zod.default.string()
18
+ }).required();
19
+ class TaskEventValidation {
20
+ validate(event) {
21
+ const result = validation.safeParse(event);
22
+ if (result.success) {
23
+ return result.data;
24
+ }
25
+ throw (0, _utils.createZodError)(result.error);
26
+ }
27
+ }
28
+ exports.TaskEventValidation = TaskEventValidation;
29
+
30
+ //# sourceMappingURL=TaskEventValidation.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["_zod","_interopRequireDefault","require","_utils","validation","zod","object","webinyTaskId","string","webinyTaskDefinitionId","endpoint","tenant","locale","executionName","stateMachineId","required","TaskEventValidation","validate","event","result","safeParse","success","data","createZodError","error","exports"],"sources":["TaskEventValidation.ts"],"sourcesContent":["import zod from \"zod\";\nimport { createZodError } from \"@webiny/utils\";\nimport type { ITaskEventValidation, ITaskEventValidationResult } from \"./abstractions\";\nimport type { ITaskEvent } from \"~/handler/types\";\n\nconst validation = zod\n .object({\n webinyTaskId: zod.string(),\n webinyTaskDefinitionId: zod.string(),\n endpoint: zod.string(),\n tenant: zod.string(),\n locale: zod.string(),\n executionName: zod.string(),\n stateMachineId: zod.string()\n })\n .required();\n\nexport class TaskEventValidation implements ITaskEventValidation {\n public validate(event: Partial<ITaskEvent>): ITaskEventValidationResult {\n const result = validation.safeParse(event);\n if (result.success) {\n return result.data;\n }\n throw createZodError(result.error);\n }\n}\n"],"mappings":";;;;;;;AAAA,IAAAA,IAAA,GAAAC,sBAAA,CAAAC,OAAA;AACA,IAAAC,MAAA,GAAAD,OAAA;AAIA,MAAME,UAAU,GAAGC,YAAG,CACjBC,MAAM,CAAC;EACJC,YAAY,EAAEF,YAAG,CAACG,MAAM,CAAC,CAAC;EAC1BC,sBAAsB,EAAEJ,YAAG,CAACG,MAAM,CAAC,CAAC;EACpCE,QAAQ,EAAEL,YAAG,CAACG,MAAM,CAAC,CAAC;EACtBG,MAAM,EAAEN,YAAG,CAACG,MAAM,CAAC,CAAC;EACpBI,MAAM,EAAEP,YAAG,CAACG,MAAM,CAAC,CAAC;EACpBK,aAAa,EAAER,YAAG,CAACG,MAAM,CAAC,CAAC;EAC3BM,cAAc,EAAET,YAAG,CAACG,MAAM,CAAC;AAC/B,CAAC,CAAC,CACDO,QAAQ,CAAC,CAAC;AAER,MAAMC,mBAAmB,CAAiC;EACtDC,QAAQA,CAACC,KAA0B,EAA8B;IACpE,MAAMC,MAAM,GAAGf,UAAU,CAACgB,SAAS,CAACF,KAAK,CAAC;IAC1C,IAAIC,MAAM,CAACE,OAAO,EAAE;MAChB,OAAOF,MAAM,CAACG,IAAI;IACtB;IACA,MAAM,IAAAC,qBAAc,EAACJ,MAAM,CAACK,KAAK,CAAC;EACtC;AACJ;AAACC,OAAA,CAAAT,mBAAA,GAAAA,mBAAA","ignoreList":[]}
@@ -0,0 +1,14 @@
1
+ import type { ITaskManager, ITaskManagerStorePrivate, ITaskRunner } from "./abstractions";
2
+ import type { Context, ITaskDataInput, ITaskDefinition } from "../types";
3
+ import type { IResponse, IResponseResult, ITaskResponse } from "../response/abstractions";
4
+ type ITaskManagerRunner = Pick<ITaskRunner, "isCloseToTimeout" | "timer">;
5
+ export declare class TaskManager<T = ITaskDataInput> implements ITaskManager<T> {
6
+ private readonly runner;
7
+ private readonly context;
8
+ private readonly response;
9
+ private readonly taskResponse;
10
+ private readonly store;
11
+ constructor(runner: ITaskManagerRunner, context: Context, response: IResponse, taskResponse: ITaskResponse, store: ITaskManagerStorePrivate);
12
+ run(definition: ITaskDefinition): Promise<IResponseResult>;
13
+ }
14
+ export {};
@@ -0,0 +1,139 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.TaskManager = void 0;
7
+ var _types = require("../types");
8
+ var _getErrorProperties = require("../utils/getErrorProperties");
9
+ class TaskManager {
10
+ constructor(runner, context, response, taskResponse, store) {
11
+ this.runner = runner;
12
+ this.context = context;
13
+ this.response = response;
14
+ this.taskResponse = taskResponse;
15
+ this.store = store;
16
+ }
17
+ async run(definition) {
18
+ /**
19
+ * If task was aborted, do not run it again, return as it was done.
20
+ */
21
+ if (this.store.getStatus() === _types.TaskDataStatus.ABORTED) {
22
+ return this.response.aborted();
23
+ }
24
+ /**
25
+ * If the task status is pending, update it to running and add a log.
26
+ */
27
+ //
28
+ else if (this.store.getStatus() === _types.TaskDataStatus.PENDING) {
29
+ try {
30
+ await this.store.updateTask({
31
+ taskStatus: _types.TaskDataStatus.RUNNING,
32
+ startedOn: new Date().toISOString(),
33
+ executionName: this.response.event.executionName,
34
+ iterations: 1
35
+ });
36
+ await this.store.save();
37
+ } catch (error) {
38
+ return this.response.error({
39
+ error
40
+ });
41
+ }
42
+ }
43
+ /**
44
+ * We do not want to run the task indefinitely.
45
+ * If the task has reached the max iterations, we will stop it and execute the onMaxIterations handler, if any.
46
+ */
47
+ //
48
+ else if (this.store.getTask().iterations >= definition.maxIterations) {
49
+ try {
50
+ if (definition.onMaxIterations) {
51
+ await definition.onMaxIterations({
52
+ task: this.store.getTask(),
53
+ context: this.context
54
+ });
55
+ }
56
+ return this.response.error({
57
+ error: {
58
+ message: "Task reached max iterations."
59
+ }
60
+ });
61
+ } catch (ex) {
62
+ return this.response.error({
63
+ error: {
64
+ message: "Failed to execute onMaxIterations handler.",
65
+ data: (0, _getErrorProperties.getErrorProperties)(ex)
66
+ }
67
+ });
68
+ }
69
+ }
70
+ /**
71
+ * Always update the task iteration.
72
+ */
73
+ //
74
+ else {
75
+ try {
76
+ await this.store.updateTask(task => {
77
+ return {
78
+ iterations: task.iterations + 1
79
+ };
80
+ });
81
+ } catch (error) {
82
+ return this.response.error({
83
+ error
84
+ });
85
+ }
86
+ }
87
+ let result;
88
+ try {
89
+ const input = structuredClone(this.store.getInput());
90
+ /**
91
+ * We always run the task without authorization because we are running a task without a user - nothing to authorize against.
92
+ */
93
+ result = await this.context.security.withoutAuthorization(async () => {
94
+ return await definition.run({
95
+ input,
96
+ context: this.context,
97
+ response: this.taskResponse,
98
+ isCloseToTimeout: seconds => {
99
+ return this.runner.isCloseToTimeout(seconds);
100
+ },
101
+ isAborted: () => {
102
+ return this.store.getStatus() === _types.TaskDataStatus.ABORTED;
103
+ },
104
+ store: this.store,
105
+ trigger: async params => {
106
+ return this.context.tasks.trigger({
107
+ ...params,
108
+ parent: this.store.getTask()
109
+ });
110
+ },
111
+ timer: this.runner.timer
112
+ });
113
+ });
114
+ } catch (ex) {
115
+ return this.response.error({
116
+ error: (0, _getErrorProperties.getErrorProperties)(ex)
117
+ });
118
+ }
119
+ if (result.status === _types.TaskResponseStatus.CONTINUE) {
120
+ return this.response.continue({
121
+ input: result.input,
122
+ wait: result.wait
123
+ });
124
+ } else if (result.status === _types.TaskResponseStatus.ERROR) {
125
+ return this.response.error({
126
+ error: result.error
127
+ });
128
+ } else if (result.status === _types.TaskResponseStatus.ABORTED) {
129
+ return this.response.aborted();
130
+ }
131
+ return this.response.done({
132
+ message: result.message,
133
+ output: result.output
134
+ });
135
+ }
136
+ }
137
+ exports.TaskManager = TaskManager;
138
+
139
+ //# sourceMappingURL=TaskManager.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["_types","require","_getErrorProperties","TaskManager","constructor","runner","context","response","taskResponse","store","run","definition","getStatus","TaskDataStatus","ABORTED","aborted","PENDING","updateTask","taskStatus","RUNNING","startedOn","Date","toISOString","executionName","event","iterations","save","error","getTask","maxIterations","onMaxIterations","task","message","ex","data","getErrorProperties","result","input","structuredClone","getInput","security","withoutAuthorization","isCloseToTimeout","seconds","isAborted","trigger","params","tasks","parent","timer","status","TaskResponseStatus","CONTINUE","continue","wait","ERROR","done","output","exports"],"sources":["TaskManager.ts"],"sourcesContent":["import type { ITaskManager, ITaskManagerStorePrivate, ITaskRunner } from \"./abstractions\";\nimport type { Context, ITask, ITaskDataInput, ITaskDefinition, ITaskTriggerParams } from \"~/types\";\nimport { TaskDataStatus, TaskResponseStatus } from \"~/types\";\nimport type {\n IResponse,\n IResponseResult,\n ITaskResponse,\n ITaskResponseResult\n} from \"~/response/abstractions\";\nimport { getErrorProperties } from \"~/utils/getErrorProperties\";\n\ntype ITaskManagerRunner = Pick<ITaskRunner, \"isCloseToTimeout\" | \"timer\">;\n\nexport class TaskManager<T = ITaskDataInput> implements ITaskManager<T> {\n private readonly runner: ITaskManagerRunner;\n private readonly context: Context;\n private readonly response: IResponse;\n private readonly taskResponse: ITaskResponse;\n private readonly store: ITaskManagerStorePrivate;\n\n public constructor(\n runner: ITaskManagerRunner,\n context: Context,\n response: IResponse,\n taskResponse: ITaskResponse,\n store: ITaskManagerStorePrivate\n ) {\n this.runner = runner;\n this.context = context;\n this.response = response;\n this.taskResponse = taskResponse;\n this.store = store;\n }\n\n public async run(definition: ITaskDefinition): Promise<IResponseResult> {\n /**\n * If task was aborted, do not run it again, return as it was done.\n */\n if (this.store.getStatus() === TaskDataStatus.ABORTED) {\n return this.response.aborted();\n }\n /**\n * If the task status is pending, update it to running and add a log.\n */\n //\n else if (this.store.getStatus() === TaskDataStatus.PENDING) {\n try {\n await this.store.updateTask({\n taskStatus: TaskDataStatus.RUNNING,\n startedOn: new Date().toISOString(),\n executionName: this.response.event.executionName,\n iterations: 1\n });\n await this.store.save();\n } catch (error) {\n return this.response.error({\n error\n });\n }\n }\n /**\n * We do not want to run the task indefinitely.\n * If the task has reached the max iterations, we will stop it and execute the onMaxIterations handler, if any.\n */\n //\n else if (this.store.getTask().iterations >= definition.maxIterations) {\n try {\n if (definition.onMaxIterations) {\n await definition.onMaxIterations({\n task: this.store.getTask(),\n context: this.context\n });\n }\n return this.response.error({\n error: {\n message: \"Task reached max iterations.\"\n }\n });\n } catch (ex) {\n return this.response.error({\n error: {\n message: \"Failed to execute onMaxIterations handler.\",\n data: getErrorProperties(ex)\n }\n });\n }\n }\n /**\n * Always update the task iteration.\n */\n //\n else {\n try {\n await this.store.updateTask(task => {\n return {\n iterations: task.iterations + 1\n };\n });\n } catch (error) {\n return this.response.error({\n error\n });\n }\n }\n\n let result: ITaskResponseResult;\n\n try {\n const input = structuredClone(this.store.getInput());\n /**\n * We always run the task without authorization because we are running a task without a user - nothing to authorize against.\n */\n result = await this.context.security.withoutAuthorization(async () => {\n return await definition.run({\n input,\n context: this.context,\n response: this.taskResponse,\n isCloseToTimeout: seconds => {\n return this.runner.isCloseToTimeout(seconds);\n },\n isAborted: () => {\n return this.store.getStatus() === TaskDataStatus.ABORTED;\n },\n store: this.store,\n trigger: async <I = ITaskDataInput>(\n params: Omit<ITaskTriggerParams<I>, \"parent\">\n ): Promise<ITask<I>> => {\n return this.context.tasks.trigger({\n ...params,\n parent: this.store.getTask()\n });\n },\n timer: this.runner.timer\n });\n });\n } catch (ex) {\n return this.response.error({\n error: getErrorProperties(ex)\n });\n }\n\n if (result.status === TaskResponseStatus.CONTINUE) {\n return this.response.continue({\n input: result.input,\n wait: result.wait\n });\n } else if (result.status === TaskResponseStatus.ERROR) {\n return this.response.error({\n error: result.error\n });\n } else if (result.status === TaskResponseStatus.ABORTED) {\n return this.response.aborted();\n }\n return this.response.done({\n message: result.message,\n output: result.output\n });\n }\n}\n"],"mappings":";;;;;;AAEA,IAAAA,MAAA,GAAAC,OAAA;AAOA,IAAAC,mBAAA,GAAAD,OAAA;AAIO,MAAME,WAAW,CAAgD;EAO7DC,WAAWA,CACdC,MAA0B,EAC1BC,OAAgB,EAChBC,QAAmB,EACnBC,YAA2B,EAC3BC,KAA+B,EACjC;IACE,IAAI,CAACJ,MAAM,GAAGA,MAAM;IACpB,IAAI,CAACC,OA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,WAAA","ignoreList":[]}
@@ -0,0 +1,40 @@
1
+ import type { ITask, ITaskDataInput, ITaskLog, ITaskManagerStoreInfoLog, ITaskManagerStorePrivate, ITaskManagerStoreSetOutputOptions, ITaskManagerStoreUpdateTaskInputOptions, ITaskManagerStoreUpdateTaskOptions, ITaskResponseDoneResultOutput, ITasksContextObject, TaskDataStatus } from "../types";
2
+ import type { ITaskManagerStoreAddLogOptions, ITaskManagerStoreErrorLog, ITaskManagerStoreUpdateTaskInputParam, ITaskManagerStoreUpdateTaskParams } from "./abstractions";
3
+ import type { GenericRecord } from "@webiny/api/types";
4
+ export interface TaskManagerStoreContext {
5
+ tasks: Pick<ITasksContextObject, "updateTask" | "updateLog" | "listTasks">;
6
+ }
7
+ export interface ITaskManagerStoreParams {
8
+ context: TaskManagerStoreContext;
9
+ task: ITask;
10
+ log: ITaskLog;
11
+ disableDatabaseLogs?: boolean;
12
+ }
13
+ export declare class TaskManagerStore<T extends ITaskDataInput = ITaskDataInput, O extends ITaskResponseDoneResultOutput = ITaskResponseDoneResultOutput> implements ITaskManagerStorePrivate<T, O> {
14
+ private readonly context;
15
+ private task;
16
+ private taskLog;
17
+ private readonly disableDatabaseLogs;
18
+ private readonly taskUpdater;
19
+ private readonly taskLogUpdater;
20
+ constructor(params: ITaskManagerStoreParams);
21
+ getStatus(): TaskDataStatus;
22
+ getTask(): ITask<T, O>;
23
+ listChildTasks<I = GenericRecord, O extends ITaskResponseDoneResultOutput = ITaskResponseDoneResultOutput>(definitionId?: string): Promise<ITask<I, O>[]>;
24
+ updateTask(param: ITaskManagerStoreUpdateTaskParams<T, O>, options?: ITaskManagerStoreUpdateTaskOptions): Promise<void>;
25
+ updateInput(param: ITaskManagerStoreUpdateTaskInputParam<T>, options?: ITaskManagerStoreUpdateTaskInputOptions): Promise<void>;
26
+ getInput(): T;
27
+ updateOutput(values: Partial<O>, options?: ITaskManagerStoreSetOutputOptions): Promise<void>;
28
+ getOutput(): O;
29
+ /**
30
+ * Currently the methods throws an error if something goes wrong during the database update.
31
+ * TODO: Maybe we should wrap it into try/catch and return error if any?
32
+ */
33
+ addInfoLog(log: ITaskManagerStoreInfoLog, options?: ITaskManagerStoreAddLogOptions): Promise<void>;
34
+ /**
35
+ * Currently the methods throws an error if something goes wrong during the database update.
36
+ * TODO: Maybe we should wrap it into try/catch and return error if any?
37
+ */
38
+ addErrorLog(log: ITaskManagerStoreErrorLog, options?: ITaskManagerStoreAddLogOptions): Promise<void>;
39
+ save(): Promise<void>;
40
+ }