@webiny/api-headless-cms-bulk-actions 0.0.0-unstable.3bc8100a7f
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.
- package/LICENSE +21 -0
- package/README.md +15 -0
- package/abstractions/IListEntries.d.ts +7 -0
- package/abstractions/IListEntries.js +7 -0
- package/abstractions/IListEntries.js.map +1 -0
- package/abstractions/IProcessEntry.d.ts +4 -0
- package/abstractions/IProcessEntry.js +7 -0
- package/abstractions/IProcessEntry.js.map +1 -0
- package/abstractions/IUseCase.d.ts +3 -0
- package/abstractions/IUseCase.js +7 -0
- package/abstractions/IUseCase.js.map +1 -0
- package/abstractions/index.d.ts +3 -0
- package/abstractions/index.js +40 -0
- package/abstractions/index.js.map +1 -0
- package/handlers/eventBridgeEventHandler.d.ts +1 -0
- package/handlers/eventBridgeEventHandler.js +53 -0
- package/handlers/eventBridgeEventHandler.js.map +1 -0
- package/handlers/index.d.ts +1 -0
- package/handlers/index.js +14 -0
- package/handlers/index.js.map +1 -0
- package/handlers/setupEventsTenant.d.ts +1 -0
- package/handlers/setupEventsTenant.js +19 -0
- package/handlers/setupEventsTenant.js.map +1 -0
- package/index.d.ts +6 -0
- package/index.js +73 -0
- package/index.js.map +1 -0
- package/package.json +45 -0
- package/plugins/createBulkAction.d.ts +10 -0
- package/plugins/createBulkAction.js +36 -0
- package/plugins/createBulkAction.js.map +1 -0
- package/plugins/createBulkActionGraphQL.d.ts +7 -0
- package/plugins/createBulkActionGraphQL.js +72 -0
- package/plugins/createBulkActionGraphQL.js.map +1 -0
- package/plugins/createBulkActionTasks.d.ts +9 -0
- package/plugins/createBulkActionTasks.js +135 -0
- package/plugins/createBulkActionTasks.js.map +1 -0
- package/plugins/createDefaultGraphQL.d.ts +3 -0
- package/plugins/createDefaultGraphQL.js +70 -0
- package/plugins/createDefaultGraphQL.js.map +1 -0
- package/plugins/index.d.ts +2 -0
- package/plugins/index.js +29 -0
- package/plugins/index.js.map +1 -0
- package/tasks/createBulkActionEntriesTasks.d.ts +1 -0
- package/tasks/createBulkActionEntriesTasks.js +38 -0
- package/tasks/createBulkActionEntriesTasks.js.map +1 -0
- package/tasks/createEmptyTrashBinsTask.d.ts +2 -0
- package/tasks/createEmptyTrashBinsTask.js +118 -0
- package/tasks/createEmptyTrashBinsTask.js.map +1 -0
- package/tasks/index.d.ts +2 -0
- package/tasks/index.js +29 -0
- package/tasks/index.js.map +1 -0
- package/types.d.ts +53 -0
- package/types.js +24 -0
- package/types.js.map +1 -0
- package/useCases/DeleteEntry.d.ts +10 -0
- package/useCases/DeleteEntry.js +26 -0
- package/useCases/DeleteEntry.js.map +1 -0
- package/useCases/ListDeletedEntries.d.ts +13 -0
- package/useCases/ListDeletedEntries.js +28 -0
- package/useCases/ListDeletedEntries.js.map +1 -0
- package/useCases/ListLatestEntries.d.ts +13 -0
- package/useCases/ListLatestEntries.js +28 -0
- package/useCases/ListLatestEntries.js.map +1 -0
- package/useCases/ListNotPublishedEntries.d.ts +13 -0
- package/useCases/ListNotPublishedEntries.js +34 -0
- package/useCases/ListNotPublishedEntries.js.map +1 -0
- package/useCases/ListPublishedEntries.d.ts +13 -0
- package/useCases/ListPublishedEntries.js +28 -0
- package/useCases/ListPublishedEntries.js.map +1 -0
- package/useCases/MoveEntryToFolder.d.ts +13 -0
- package/useCases/MoveEntryToFolder.js +23 -0
- package/useCases/MoveEntryToFolder.js.map +1 -0
- package/useCases/MoveEntryToTrash.d.ts +10 -0
- package/useCases/MoveEntryToTrash.js +26 -0
- package/useCases/MoveEntryToTrash.js.map +1 -0
- package/useCases/PublishEntry.d.ts +10 -0
- package/useCases/PublishEntry.js +20 -0
- package/useCases/PublishEntry.js.map +1 -0
- package/useCases/RestoreEntryFromTrash.d.ts +10 -0
- package/useCases/RestoreEntryFromTrash.js +24 -0
- package/useCases/RestoreEntryFromTrash.js.map +1 -0
- package/useCases/UnpublishEntry.d.ts +10 -0
- package/useCases/UnpublishEntry.js +20 -0
- package/useCases/UnpublishEntry.js.map +1 -0
- package/useCases/index.d.ts +10 -0
- package/useCases/index.js +117 -0
- package/useCases/index.js.map +1 -0
- package/useCases/internals/ChildTaskCleanup/ChildTasksCleanup.d.ts +17 -0
- package/useCases/internals/ChildTaskCleanup/ChildTasksCleanup.js +77 -0
- package/useCases/internals/ChildTaskCleanup/ChildTasksCleanup.js.map +1 -0
- package/useCases/internals/ChildTaskCleanup/index.d.ts +1 -0
- package/useCases/internals/ChildTaskCleanup/index.js +18 -0
- package/useCases/internals/ChildTaskCleanup/index.js.map +1 -0
- package/useCases/internals/CreateTasksByModel/CreateTasksByModel.d.ts +13 -0
- package/useCases/internals/CreateTasksByModel/CreateTasksByModel.js +111 -0
- package/useCases/internals/CreateTasksByModel/CreateTasksByModel.js.map +1 -0
- package/useCases/internals/CreateTasksByModel/TaskCache.d.ts +36 -0
- package/useCases/internals/CreateTasksByModel/TaskCache.js +76 -0
- package/useCases/internals/CreateTasksByModel/TaskCache.js.map +1 -0
- package/useCases/internals/CreateTasksByModel/index.d.ts +1 -0
- package/useCases/internals/CreateTasksByModel/index.js +18 -0
- package/useCases/internals/CreateTasksByModel/index.js.map +1 -0
- package/useCases/internals/ProcessTask/ProcessTask.d.ts +14 -0
- package/useCases/internals/ProcessTask/ProcessTask.js +81 -0
- package/useCases/internals/ProcessTask/ProcessTask.js.map +1 -0
- package/useCases/internals/ProcessTask/Result.d.ts +9 -0
- package/useCases/internals/ProcessTask/Result.js +29 -0
- package/useCases/internals/ProcessTask/Result.js.map +1 -0
- package/useCases/internals/ProcessTask/index.d.ts +1 -0
- package/useCases/internals/ProcessTask/index.js +18 -0
- package/useCases/internals/ProcessTask/index.js.map +1 -0
- package/useCases/internals/ProcessTasksByModel/ProcessTasksByModel.d.ts +11 -0
- package/useCases/internals/ProcessTasksByModel/ProcessTasksByModel.js +67 -0
- package/useCases/internals/ProcessTasksByModel/ProcessTasksByModel.js.map +1 -0
- package/useCases/internals/ProcessTasksByModel/index.d.ts +1 -0
- package/useCases/internals/ProcessTasksByModel/index.js +18 -0
- package/useCases/internals/ProcessTasksByModel/index.js.map +1 -0
- package/useCases/internals/index.d.ts +4 -0
- package/useCases/internals/index.js +51 -0
- package/useCases/internals/index.js.map +1 -0
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.ChildTasksCleanup = void 0;
|
|
7
|
+
var _tasks = require("@webiny/tasks");
|
|
8
|
+
/**
|
|
9
|
+
* Cleanup of the child tasks.
|
|
10
|
+
* This code will remove all the child tasks and their logs, which have no errors in them.
|
|
11
|
+
*/
|
|
12
|
+
class ChildTasksCleanup {
|
|
13
|
+
async execute(params) {
|
|
14
|
+
const {
|
|
15
|
+
context,
|
|
16
|
+
task
|
|
17
|
+
} = params;
|
|
18
|
+
const {
|
|
19
|
+
items: childTasks
|
|
20
|
+
} = await context.tasks.listTasks({
|
|
21
|
+
where: {
|
|
22
|
+
parentId: task.id
|
|
23
|
+
},
|
|
24
|
+
// Really doubtful there will be more than 10k of child tasks.
|
|
25
|
+
limit: 10000
|
|
26
|
+
});
|
|
27
|
+
if (childTasks.length === 0) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
const childTaskIdList = childTasks.map(childTask => childTask.id);
|
|
31
|
+
const {
|
|
32
|
+
items: childLogs
|
|
33
|
+
} = await context.tasks.listLogs({
|
|
34
|
+
where: {
|
|
35
|
+
task_in: childTaskIdList
|
|
36
|
+
},
|
|
37
|
+
limit: 10000
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* No logs found. Proceed with deleting the child tasks.
|
|
42
|
+
*/
|
|
43
|
+
if (childLogs.length === 0) {
|
|
44
|
+
await this.deleteTasks(context, childTaskIdList);
|
|
45
|
+
}
|
|
46
|
+
const deletedChildTaskLogIdList = [];
|
|
47
|
+
/**
|
|
48
|
+
* First, we need to remove all the logs which have no errors.
|
|
49
|
+
*/
|
|
50
|
+
for (const log of childLogs) {
|
|
51
|
+
if (log.items.some(item => item.type === _tasks.TaskLogItemType.ERROR)) {
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
await context.tasks.deleteLog(log.id);
|
|
55
|
+
if (deletedChildTaskLogIdList.includes(log.task)) {
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
deletedChildTaskLogIdList.push(log.task);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Now we can remove the tasks.
|
|
62
|
+
*/
|
|
63
|
+
await this.deleteTasks(context, deletedChildTaskLogIdList);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Helper method to delete tasks by ID.
|
|
68
|
+
*/
|
|
69
|
+
async deleteTasks(context, taskIds) {
|
|
70
|
+
for (const taskId of taskIds) {
|
|
71
|
+
await context.tasks.deleteTask(taskId);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
exports.ChildTasksCleanup = ChildTasksCleanup;
|
|
76
|
+
|
|
77
|
+
//# sourceMappingURL=ChildTasksCleanup.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["_tasks","require","ChildTasksCleanup","execute","params","context","task","items","childTasks","tasks","listTasks","where","parentId","id","limit","length","childTaskIdList","map","childTask","childLogs","listLogs","task_in","deleteTasks","deletedChildTaskLogIdList","log","some","item","type","TaskLogItemType","ERROR","deleteLog","includes","push","taskIds","taskId","deleteTask","exports"],"sources":["ChildTasksCleanup.ts"],"sourcesContent":["import type { ITask, Context } from \"@webiny/tasks\";\nimport { TaskLogItemType } from \"@webiny/tasks\";\nimport type { IUseCase } from \"~/abstractions\";\nimport type { HcmsBulkActionsContext } from \"~/types\";\n\nexport interface IChildTasksCleanupExecuteParams {\n context: Context;\n task: ITask;\n}\n\n/**\n * Cleanup of the child tasks.\n * This code will remove all the child tasks and their logs, which have no errors in them.\n */\nexport class ChildTasksCleanup implements IUseCase<IChildTasksCleanupExecuteParams, void> {\n public async execute(params: IChildTasksCleanupExecuteParams): Promise<void> {\n const { context, task } = params;\n\n const { items: childTasks } = await context.tasks.listTasks({\n where: {\n parentId: task.id\n },\n // Really doubtful there will be more than 10k of child tasks.\n limit: 10000\n });\n\n if (childTasks.length === 0) {\n return;\n }\n\n const childTaskIdList = childTasks.map(childTask => childTask.id);\n\n const { items: childLogs } = await context.tasks.listLogs({\n where: {\n task_in: childTaskIdList\n },\n limit: 10000\n });\n\n /**\n * No logs found. Proceed with deleting the child tasks.\n */\n if (childLogs.length === 0) {\n await this.deleteTasks(context, childTaskIdList);\n }\n\n const deletedChildTaskLogIdList: string[] = [];\n /**\n * First, we need to remove all the logs which have no errors.\n */\n for (const log of childLogs) {\n if (log.items.some(item => item.type === TaskLogItemType.ERROR)) {\n continue;\n }\n await context.tasks.deleteLog(log.id);\n if (deletedChildTaskLogIdList.includes(log.task)) {\n continue;\n }\n deletedChildTaskLogIdList.push(log.task);\n }\n /**\n * Now we can remove the tasks.\n */\n await this.deleteTasks(context, deletedChildTaskLogIdList);\n }\n\n /**\n * Helper method to delete tasks by ID.\n */\n private async deleteTasks(context: HcmsBulkActionsContext, taskIds: string[]): Promise<void> {\n for (const taskId of taskIds) {\n await context.tasks.deleteTask(taskId);\n }\n }\n}\n"],"mappings":";;;;;;AACA,IAAAA,MAAA,GAAAC,OAAA;AASA;AACA;AACA;AACA;AACO,MAAMC,iBAAiB,CAA4D;EACtF,MAAaC,OAAOA,CAACC,MAAuC,EAAiB;IACzE,MAAM;MAAEC,OAAO;MAAEC;IAAK,CAAC,GAAGF,MAAM;IAEhC,MAAM;MAAEG,KAAK,EAAEC;IAAW,CAAC,GAAG,MAAMH,OAAO,CAACI,KAAK,CAACC,SAAS,CAAC;MACxDC,KAAK,EAAE;QACHC,QAAQ,EAAEN,IAAI,CAACO;MACnB,CAAC;MACD;MACAC,KAAK,EAAE;IACX,C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OAAA,CAAAlC,iBAAA,GAAAA,iBAAA","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./ChildTasksCleanup";
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
var _ChildTasksCleanup = require("./ChildTasksCleanup");
|
|
7
|
+
Object.keys(_ChildTasksCleanup).forEach(function (key) {
|
|
8
|
+
if (key === "default" || key === "__esModule") return;
|
|
9
|
+
if (key in exports && exports[key] === _ChildTasksCleanup[key]) return;
|
|
10
|
+
Object.defineProperty(exports, key, {
|
|
11
|
+
enumerable: true,
|
|
12
|
+
get: function () {
|
|
13
|
+
return _ChildTasksCleanup[key];
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["_ChildTasksCleanup","require","Object","keys","forEach","key","exports","defineProperty","enumerable","get"],"sources":["index.ts"],"sourcesContent":["export * from \"./ChildTasksCleanup\";\n"],"mappings":";;;;;AAAA,IAAAA,kBAAA,GAAAC,OAAA;AAAAC,MAAA,CAAAC,IAAA,CAAAH,kBAAA,EAAAI,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAA,GAAA,IAAAC,OAAA,IAAAA,OAAA,CAAAD,GAAA,MAAAL,kBAAA,CAAAK,GAAA;EAAAH,MAAA,CAAAK,cAAA,CAAAD,OAAA,EAAAD,GAAA;IAAAG,UAAA;IAAAC,GAAA,WAAAA,CAAA;MAAA,OAAAT,kBAAA,CAAAK,GAAA;IAAA;EAAA;AAAA","ignoreList":[]}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { ITaskResponseResult } from "@webiny/tasks";
|
|
2
|
+
import type { IListEntries } from "../../../abstractions";
|
|
3
|
+
import type { IBulkActionOperationByModelTaskParams } from "../../../types";
|
|
4
|
+
/**
|
|
5
|
+
* The `CreateTasksByModel` class handles the execution of a task to process entries in batches.
|
|
6
|
+
*/
|
|
7
|
+
export declare class CreateTasksByModel {
|
|
8
|
+
private readonly taskCache;
|
|
9
|
+
private listEntriesGateway;
|
|
10
|
+
private readonly batchSize;
|
|
11
|
+
constructor(taskDefinition: string, listEntriesGateway: IListEntries, batchSize: number);
|
|
12
|
+
execute(params: IBulkActionOperationByModelTaskParams): Promise<ITaskResponseResult>;
|
|
13
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.CreateTasksByModel = void 0;
|
|
7
|
+
var _TaskCache = require("./TaskCache");
|
|
8
|
+
var _types = require("../../../types");
|
|
9
|
+
const MAX_TASK_LIST_LENGTH = 10;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The `CreateTasksByModel` class handles the execution of a task to process entries in batches.
|
|
13
|
+
*/
|
|
14
|
+
class CreateTasksByModel {
|
|
15
|
+
constructor(taskDefinition, listEntriesGateway, batchSize) {
|
|
16
|
+
this.taskCache = new _TaskCache.TaskCache(taskDefinition);
|
|
17
|
+
this.listEntriesGateway = listEntriesGateway;
|
|
18
|
+
this.batchSize = batchSize;
|
|
19
|
+
}
|
|
20
|
+
async execute(params) {
|
|
21
|
+
const {
|
|
22
|
+
response,
|
|
23
|
+
input,
|
|
24
|
+
isAborted,
|
|
25
|
+
isCloseToTimeout,
|
|
26
|
+
context,
|
|
27
|
+
store
|
|
28
|
+
} = params;
|
|
29
|
+
try {
|
|
30
|
+
const listEntriesParams = {
|
|
31
|
+
where: input.where,
|
|
32
|
+
search: input.search,
|
|
33
|
+
after: input.after,
|
|
34
|
+
limit: this.batchSize
|
|
35
|
+
};
|
|
36
|
+
while (true) {
|
|
37
|
+
if (isAborted()) {
|
|
38
|
+
return response.aborted();
|
|
39
|
+
} else if (isCloseToTimeout()) {
|
|
40
|
+
await this.taskCache.triggerTask(context, store.getTask());
|
|
41
|
+
return response.continue({
|
|
42
|
+
...input,
|
|
43
|
+
action: _types.BulkActionOperationByModelAction.PROCESS_SUBTASKS
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// List entries from the HCMS based on the provided query
|
|
48
|
+
const {
|
|
49
|
+
entries,
|
|
50
|
+
meta
|
|
51
|
+
} = await this.listEntriesGateway.execute(input.modelId, listEntriesParams);
|
|
52
|
+
|
|
53
|
+
// End the task if no entries match the query
|
|
54
|
+
if (meta.totalCount === 0) {
|
|
55
|
+
return response.continue({
|
|
56
|
+
...input,
|
|
57
|
+
action: _types.BulkActionOperationByModelAction.END_TASK
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Continue processing if we are reached the task list length limit
|
|
62
|
+
if (this.taskCache.getTasksLength() === MAX_TASK_LIST_LENGTH) {
|
|
63
|
+
await this.taskCache.triggerTask(context, store.getTask());
|
|
64
|
+
return response.continue({
|
|
65
|
+
...input,
|
|
66
|
+
action: _types.BulkActionOperationByModelAction.PROCESS_SUBTASKS
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Continue processing if no entries are returned in the current batch
|
|
71
|
+
if (entries.length === 0) {
|
|
72
|
+
await this.taskCache.triggerTask(context, store.getTask());
|
|
73
|
+
return response.continue({
|
|
74
|
+
...input,
|
|
75
|
+
action: _types.BulkActionOperationByModelAction.PROCESS_SUBTASKS
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Extract entry IDs
|
|
80
|
+
const ids = [];
|
|
81
|
+
for (let i = 0; i < entries.length; i++) {
|
|
82
|
+
ids.push(entries[i].id);
|
|
83
|
+
}
|
|
84
|
+
if (ids.length > 0) {
|
|
85
|
+
// Cache the task with the entry IDs
|
|
86
|
+
this.taskCache.cacheTask({
|
|
87
|
+
modelId: input.modelId,
|
|
88
|
+
identity: input.identity,
|
|
89
|
+
data: input.data,
|
|
90
|
+
ids
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Continue processing if there are no more entries or pagination is complete
|
|
95
|
+
if (!meta.hasMoreItems || !meta.cursor) {
|
|
96
|
+
await this.taskCache.triggerTask(context, store.getTask());
|
|
97
|
+
return response.continue({
|
|
98
|
+
...input,
|
|
99
|
+
action: _types.BulkActionOperationByModelAction.PROCESS_SUBTASKS
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
listEntriesParams.after = meta.cursor;
|
|
103
|
+
}
|
|
104
|
+
} catch (ex) {
|
|
105
|
+
return response.error(ex.message ?? `Error while creating task.`);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
exports.CreateTasksByModel = CreateTasksByModel;
|
|
110
|
+
|
|
111
|
+
//# sourceMappingURL=CreateTasksByModel.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["_TaskCache","require","_types","MAX_TASK_LIST_LENGTH","CreateTasksByModel","constructor","taskDefinition","listEntriesGateway","batchSize","taskCache","TaskCache","execute","params","response","input","isAborted","isCloseToTimeout","context","store","listEntriesParams","where","search","after","limit","aborted","triggerTask","getTask","continue","action","BulkActionOperationByModelAction","PROCESS_SUBTASKS","entries","meta","modelId","totalCount","END_TASK","getTasksLength","length","ids","i","push","id","cacheTask","identity","data","hasMoreItems","cursor","ex","error","message","exports"],"sources":["CreateTasksByModel.ts"],"sourcesContent":["import type { ITaskResponseResult } from \"@webiny/tasks\";\nimport { TaskCache } from \"./TaskCache\";\nimport type { CmsEntryListParams } from \"@webiny/api-headless-cms/types\";\nimport type { IListEntries } from \"~/abstractions\";\nimport type { IBulkActionOperationByModelTaskParams } from \"~/types\";\nimport { BulkActionOperationByModelAction } from \"~/types\";\n\nconst MAX_TASK_LIST_LENGTH = 10;\n\n/**\n * The `CreateTasksByModel` class handles the execution of a task to process entries in batches.\n */\nexport class CreateTasksByModel {\n private readonly taskCache: TaskCache;\n private listEntriesGateway: IListEntries;\n private readonly batchSize: number;\n\n constructor(taskDefinition: string, listEntriesGateway: IListEntries, batchSize: number) {\n this.taskCache = new TaskCache(taskDefinition);\n this.listEntriesGateway = listEntriesGateway;\n this.batchSize = batchSize;\n }\n\n async execute(params: IBulkActionOperationByModelTaskParams): Promise<ITaskResponseResult> {\n const { response, input, isAborted, isCloseToTimeout, context, store } = params;\n\n try {\n const listEntriesParams: CmsEntryListParams = {\n where: input.where,\n search: input.search,\n after: input.after,\n limit: this.batchSize\n };\n\n while (true) {\n if (isAborted()) {\n return response.aborted();\n } else if (isCloseToTimeout()) {\n await this.taskCache.triggerTask(context, store.getTask());\n return response.continue({\n ...input,\n action: BulkActionOperationByModelAction.PROCESS_SUBTASKS\n });\n }\n\n // List entries from the HCMS based on the provided query\n const { entries, meta } = await this.listEntriesGateway.execute(\n input.modelId,\n listEntriesParams\n );\n\n // End the task if no entries match the query\n if (meta.totalCount === 0) {\n return response.continue({\n ...input,\n action: BulkActionOperationByModelAction.END_TASK\n });\n }\n\n // Continue processing if we are reached the task list length limit\n if (this.taskCache.getTasksLength() === MAX_TASK_LIST_LENGTH) {\n await this.taskCache.triggerTask(context, store.getTask());\n return response.continue({\n ...input,\n action: BulkActionOperationByModelAction.PROCESS_SUBTASKS\n });\n }\n\n // Continue processing if no entries are returned in the current batch\n if (entries.length === 0) {\n await this.taskCache.triggerTask(context, store.getTask());\n return response.continue({\n ...input,\n action: BulkActionOperationByModelAction.PROCESS_SUBTASKS\n });\n }\n\n // Extract entry IDs\n const ids: string[] = [];\n for (let i = 0; i < entries.length; i++) {\n ids.push(entries[i].id);\n }\n\n if (ids.length > 0) {\n // Cache the task with the entry IDs\n this.taskCache.cacheTask({\n modelId: input.modelId,\n identity: input.identity,\n data: input.data,\n ids\n });\n }\n\n // Continue processing if there are no more entries or pagination is complete\n if (!meta.hasMoreItems || !meta.cursor) {\n await this.taskCache.triggerTask(context, store.getTask());\n\n return response.continue({\n ...input,\n action: BulkActionOperationByModelAction.PROCESS_SUBTASKS\n });\n }\n\n listEntriesParams.after = meta.cursor;\n }\n } catch (ex) {\n return response.error(ex.message ?? `Error while creating task.`);\n }\n }\n}\n"],"mappings":";;;;;;AACA,IAAAA,UAAA,GAAAC,OAAA;AAIA,IAAAC,MAAA,GAAAD,OAAA;AAEA,MAAME,oBAAoB,GAAG,EAAE;;AAE/B;AACA;AACA;AACO,MAAMC,kBAAkB,CAAC;EAK5BC,WAAWA,CAACC,cAAsB,EAAEC,kBAAgC,EAAEC,SAAiB,EAAE;IACrF,IAAI,CAACC,SAAS,GAAG,IAAIC,oBAAS,CAACJ,cAAc,CAAC;IAC9C,IAAI,CAACC,kBAAkB,GAAGA,kBAAkB;IAC5C,IAAI,CAACC,SAAS,GAAGA,SAAS;EAC9B;EAEA,MAAMG,OAAOA,CAACC,MAA6C,EAAgC;IACvF,MAAM;MAAEC,QAAQ;MAAEC,KAAK;MAAEC,SAAS;MAAEC,gBAAgB;MAAEC,OAAO;MAAEC;IAAM,CAAC,GAAGN,MAAM;IAE/E,IAAI;MACA,MAAMO,iBAAqC,GAAG;QAC1CC,KAAK,EAAEN,KAAK,CAACM,KAAK;QAClBC,MAAM,EAAEP,KAAK,CAACO,MAAM;QACpBC,KAAK,EAAER,KAAK,CAACQ,KAAK;QAClBC,KAAK,EAAE,IAAI,CAACf;MAChB,CAAC;MAED,OAAO,IAAI,EAAE;QACT,IAAIO,SAAS,CAAC,CAAC,EAAE;UACb,OAAOF,QAAQ,CAACW,OAAO,CAAC,CAAC;QAC7B,CAAC,MAAM,IAAIR,gBAAgB,CAAC,CAAC,EAAE;UAC3B,MAAM,IAAI,CAACP,SAAS,CAACgB,WAAW,CAACR,OAAO,EAAEC,KAAK,CAACQ,OAAO,CAAC,CAAC,CAAC;UAC1D,OAAOb,QAAQ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AG,CAACD,MAAM,GAAG,CAAC,EAAE;UAChB;UACA,IAAI,CAAC5B,SAAS,CAACiC,SAAS,CAAC;YACrBT,OAAO,EAAEnB,KAAK,CAACmB,OAAO;YACtBU,QAAQ,EAAE7B,KAAK,CAAC6B,QAAQ;YACxBC,IAAI,EAAE9B,KAAK,CAAC8B,IAAI;YAChBN;UACJ,CAAC,CAAC;QACN;;QAEA;QACA,IAAI,CAACN,IAAI,CAACa,YAAY,IAAI,CAACb,IAAI,CAACc,MAAM,EAAE;UACpC,MAAM,IAAI,CAACrC,SAAS,CAACgB,WAAW,CAACR,OAAO,EAAEC,KAAK,CAACQ,OAAO,CAAC,CAAC,CAAC;UAE1D,OAAOb,QAAQ,CAACc,QAAQ,CAAC;YACrB,GAAGb,KAAK;YACRc,MAAM,EAAEC,uCAAgC,CAACC;UAC7C,CAAC,CAAC;QACN;QAEAX,iBAAiB,CAACG,KAAK,GAAGU,IAAI,CAACc,MAAM;MACzC;IACJ,CAAC,CAAC,OAAOC,EAAE,EAAE;MACT,OAAOlC,QAAQ,CAACmC,KAAK,CAACD,EAAE,CAACE,OAAO,IAAI,4BAA4B,CAAC;IACrE;EACJ;AACJ;AAACC,OAAA,CAAA9C,kBAAA,GAAAA,kBAAA","ignoreList":[]}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { HcmsBulkActionsContext } from "../../../types";
|
|
2
|
+
import type { ITask, ITaskDataInput } from "@webiny/tasks";
|
|
3
|
+
/**
|
|
4
|
+
* TaskCache class for managing and triggering cached tasks.
|
|
5
|
+
* @template TTask - Task input data.
|
|
6
|
+
*/
|
|
7
|
+
export declare class TaskCache<TTask = ITaskDataInput> {
|
|
8
|
+
private readonly taskDefinition;
|
|
9
|
+
private taskCache;
|
|
10
|
+
constructor(taskDefinition: string);
|
|
11
|
+
/**
|
|
12
|
+
* Adds a task to the cache.
|
|
13
|
+
* @param {TTask} item - The task input data to be cached.
|
|
14
|
+
*/
|
|
15
|
+
cacheTask(item: TTask): void;
|
|
16
|
+
/**
|
|
17
|
+
* Triggers all cached tasks using the provided context and parent task.
|
|
18
|
+
* @param {HcmsBulkActionsContext} context - The context used to trigger the tasks.
|
|
19
|
+
* @param {ITask} parent - The parent task to associate with the triggered tasks.
|
|
20
|
+
*/
|
|
21
|
+
triggerTask(context: HcmsBulkActionsContext, parent: ITask): Promise<void>;
|
|
22
|
+
/**
|
|
23
|
+
* Retrieves the cached tasks length.
|
|
24
|
+
* @returns number
|
|
25
|
+
*/
|
|
26
|
+
getTasksLength(): number;
|
|
27
|
+
/**
|
|
28
|
+
* Retrieves the cached tasks.
|
|
29
|
+
* @returns {TTask[]} The list of cached tasks.
|
|
30
|
+
*/
|
|
31
|
+
private getTasks;
|
|
32
|
+
/**
|
|
33
|
+
* Clears all cached tasks.
|
|
34
|
+
*/
|
|
35
|
+
private clearTasks;
|
|
36
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.TaskCache = void 0;
|
|
7
|
+
/**
|
|
8
|
+
* TaskCache class for managing and triggering cached tasks.
|
|
9
|
+
* @template TTask - Task input data.
|
|
10
|
+
*/
|
|
11
|
+
class TaskCache {
|
|
12
|
+
taskCache = [];
|
|
13
|
+
constructor(taskDefinition) {
|
|
14
|
+
this.taskDefinition = taskDefinition;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Adds a task to the cache.
|
|
19
|
+
* @param {TTask} item - The task input data to be cached.
|
|
20
|
+
*/
|
|
21
|
+
cacheTask(item) {
|
|
22
|
+
this.taskCache.push(item);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Triggers all cached tasks using the provided context and parent task.
|
|
27
|
+
* @param {HcmsBulkActionsContext} context - The context used to trigger the tasks.
|
|
28
|
+
* @param {ITask} parent - The parent task to associate with the triggered tasks.
|
|
29
|
+
*/
|
|
30
|
+
async triggerTask(context, parent) {
|
|
31
|
+
const tasks = this.getTasks();
|
|
32
|
+
if (tasks.length === 0) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
for (const task of tasks) {
|
|
36
|
+
try {
|
|
37
|
+
await context.tasks.trigger({
|
|
38
|
+
definition: this.taskDefinition,
|
|
39
|
+
parent,
|
|
40
|
+
input: task
|
|
41
|
+
});
|
|
42
|
+
} catch (error) {
|
|
43
|
+
console.error(`Error triggering task.`, error);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Clear the cache after processing
|
|
48
|
+
this.clearTasks();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Retrieves the cached tasks length.
|
|
53
|
+
* @returns number
|
|
54
|
+
*/
|
|
55
|
+
getTasksLength() {
|
|
56
|
+
return this.getTasks().length;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Retrieves the cached tasks.
|
|
61
|
+
* @returns {TTask[]} The list of cached tasks.
|
|
62
|
+
*/
|
|
63
|
+
getTasks() {
|
|
64
|
+
return this.taskCache;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Clears all cached tasks.
|
|
69
|
+
*/
|
|
70
|
+
clearTasks() {
|
|
71
|
+
this.taskCache = [];
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
exports.TaskCache = TaskCache;
|
|
75
|
+
|
|
76
|
+
//# sourceMappingURL=TaskCache.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["TaskCache","taskCache","constructor","taskDefinition","cacheTask","item","push","triggerTask","context","parent","tasks","getTasks","length","task","trigger","definition","input","error","console","clearTasks","getTasksLength","exports"],"sources":["TaskCache.ts"],"sourcesContent":["import type { HcmsBulkActionsContext } from \"~/types\";\nimport type { ITask, ITaskDataInput } from \"@webiny/tasks\";\n\n/**\n * TaskCache class for managing and triggering cached tasks.\n * @template TTask - Task input data.\n */\nexport class TaskCache<TTask = ITaskDataInput> {\n private readonly taskDefinition: string;\n private taskCache: TTask[] = [];\n\n constructor(taskDefinition: string) {\n this.taskDefinition = taskDefinition;\n }\n\n /**\n * Adds a task to the cache.\n * @param {TTask} item - The task input data to be cached.\n */\n cacheTask(item: TTask) {\n this.taskCache.push(item);\n }\n\n /**\n * Triggers all cached tasks using the provided context and parent task.\n * @param {HcmsBulkActionsContext} context - The context used to trigger the tasks.\n * @param {ITask} parent - The parent task to associate with the triggered tasks.\n */\n async triggerTask(context: HcmsBulkActionsContext, parent: ITask) {\n const tasks = this.getTasks();\n\n if (tasks.length === 0) {\n return;\n }\n\n for (const task of tasks) {\n try {\n await context.tasks.trigger<TTask>({\n definition: this.taskDefinition,\n parent,\n input: task\n });\n } catch (error) {\n console.error(`Error triggering task.`, error);\n }\n }\n\n // Clear the cache after processing\n this.clearTasks();\n }\n\n /**\n * Retrieves the cached tasks length.\n * @returns number\n */\n getTasksLength() {\n return this.getTasks().length;\n }\n\n /**\n * Retrieves the cached tasks.\n * @returns {TTask[]} The list of cached tasks.\n */\n private getTasks() {\n return this.taskCache;\n }\n\n /**\n * Clears all cached tasks.\n */\n private clearTasks() {\n this.taskCache = [];\n }\n}\n"],"mappings":";;;;;;AAGA;AACA;AACA;AACA;AACO,MAAMA,SAAS,CAAyB;EAEnCC,SAAS,GAAY,EAAE;EAE/BC,WAAWA,CAACC,cAAsB,EAAE;IAChC,IAAI,CAACA,cAAc,GAAGA,cAAc;EACxC;;EAEA;AACJ;AACA;AACA;EACIC,SAASA,CAACC,IAAW,EAAE;IACnB,IAAI,CAACJ,SAAS,CAACK,IAAI,CAACD,IAAI,CAAC;EAC7B;;EAEA;AACJ;AACA;AACA;AACA;EACI,MAAME,WAAWA,CAACC,OAA+B,EAAEC,MAAa,EAAE;IAC9D,MAAMC,KAAK,GAAG,IAAI,CAACC,QAAQ,CAAC,CAAC;IAE7B,IAAID,KAAK,CAACE,MAAM,KAAK,CAAC,EAAE;MACpB;IACJ;IAEA,KAAK,MAAMC,IAAI,IAAIH,KAAK,EAAE;MACtB,IAAI;QACA,MAAMF,OAAO,CAACE,KAAK,CAACI,OAAO,CAAQ;UAC/BC,UAAU,EAAE,IAAI,CAACZ,cAAc;UAC/BM,MAAM;UACNO,KAAK,EAAEH;QACX,CAAC,CAAC;MACN,CAAC,CAAC,OAAOI,KAAK,EAAE;QACZC,OAAO,CAACD,KAAK,CAAC,wBAAwB,EAAEA,KAAK,CAAC;MAClD;IACJ;;IAEA;IACA,IAAI,CAACE,UAAU,CAAC,CAAC;EACrB;;EAEA;AACJ;AACA;AACA;EACIC,cAAcA,CAAA,EAAG;IACb,OAAO,IAAI,CAACT,QAAQ,CAAC,CAAC,CAACC,MAAM;EACjC;;EAEA;AACJ;AACA;AACA;EACYD,QAAQA,CAAA,EAAG;IACf,OAAO,IAAI,CAACV,SAAS;EACzB;;EAEA;AACJ;AACA;EACYkB,UAAUA,CAAA,EAAG;IACjB,IAAI,CAAClB,SAAS,GAAG,EAAE;EACvB;AACJ;AAACoB,OAAA,CAAArB,SAAA,GAAAA,SAAA","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./CreateTasksByModel";
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
var _CreateTasksByModel = require("./CreateTasksByModel");
|
|
7
|
+
Object.keys(_CreateTasksByModel).forEach(function (key) {
|
|
8
|
+
if (key === "default" || key === "__esModule") return;
|
|
9
|
+
if (key in exports && exports[key] === _CreateTasksByModel[key]) return;
|
|
10
|
+
Object.defineProperty(exports, key, {
|
|
11
|
+
enumerable: true,
|
|
12
|
+
get: function () {
|
|
13
|
+
return _CreateTasksByModel[key];
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["_CreateTasksByModel","require","Object","keys","forEach","key","exports","defineProperty","enumerable","get"],"sources":["index.ts"],"sourcesContent":["export * from \"./CreateTasksByModel\";\n"],"mappings":";;;;;AAAA,IAAAA,mBAAA,GAAAC,OAAA;AAAAC,MAAA,CAAAC,IAAA,CAAAH,mBAAA,EAAAI,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAA,GAAA,IAAAC,OAAA,IAAAA,OAAA,CAAAD,GAAA,MAAAL,mBAAA,CAAAK,GAAA;EAAAH,MAAA,CAAAK,cAAA,CAAAD,OAAA,EAAAD,GAAA;IAAAG,UAAA;IAAAC,GAAA,WAAAA,CAAA;MAAA,OAAAT,mBAAA,CAAAK,GAAA;IAAA;EAAA;AAAA","ignoreList":[]}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { IBulkActionOperationTaskParams } from "../../../types";
|
|
2
|
+
import type { IProcessEntry } from "../../../abstractions";
|
|
3
|
+
/**
|
|
4
|
+
* The `ProcessTask` class is responsible for processing a batch of entries
|
|
5
|
+
* based on the provided parameters. It uses a gateway to perform the actual
|
|
6
|
+
* processing and maintains the results of the operations, including successfully
|
|
7
|
+
* processed and failed entries.
|
|
8
|
+
*/
|
|
9
|
+
export declare class ProcessTask {
|
|
10
|
+
private readonly result;
|
|
11
|
+
private gateway;
|
|
12
|
+
constructor(gateway: IProcessEntry);
|
|
13
|
+
execute(params: IBulkActionOperationTaskParams): Promise<import("@webiny/tasks").ITaskResponseErrorResult | import("@webiny/tasks").ITaskResponseAbortedResult | import("@webiny/tasks").ITaskResponseContinueResult<import("../../../types").IBulkActionOperationInput> | import("@webiny/tasks").ITaskResponseDoneResult<import("../../../types").IBulkActionOperationOutput>>;
|
|
14
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.ProcessTask = void 0;
|
|
7
|
+
var _Result = require("./Result");
|
|
8
|
+
/**
|
|
9
|
+
* The `ProcessTask` class is responsible for processing a batch of entries
|
|
10
|
+
* based on the provided parameters. It uses a gateway to perform the actual
|
|
11
|
+
* processing and maintains the results of the operations, including successfully
|
|
12
|
+
* processed and failed entries.
|
|
13
|
+
*/
|
|
14
|
+
class ProcessTask {
|
|
15
|
+
constructor(gateway) {
|
|
16
|
+
this.result = new _Result.Result();
|
|
17
|
+
this.gateway = gateway;
|
|
18
|
+
}
|
|
19
|
+
async execute(params) {
|
|
20
|
+
const {
|
|
21
|
+
input,
|
|
22
|
+
response,
|
|
23
|
+
isAborted,
|
|
24
|
+
isCloseToTimeout,
|
|
25
|
+
context
|
|
26
|
+
} = params;
|
|
27
|
+
try {
|
|
28
|
+
if (isAborted()) {
|
|
29
|
+
return response.aborted();
|
|
30
|
+
} else if (isCloseToTimeout()) {
|
|
31
|
+
return response.continue({
|
|
32
|
+
...input
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Check if the input contains a model ID.
|
|
37
|
+
if (!input.modelId) {
|
|
38
|
+
return response.error(`Missing "modelId" in the input.`);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Check if the model exists.
|
|
42
|
+
const model = await context.cms.getModel(input.modelId);
|
|
43
|
+
if (!model) {
|
|
44
|
+
return response.error(`Model with ${input.modelId} not found!`);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Check if there are any IDs to process.
|
|
48
|
+
if (!input.ids || input.ids.length === 0) {
|
|
49
|
+
return response.done(`Task done: no entries to process for "${input.modelId}" model.`);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Set the security identity in the context.
|
|
53
|
+
context.security.setIdentity(input.identity);
|
|
54
|
+
|
|
55
|
+
// Process each ID in the input.
|
|
56
|
+
for (const id of input.ids) {
|
|
57
|
+
try {
|
|
58
|
+
// Execute the gateway operation for the current ID.
|
|
59
|
+
await this.gateway.execute(model, id, input.data);
|
|
60
|
+
// Add the ID to the list of successfully processed entries.
|
|
61
|
+
this.result.addDone(id);
|
|
62
|
+
} catch (ex) {
|
|
63
|
+
console.error(ex.message || `Failed to process entry with id "${id}".`);
|
|
64
|
+
// Add the ID to the list of failed entries.
|
|
65
|
+
this.result.addFailed(id);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Return a done response with the results of the processing.
|
|
70
|
+
return response.done(`Task done: all entries processed for "${model.name}" model.`, {
|
|
71
|
+
done: this.result.getDone(),
|
|
72
|
+
failed: this.result.getFailed()
|
|
73
|
+
});
|
|
74
|
+
} catch (ex) {
|
|
75
|
+
return response.error(ex.message ?? `Error while processing task.`);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
exports.ProcessTask = ProcessTask;
|
|
80
|
+
|
|
81
|
+
//# sourceMappingURL=ProcessTask.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["_Result","require","ProcessTask","constructor","gateway","result","Result","execute","params","input","response","isAborted","isCloseToTimeout","context","aborted","continue","modelId","error","model","cms","getModel","ids","length","done","security","setIdentity","identity","id","data","addDone","ex","console","message","addFailed","name","getDone","failed","getFailed","exports"],"sources":["ProcessTask.ts"],"sourcesContent":["import { Result } from \"./Result\";\nimport type { IBulkActionOperationTaskParams } from \"~/types\";\nimport type { IProcessEntry } from \"~/abstractions\";\n\n/**\n * The `ProcessTask` class is responsible for processing a batch of entries\n * based on the provided parameters. It uses a gateway to perform the actual\n * processing and maintains the results of the operations, including successfully\n * processed and failed entries.\n */\nexport class ProcessTask {\n private readonly result: Result;\n private gateway: IProcessEntry;\n\n constructor(gateway: IProcessEntry) {\n this.result = new Result();\n this.gateway = gateway;\n }\n\n async execute(params: IBulkActionOperationTaskParams) {\n const { input, response, isAborted, isCloseToTimeout, context } = params;\n\n try {\n if (isAborted()) {\n return response.aborted();\n } else if (isCloseToTimeout()) {\n return response.continue({\n ...input\n });\n }\n\n // Check if the input contains a model ID.\n if (!input.modelId) {\n return response.error(`Missing \"modelId\" in the input.`);\n }\n\n // Check if the model exists.\n const model = await context.cms.getModel(input.modelId);\n\n if (!model) {\n return response.error(`Model with ${input.modelId} not found!`);\n }\n\n // Check if there are any IDs to process.\n if (!input.ids || input.ids.length === 0) {\n return response.done(\n `Task done: no entries to process for \"${input.modelId}\" model.`\n );\n }\n\n // Set the security identity in the context.\n context.security.setIdentity(input.identity);\n\n // Process each ID in the input.\n for (const id of input.ids) {\n try {\n // Execute the gateway operation for the current ID.\n await this.gateway.execute(model, id, input.data);\n // Add the ID to the list of successfully processed entries.\n this.result.addDone(id);\n } catch (ex) {\n console.error(ex.message || `Failed to process entry with id \"${id}\".`);\n // Add the ID to the list of failed entries.\n this.result.addFailed(id);\n }\n }\n\n // Return a done response with the results of the processing.\n return response.done(`Task done: all entries processed for \"${model.name}\" model.`, {\n done: this.result.getDone(),\n failed: this.result.getFailed()\n });\n } catch (ex) {\n return response.error(ex.message ?? `Error while processing task.`);\n }\n }\n}\n"],"mappings":";;;;;;AAAA,IAAAA,OAAA,GAAAC,OAAA;AAIA;AACA;AACA;AACA;AACA;AACA;AACO,MAAMC,WAAW,CAAC;EAIrBC,WAAWA,CAACC,OAAsB,EAAE;IAChC,IAAI,CAACC,MAAM,GAAG,IAAIC,cAAM,CAAC,CAAC;IAC1B,IAAI,CAACF,OAAO,GAAGA,OAAO;EAC1B;EAEA,MAAMG,OAAOA,CAACC,MAAsC,EAAE;IAClD,MAAM;MAAEC,KAAK;MAAEC,QAAQ;MAAEC,SAAS;MAAEC,gBAAgB;MAAEC;IAAQ,CAAC,GAAGL,MAAM;IAExE,IAAI;MACA,IAAIG,SAAS,CAAC,CAAC,EAAE;QACb,OAAOD,QAAQ,CAAC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gB,IAAI,UAAU,EAAE;QAChFX,IAAI,EAAE,IAAI,CAAClB,MAAM,CAAC8B,OAAO,CAAC,CAAC;QAC3BC,MAAM,EAAE,IAAI,CAAC/B,MAAM,CAACgC,SAAS,CAAC;MAClC,CAAC,CAAC;IACN,CAAC,CAAC,OAAOP,EAAE,EAAE;MACT,OAAOpB,QAAQ,CAACO,KAAK,CAACa,EAAE,CAACE,OAAO,IAAI,8BAA8B,CAAC;IACvE;EACJ;AACJ;AAACM,OAAA,CAAApC,WAAA,GAAAA,WAAA","ignoreList":[]}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.Result = void 0;
|
|
7
|
+
class Result {
|
|
8
|
+
constructor() {
|
|
9
|
+
this.done = new Set();
|
|
10
|
+
this.failed = new Set();
|
|
11
|
+
}
|
|
12
|
+
addDone(entryId) {
|
|
13
|
+
this.failed.delete(entryId);
|
|
14
|
+
this.done.add(entryId);
|
|
15
|
+
}
|
|
16
|
+
addFailed(entryId) {
|
|
17
|
+
this.failed.add(entryId);
|
|
18
|
+
this.done.delete(entryId);
|
|
19
|
+
}
|
|
20
|
+
getFailed() {
|
|
21
|
+
return Array.from(this.failed);
|
|
22
|
+
}
|
|
23
|
+
getDone() {
|
|
24
|
+
return Array.from(this.done);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
exports.Result = Result;
|
|
28
|
+
|
|
29
|
+
//# sourceMappingURL=Result.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["Result","constructor","done","Set","failed","addDone","entryId","delete","add","addFailed","getFailed","Array","from","getDone","exports"],"sources":["Result.ts"],"sourcesContent":["export class Result {\n private readonly done: Set<string>;\n private readonly failed: Set<string>;\n\n public constructor() {\n this.done = new Set();\n this.failed = new Set();\n }\n\n public addDone(entryId: string): void {\n this.failed.delete(entryId);\n this.done.add(entryId);\n }\n\n public addFailed(entryId: string): void {\n this.failed.add(entryId);\n this.done.delete(entryId);\n }\n\n public getFailed() {\n return Array.from(this.failed);\n }\n\n public getDone() {\n return Array.from(this.done);\n }\n}\n"],"mappings":";;;;;;AAAO,MAAMA,MAAM,CAAC;EAITC,WAAWA,CAAA,EAAG;IACjB,IAAI,CAACC,IAAI,GAAG,IAAIC,GAAG,CAAC,CAAC;IACrB,IAAI,CAACC,MAAM,GAAG,IAAID,GAAG,CAAC,CAAC;EAC3B;EAEOE,OAAOA,CAACC,OAAe,EAAQ;IAClC,IAAI,CAACF,MAAM,CAACG,MAAM,CAACD,OAAO,CAAC;IAC3B,IAAI,CAACJ,IAAI,CAACM,GAAG,CAACF,OAAO,CAAC;EAC1B;EAEOG,SAASA,CAACH,OAAe,EAAQ;IACpC,IAAI,CAACF,MAAM,CAACI,GAAG,CAACF,OAAO,CAAC;IACxB,IAAI,CAACJ,IAAI,CAACK,MAAM,CAACD,OAAO,CAAC;EAC7B;EAEOI,SAASA,CAAA,EAAG;IACf,OAAOC,KAAK,CAACC,IAAI,CAAC,IAAI,CAACR,MAAM,CAAC;EAClC;EAEOS,OAAOA,CAAA,EAAG;IACb,OAAOF,KAAK,CAACC,IAAI,CAAC,IAAI,CAACV,IAAI,CAAC;EAChC;AACJ;AAACY,OAAA,CAAAd,MAAA,GAAAA,MAAA","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./ProcessTask";
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
var _ProcessTask = require("./ProcessTask");
|
|
7
|
+
Object.keys(_ProcessTask).forEach(function (key) {
|
|
8
|
+
if (key === "default" || key === "__esModule") return;
|
|
9
|
+
if (key in exports && exports[key] === _ProcessTask[key]) return;
|
|
10
|
+
Object.defineProperty(exports, key, {
|
|
11
|
+
enumerable: true,
|
|
12
|
+
get: function () {
|
|
13
|
+
return _ProcessTask[key];
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["_ProcessTask","require","Object","keys","forEach","key","exports","defineProperty","enumerable","get"],"sources":["index.ts"],"sourcesContent":["export * from \"./ProcessTask\";\n"],"mappings":";;;;;AAAA,IAAAA,YAAA,GAAAC,OAAA;AAAAC,MAAA,CAAAC,IAAA,CAAAH,YAAA,EAAAI,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAA,GAAA,IAAAC,OAAA,IAAAA,OAAA,CAAAD,GAAA,MAAAL,YAAA,CAAAK,GAAA;EAAAH,MAAA,CAAAK,cAAA,CAAAD,OAAA,EAAAD,GAAA;IAAAG,UAAA;IAAAC,GAAA,WAAAA,CAAA;MAAA,OAAAT,YAAA,CAAAK,GAAA;IAAA;EAAA;AAAA","ignoreList":[]}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { IBulkActionOperationByModelTaskParams } from "../../../types";
|
|
2
|
+
/**
|
|
3
|
+
* The `ProcessTasksByModel` class is responsible for processing tasks for a specific model.
|
|
4
|
+
* It checks for any running or pending tasks from the parent task and continues or completes
|
|
5
|
+
* the task based on the status.
|
|
6
|
+
*/
|
|
7
|
+
export declare class ProcessTasksByModel {
|
|
8
|
+
private taskDefinition;
|
|
9
|
+
constructor(taskDefinition: string);
|
|
10
|
+
execute(params: IBulkActionOperationByModelTaskParams): Promise<import("@webiny/tasks").ITaskResponseErrorResult | import("@webiny/tasks").ITaskResponseAbortedResult | import("@webiny/tasks").ITaskResponseContinueResult<import("../../../types").IBulkActionOperationByModelInput>>;
|
|
11
|
+
}
|