@webiny/background-tasks 0.0.0-unstable.0d717d18dd → 0.0.0-unstable.3fddf667ea
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/admin/presentation/TaskExecutions/TaskExecutionsPresenter.d.ts +1 -0
- package/admin/presentation/TaskExecutions/TaskExecutionsPresenter.js +16 -1
- package/admin/presentation/TaskExecutions/TaskExecutionsPresenter.js.map +1 -1
- package/admin/presentation/TaskExecutions/abstractions.d.ts +2 -0
- package/admin/presentation/TaskExecutions/abstractions.js.map +1 -1
- package/admin/presentation/TaskExecutions/components/TaskExecutionsView.js +1 -0
- package/admin/presentation/TaskExecutions/components/TaskExecutionsView.js.map +1 -1
- package/api/BackgroundTasksFeature.js +44 -24
- package/api/BackgroundTasksFeature.js.map +1 -1
- package/api/crud/TaskLogPrivateModel.js +2 -1
- package/api/crud/TaskLogPrivateModel.js.map +1 -1
- package/api/crud/TaskPrivateModel.js +2 -1
- package/api/crud/TaskPrivateModel.js.map +1 -1
- package/api/decorators/{SelfCleaningTaskDecorator.d.ts → SelfCleaningTaskHandlerDecorator.d.ts} +21 -18
- package/api/decorators/SelfCleaningTaskHandlerDecorator.js +69 -0
- package/api/decorators/SelfCleaningTaskHandlerDecorator.js.map +1 -0
- package/api/decorators/TaskDefinitionDefaultsDecorator.d.ts +27 -0
- package/api/decorators/TaskDefinitionDefaultsDecorator.js +47 -0
- package/api/decorators/TaskDefinitionDefaultsDecorator.js.map +1 -0
- package/api/domain/errors.d.ts +14 -0
- package/api/domain/errors.js +27 -2
- package/api/domain/errors.js.map +1 -1
- package/api/domain/task/TaskLogModelProvider.d.ts +12 -0
- package/api/domain/task/TaskLogModelProvider.js +23 -0
- package/api/domain/task/TaskLogModelProvider.js.map +1 -0
- package/api/domain/task/TaskLogsRepository.d.ts +34 -0
- package/api/domain/task/TaskLogsRepository.js +146 -0
- package/api/domain/task/TaskLogsRepository.js.map +1 -0
- package/api/domain/task/TaskModelProvider.d.ts +12 -0
- package/api/domain/task/TaskModelProvider.js +23 -0
- package/api/domain/task/TaskModelProvider.js.map +1 -0
- package/api/domain/task/TasksRepository.d.ts +37 -0
- package/api/domain/task/TasksRepository.js +126 -0
- package/api/domain/task/TasksRepository.js.map +1 -0
- package/api/domain/task/abstractions.d.ts +50 -0
- package/api/domain/task/abstractions.js +8 -0
- package/api/domain/task/abstractions.js.map +1 -0
- package/api/domain/task/createRevisionId.d.ts +1 -0
- package/api/domain/task/createRevisionId.js +8 -0
- package/api/domain/task/createRevisionId.js.map +1 -0
- package/api/domain/task/entryToTask.d.ts +3 -0
- package/api/domain/task/entryToTask.js +20 -0
- package/api/domain/task/entryToTask.js.map +1 -0
- package/api/domain/task/entryToTaskLog.d.ts +3 -0
- package/api/domain/task/entryToTaskLog.js +12 -0
- package/api/domain/task/entryToTaskLog.js.map +1 -0
- package/api/features/AbortTask/abstractions.d.ts +4 -0
- package/api/features/AbortTask/abstractions.js.map +1 -1
- package/api/features/CleanupTaskSubtree/CleanupTaskSubtreeUseCase.d.ts +25 -5
- package/api/features/CleanupTaskSubtree/CleanupTaskSubtreeUseCase.js +111 -5
- package/api/features/CleanupTaskSubtree/CleanupTaskSubtreeUseCase.js.map +1 -1
- package/api/features/CleanupTaskSubtree/feature.d.ts +4 -0
- package/api/features/CleanupTaskSubtree/feature.js +11 -0
- package/api/features/CleanupTaskSubtree/feature.js.map +1 -0
- package/api/features/CleanupTaskSubtree/index.d.ts +1 -1
- package/api/features/CleanupTaskSubtree/index.js +1 -1
- package/api/features/CreateTask/CreateTaskUseCase.d.ts +17 -0
- package/api/features/CreateTask/CreateTaskUseCase.js +56 -0
- package/api/features/CreateTask/CreateTaskUseCase.js.map +1 -0
- package/api/features/CreateTask/abstractions.d.ts +21 -0
- package/api/features/CreateTask/abstractions.js +5 -0
- package/api/features/CreateTask/abstractions.js.map +1 -0
- package/api/features/{GetTaskDefinition → CreateTask}/feature.d.ts +1 -1
- package/api/features/CreateTask/feature.js +11 -0
- package/api/features/CreateTask/feature.js.map +1 -0
- package/api/features/CreateTask/index.d.ts +2 -0
- package/api/features/CreateTask/index.js +2 -0
- package/api/features/CreateTaskLog/CreateTaskLogUseCase.d.ts +13 -0
- package/api/features/CreateTaskLog/CreateTaskLogUseCase.js +19 -0
- package/api/features/CreateTaskLog/CreateTaskLogUseCase.js.map +1 -0
- package/api/features/CreateTaskLog/abstractions.d.ts +16 -0
- package/api/features/CreateTaskLog/abstractions.js +5 -0
- package/api/features/CreateTaskLog/abstractions.js.map +1 -0
- package/api/features/CreateTaskLog/feature.d.ts +4 -0
- package/api/features/CreateTaskLog/feature.js +11 -0
- package/api/features/CreateTaskLog/feature.js.map +1 -0
- package/api/features/CreateTaskLog/index.d.ts +2 -0
- package/api/features/CreateTaskLog/index.js +2 -0
- package/api/features/DeleteTask/DeleteTaskUseCase.d.ts +14 -0
- package/api/features/DeleteTask/DeleteTaskUseCase.js +34 -0
- package/api/features/DeleteTask/DeleteTaskUseCase.js.map +1 -0
- package/api/features/DeleteTask/abstractions.d.ts +17 -0
- package/api/features/DeleteTask/abstractions.js +5 -0
- package/api/features/DeleteTask/abstractions.js.map +1 -0
- package/api/features/DeleteTask/feature.d.ts +4 -0
- package/api/features/DeleteTask/feature.js +11 -0
- package/api/features/DeleteTask/feature.js.map +1 -0
- package/api/features/DeleteTask/index.d.ts +2 -0
- package/api/features/DeleteTask/index.js +2 -0
- package/api/features/DeleteTaskLog/DeleteTaskLogUseCase.d.ts +11 -0
- package/api/features/DeleteTaskLog/DeleteTaskLogUseCase.js +19 -0
- package/api/features/DeleteTaskLog/DeleteTaskLogUseCase.js.map +1 -0
- package/api/features/DeleteTaskLog/abstractions.d.ts +14 -0
- package/api/features/DeleteTaskLog/abstractions.js +5 -0
- package/api/features/DeleteTaskLog/abstractions.js.map +1 -0
- package/api/features/DeleteTaskLog/feature.d.ts +4 -0
- package/api/features/DeleteTaskLog/feature.js +11 -0
- package/api/features/DeleteTaskLog/feature.js.map +1 -0
- package/api/features/DeleteTaskLog/index.d.ts +2 -0
- package/api/features/DeleteTaskLog/index.js +2 -0
- package/api/features/GetLatestTaskLog/GetLatestTaskLogUseCase.d.ts +11 -0
- package/api/features/GetLatestTaskLog/GetLatestTaskLogUseCase.js +19 -0
- package/api/features/GetLatestTaskLog/GetLatestTaskLogUseCase.js.map +1 -0
- package/api/features/GetLatestTaskLog/abstractions.d.ts +15 -0
- package/api/features/GetLatestTaskLog/abstractions.js +5 -0
- package/api/features/GetLatestTaskLog/abstractions.js.map +1 -0
- package/api/features/GetLatestTaskLog/feature.d.ts +4 -0
- package/api/features/GetLatestTaskLog/feature.js +11 -0
- package/api/features/GetLatestTaskLog/feature.js.map +1 -0
- package/api/features/GetLatestTaskLog/index.d.ts +2 -0
- package/api/features/GetLatestTaskLog/index.js +2 -0
- package/api/features/GetRunnableTaskDefinition/GetRunnableTaskDefinitionUseCase.d.ts +13 -0
- package/api/features/GetRunnableTaskDefinition/GetRunnableTaskDefinitionUseCase.js +51 -0
- package/api/features/GetRunnableTaskDefinition/GetRunnableTaskDefinitionUseCase.js.map +1 -0
- package/api/features/{GetTaskDefinition → GetRunnableTaskDefinition}/abstractions.d.ts +6 -6
- package/api/features/GetRunnableTaskDefinition/abstractions.js +5 -0
- package/api/features/GetRunnableTaskDefinition/abstractions.js.map +1 -0
- package/api/features/GetRunnableTaskDefinition/feature.d.ts +4 -0
- package/api/features/GetRunnableTaskDefinition/feature.js +11 -0
- package/api/features/GetRunnableTaskDefinition/feature.js.map +1 -0
- package/api/features/GetRunnableTaskDefinition/index.d.ts +2 -0
- package/api/features/GetRunnableTaskDefinition/index.js +2 -0
- package/api/features/GetTask/GetTaskUseCase.d.ts +9 -5
- package/api/features/GetTask/GetTaskUseCase.js +12 -4
- package/api/features/GetTask/GetTaskUseCase.js.map +1 -1
- package/api/features/GetTask/abstractions.d.ts +9 -2
- package/api/features/GetTask/abstractions.js.map +1 -1
- package/api/features/GetTask/feature.js +2 -4
- package/api/features/GetTask/feature.js.map +1 -1
- package/api/features/ListTaskLogs/ListTaskLogsUseCase.d.ts +12 -0
- package/api/features/ListTaskLogs/ListTaskLogsUseCase.js +19 -0
- package/api/features/ListTaskLogs/ListTaskLogsUseCase.js.map +1 -0
- package/api/features/ListTaskLogs/abstractions.d.ts +15 -0
- package/api/features/ListTaskLogs/abstractions.js +5 -0
- package/api/features/ListTaskLogs/abstractions.js.map +1 -0
- package/api/features/ListTaskLogs/feature.d.ts +4 -0
- package/api/features/ListTaskLogs/feature.js +11 -0
- package/api/features/ListTaskLogs/feature.js.map +1 -0
- package/api/features/ListTaskLogs/index.d.ts +2 -0
- package/api/features/ListTaskLogs/index.js +2 -0
- package/api/features/ListTasks/ListTasksUseCase.d.ts +9 -6
- package/api/features/ListTasks/ListTasksUseCase.js +12 -4
- package/api/features/ListTasks/ListTasksUseCase.js.map +1 -1
- package/api/features/ListTasks/abstractions.d.ts +9 -3
- package/api/features/ListTasks/abstractions.js.map +1 -1
- package/api/features/ListTasks/feature.js +2 -4
- package/api/features/ListTasks/feature.js.map +1 -1
- package/api/features/ListVisibleTaskDefinitions/ListVisibleTaskDefinitionsUseCase.d.ts +11 -0
- package/api/features/ListVisibleTaskDefinitions/ListVisibleTaskDefinitionsUseCase.js +19 -0
- package/api/features/ListVisibleTaskDefinitions/ListVisibleTaskDefinitionsUseCase.js.map +1 -0
- package/api/features/ListVisibleTaskDefinitions/abstractions.d.ts +9 -0
- package/api/features/ListVisibleTaskDefinitions/abstractions.js +5 -0
- package/api/features/ListVisibleTaskDefinitions/abstractions.js.map +1 -0
- package/api/features/ListVisibleTaskDefinitions/feature.d.ts +4 -0
- package/api/features/ListVisibleTaskDefinitions/feature.js +11 -0
- package/api/features/ListVisibleTaskDefinitions/feature.js.map +1 -0
- package/api/features/ListVisibleTaskDefinitions/index.d.ts +2 -0
- package/api/features/ListVisibleTaskDefinitions/index.js +2 -0
- package/api/features/TaskHandlerResolver/TaskHandlerResolver.d.ts +12 -0
- package/api/features/TaskHandlerResolver/TaskHandlerResolver.js +19 -0
- package/api/features/TaskHandlerResolver/TaskHandlerResolver.js.map +1 -0
- package/api/features/TaskHandlerResolver/abstractions.d.ts +17 -0
- package/api/features/TaskHandlerResolver/abstractions.js +5 -0
- package/api/features/TaskHandlerResolver/abstractions.js.map +1 -0
- package/api/features/TaskHandlerResolver/feature.d.ts +9 -0
- package/api/features/TaskHandlerResolver/feature.js +11 -0
- package/api/features/TaskHandlerResolver/feature.js.map +1 -0
- package/api/features/TaskHandlerResolver/index.d.ts +2 -0
- package/api/features/TaskHandlerResolver/index.js +2 -0
- package/api/features/TaskService/BackgroundTaskService.d.ts +42 -0
- package/api/features/TaskService/BackgroundTaskService.js +211 -0
- package/api/features/TaskService/BackgroundTaskService.js.map +1 -0
- package/api/features/TaskService/feature.d.ts +4 -0
- package/api/features/TaskService/feature.js +11 -0
- package/api/features/TaskService/feature.js.map +1 -0
- package/api/features/TaskService/index.d.ts +1 -0
- package/api/features/TaskService/index.js +1 -0
- package/api/features/TriggerTask/abstractions.d.ts +6 -0
- package/api/features/TriggerTask/abstractions.js.map +1 -1
- package/api/features/TriggerVisibleTask/TriggerVisibleTaskUseCase.d.ts +15 -0
- package/api/features/TriggerVisibleTask/TriggerVisibleTaskUseCase.js +26 -0
- package/api/features/TriggerVisibleTask/TriggerVisibleTaskUseCase.js.map +1 -0
- package/api/features/TriggerVisibleTask/abstractions.d.ts +17 -0
- package/api/features/TriggerVisibleTask/abstractions.js +5 -0
- package/api/features/TriggerVisibleTask/abstractions.js.map +1 -0
- package/api/features/TriggerVisibleTask/feature.d.ts +4 -0
- package/api/features/TriggerVisibleTask/feature.js +11 -0
- package/api/features/TriggerVisibleTask/feature.js.map +1 -0
- package/api/features/TriggerVisibleTask/index.d.ts +2 -0
- package/api/features/TriggerVisibleTask/index.js +2 -0
- package/api/features/UpdateBackgroundTaskSettings/UpdateBackgroundTaskSettingsRepository.js +3 -1
- package/api/features/UpdateBackgroundTaskSettings/UpdateBackgroundTaskSettingsRepository.js.map +1 -1
- package/api/features/UpdateTask/UpdateTaskUseCase.d.ts +17 -0
- package/api/features/UpdateTask/UpdateTaskUseCase.js +36 -0
- package/api/features/UpdateTask/UpdateTaskUseCase.js.map +1 -0
- package/api/features/UpdateTask/abstractions.d.ts +22 -0
- package/api/features/UpdateTask/abstractions.js +5 -0
- package/api/features/UpdateTask/abstractions.js.map +1 -0
- package/api/features/UpdateTask/feature.d.ts +4 -0
- package/api/features/UpdateTask/feature.js +11 -0
- package/api/features/UpdateTask/feature.js.map +1 -0
- package/api/features/UpdateTask/index.d.ts +2 -0
- package/api/features/UpdateTask/index.js +2 -0
- package/api/features/UpdateTaskLog/UpdateTaskLogUseCase.d.ts +12 -0
- package/api/features/UpdateTaskLog/UpdateTaskLogUseCase.js +19 -0
- package/api/features/UpdateTaskLog/UpdateTaskLogUseCase.js.map +1 -0
- package/api/features/UpdateTaskLog/abstractions.d.ts +16 -0
- package/api/features/UpdateTaskLog/abstractions.js +5 -0
- package/api/features/UpdateTaskLog/abstractions.js.map +1 -0
- package/api/features/UpdateTaskLog/feature.d.ts +4 -0
- package/api/features/UpdateTaskLog/feature.js +11 -0
- package/api/features/UpdateTaskLog/feature.js.map +1 -0
- package/api/features/UpdateTaskLog/index.d.ts +2 -0
- package/api/features/UpdateTaskLog/index.js +2 -0
- package/api/graphql/BackgroundTasksGraphQLSchema.d.ts +29 -0
- package/api/graphql/BackgroundTasksGraphQLSchema.js +462 -0
- package/api/graphql/BackgroundTasksGraphQLSchema.js.map +1 -0
- package/api/index.d.ts +11 -2
- package/api/index.js +11 -2
- package/api/runner/TaskControl.d.ts +35 -1
- package/api/runner/TaskControl.js +52 -42
- package/api/runner/TaskControl.js.map +1 -1
- package/api/runner/TaskManager.d.ts +12 -1
- package/api/runner/TaskManager.js +11 -10
- package/api/runner/TaskManager.js.map +1 -1
- package/api/runner/TaskManagerStore.d.ts +16 -0
- package/api/runner/TaskManagerStore.js +23 -7
- package/api/runner/TaskManagerStore.js.map +1 -1
- package/api/runner/TaskRunner.js +35 -4
- package/api/runner/TaskRunner.js.map +1 -1
- package/api/tasks/testingRunTask.d.ts +10 -2
- package/api/tasks/testingRunTask.js +10 -3
- package/api/tasks/testingRunTask.js.map +1 -1
- package/api/types.d.ts +1 -48
- package/api/types.js.map +1 -1
- package/package.json +28 -32
- package/testing/index.d.ts +3 -0
- package/testing/index.js +2 -0
- package/testing/mockTaskTriggerTransportPlugin.d.ts +2 -0
- package/testing/mockTaskTriggerTransportPlugin.js +18 -0
- package/testing/mockTaskTriggerTransportPlugin.js.map +1 -0
- package/testing/runner.d.ts +19 -0
- package/testing/runner.js +44 -0
- package/testing/runner.js.map +1 -0
- package/api/TasksCrud.d.ts +0 -10
- package/api/TasksCrud.js +0 -5
- package/api/TasksCrud.js.map +0 -1
- package/api/crud/cleanupTaskSubtree.d.ts +0 -8
- package/api/crud/cleanupTaskSubtree.js +0 -63
- package/api/crud/cleanupTaskSubtree.js.map +0 -1
- package/api/crud/crud.tasks.d.ts +0 -3
- package/api/crud/crud.tasks.js +0 -318
- package/api/crud/crud.tasks.js.map +0 -1
- package/api/crud/definition.tasks.d.ts +0 -3
- package/api/crud/definition.tasks.js +0 -17
- package/api/crud/definition.tasks.js.map +0 -1
- package/api/crud/service.tasks.d.ts +0 -3
- package/api/crud/service.tasks.js +0 -132
- package/api/crud/service.tasks.js.map +0 -1
- package/api/decorators/RunnableTaskDecorator.d.ts +0 -32
- package/api/decorators/RunnableTaskDecorator.js +0 -69
- package/api/decorators/RunnableTaskDecorator.js.map +0 -1
- package/api/decorators/SelfCleaningTaskDecorator.js +0 -84
- package/api/decorators/SelfCleaningTaskDecorator.js.map +0 -1
- package/api/features/GetTaskDefinition/GetTaskDefinitionUseCase.d.ts +0 -12
- package/api/features/GetTaskDefinition/GetTaskDefinitionUseCase.js +0 -27
- package/api/features/GetTaskDefinition/GetTaskDefinitionUseCase.js.map +0 -1
- package/api/features/GetTaskDefinition/abstractions.js +0 -5
- package/api/features/GetTaskDefinition/abstractions.js.map +0 -1
- package/api/features/GetTaskDefinition/feature.js +0 -11
- package/api/features/GetTaskDefinition/feature.js.map +0 -1
- package/api/features/GetTaskDefinition/index.d.ts +0 -2
- package/api/features/GetTaskDefinition/index.js +0 -2
- package/api/graphql/BackgroundTasksContextualSchema.d.ts +0 -21
- package/api/graphql/BackgroundTasksContextualSchema.js +0 -369
- package/api/graphql/BackgroundTasksContextualSchema.js.map +0 -1
- package/api/service/createService.d.ts +0 -6
- package/api/service/createService.js +0 -11
- package/api/service/createService.js.map +0 -1
- package/api/service/index.d.ts +0 -1
- package/api/service/index.js +0 -1
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { Result } from "@webiny/feature/api";
|
|
2
|
+
import { IdentityContext } from "@webiny/api-core/features/security/IdentityContext/index.js";
|
|
3
|
+
import { GetEntryByIdUseCase } from "@webiny/api-headless-cms/features/contentEntry/GetEntryById/index.js";
|
|
4
|
+
import { ListLatestEntriesUseCase } from "@webiny/api-headless-cms/features/contentEntry/ListEntries/index.js";
|
|
5
|
+
import { CreateEntryUseCase } from "@webiny/api-headless-cms/features/contentEntry/CreateEntry/index.js";
|
|
6
|
+
import { UpdateEntryUseCase } from "@webiny/api-headless-cms/features/contentEntry/UpdateEntry/index.js";
|
|
7
|
+
import { DeleteEntryUseCase } from "@webiny/api-headless-cms/features/contentEntry/DeleteEntry/index.js";
|
|
8
|
+
import { CmsWhereMapper } from "@webiny/api-headless-cms";
|
|
9
|
+
import { TaskModelProvider, TasksRepository } from "./abstractions.js";
|
|
10
|
+
import { entryToTask } from "./entryToTask.js";
|
|
11
|
+
import { createRevisionId } from "./createRevisionId.js";
|
|
12
|
+
import { BackgroundTaskPersistenceError, TaskNotFoundError } from "../errors.js";
|
|
13
|
+
import { TaskDataStatus } from "../../types.js";
|
|
14
|
+
const ENTRY_NOT_FOUND = "Cms/Entry/NotFound";
|
|
15
|
+
const toTaskError = (error)=>{
|
|
16
|
+
if (error.code === ENTRY_NOT_FOUND) return new TaskNotFoundError();
|
|
17
|
+
return BackgroundTaskPersistenceError.from(error);
|
|
18
|
+
};
|
|
19
|
+
class TasksRepositoryImpl {
|
|
20
|
+
constructor(identityContext, modelProvider, getEntryById, listLatestEntries, createEntry, updateEntry, deleteEntry, whereMapper){
|
|
21
|
+
this.identityContext = identityContext;
|
|
22
|
+
this.modelProvider = modelProvider;
|
|
23
|
+
this.getEntryById = getEntryById;
|
|
24
|
+
this.listLatestEntries = listLatestEntries;
|
|
25
|
+
this.createEntry = createEntry;
|
|
26
|
+
this.updateEntry = updateEntry;
|
|
27
|
+
this.deleteEntry = deleteEntry;
|
|
28
|
+
this.whereMapper = whereMapper;
|
|
29
|
+
}
|
|
30
|
+
async get(id) {
|
|
31
|
+
try {
|
|
32
|
+
const result = await this.identityContext.withoutAuthorization(async ()=>{
|
|
33
|
+
const model = await this.modelProvider.get();
|
|
34
|
+
return this.getEntryById.execute(model, createRevisionId(id));
|
|
35
|
+
});
|
|
36
|
+
if (result.isFail()) return Result.fail(toTaskError(result.error));
|
|
37
|
+
return Result.ok(entryToTask(result.value));
|
|
38
|
+
} catch (error) {
|
|
39
|
+
return Result.fail(BackgroundTaskPersistenceError.from(error));
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
async list(params) {
|
|
43
|
+
try {
|
|
44
|
+
const result = await this.identityContext.withoutAuthorization(async ()=>{
|
|
45
|
+
const model = await this.modelProvider.get();
|
|
46
|
+
return this.listLatestEntries.execute(model, {
|
|
47
|
+
...params,
|
|
48
|
+
where: this.whereMapper.map({
|
|
49
|
+
input: params?.where,
|
|
50
|
+
fields: model.fields
|
|
51
|
+
})
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
if (result.isFail()) return Result.fail(BackgroundTaskPersistenceError.from(result.error));
|
|
55
|
+
const { entries, meta } = result.value;
|
|
56
|
+
return Result.ok({
|
|
57
|
+
items: entries.map((entry)=>entryToTask(entry)),
|
|
58
|
+
meta
|
|
59
|
+
});
|
|
60
|
+
} catch (error) {
|
|
61
|
+
return Result.fail(BackgroundTaskPersistenceError.from(error));
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
async create(data) {
|
|
65
|
+
try {
|
|
66
|
+
const result = await this.identityContext.withoutAuthorization(async ()=>{
|
|
67
|
+
const model = await this.modelProvider.get();
|
|
68
|
+
return this.createEntry.execute(model, {
|
|
69
|
+
values: {
|
|
70
|
+
...data,
|
|
71
|
+
iterations: 0,
|
|
72
|
+
taskStatus: TaskDataStatus.PENDING
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
if (result.isFail()) return Result.fail(BackgroundTaskPersistenceError.from(result.error));
|
|
77
|
+
return Result.ok(entryToTask(result.value));
|
|
78
|
+
} catch (error) {
|
|
79
|
+
return Result.fail(BackgroundTaskPersistenceError.from(error));
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
async update(id, data) {
|
|
83
|
+
try {
|
|
84
|
+
const result = await this.identityContext.withoutAuthorization(async ()=>{
|
|
85
|
+
const model = await this.modelProvider.get();
|
|
86
|
+
return this.updateEntry.execute(model, createRevisionId(id), {
|
|
87
|
+
values: {
|
|
88
|
+
...data
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
if (result.isFail()) return Result.fail(toTaskError(result.error));
|
|
93
|
+
return Result.ok(entryToTask(result.value));
|
|
94
|
+
} catch (error) {
|
|
95
|
+
return Result.fail(BackgroundTaskPersistenceError.from(error));
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
async delete(id) {
|
|
99
|
+
try {
|
|
100
|
+
const result = await this.identityContext.withoutAuthorization(async ()=>{
|
|
101
|
+
const model = await this.modelProvider.get();
|
|
102
|
+
return this.deleteEntry.execute(model, createRevisionId(id));
|
|
103
|
+
});
|
|
104
|
+
if (result.isFail()) return Result.fail(toTaskError(result.error));
|
|
105
|
+
return Result.ok();
|
|
106
|
+
} catch (error) {
|
|
107
|
+
return Result.fail(BackgroundTaskPersistenceError.from(error));
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
const TasksRepository_TasksRepository = TasksRepository.createImplementation({
|
|
112
|
+
implementation: TasksRepositoryImpl,
|
|
113
|
+
dependencies: [
|
|
114
|
+
IdentityContext,
|
|
115
|
+
TaskModelProvider,
|
|
116
|
+
GetEntryByIdUseCase,
|
|
117
|
+
ListLatestEntriesUseCase,
|
|
118
|
+
CreateEntryUseCase,
|
|
119
|
+
UpdateEntryUseCase,
|
|
120
|
+
DeleteEntryUseCase,
|
|
121
|
+
CmsWhereMapper
|
|
122
|
+
]
|
|
123
|
+
});
|
|
124
|
+
export { TasksRepository_TasksRepository as TasksRepository };
|
|
125
|
+
|
|
126
|
+
//# sourceMappingURL=TasksRepository.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api/domain/task/TasksRepository.js","sources":["../../../../src/api/domain/task/TasksRepository.ts"],"sourcesContent":["import { Result } from \"@webiny/feature/api\";\nimport { IdentityContext } from \"@webiny/api-core/features/security/IdentityContext/index.js\";\nimport { GetEntryByIdUseCase } from \"@webiny/api-headless-cms/features/contentEntry/GetEntryById/index.js\";\nimport { ListLatestEntriesUseCase } from \"@webiny/api-headless-cms/features/contentEntry/ListEntries/index.js\";\nimport { CreateEntryUseCase } from \"@webiny/api-headless-cms/features/contentEntry/CreateEntry/index.js\";\nimport { UpdateEntryUseCase } from \"@webiny/api-headless-cms/features/contentEntry/UpdateEntry/index.js\";\nimport { DeleteEntryUseCase } from \"@webiny/api-headless-cms/features/contentEntry/DeleteEntry/index.js\";\nimport { CmsWhereMapper } from \"@webiny/api-headless-cms\";\nimport { TaskModelProvider, TasksRepository as Abstraction } from \"./abstractions.js\";\nimport { entryToTask } from \"./entryToTask.js\";\nimport { createRevisionId } from \"./createRevisionId.js\";\nimport { BackgroundTaskPersistenceError, TaskNotFoundError } from \"~/api/domain/errors.js\";\nimport { TaskDataStatus } from \"~/api/types.js\";\nimport type { IListTaskParams, ITaskCreateData, ITaskUpdateData } from \"~/api/types.js\";\nimport type { TaskService } from \"@webiny/api-core/features/task/TaskService/index.js\";\n\ntype TaskInput = TaskService.TaskInput;\ntype TaskOutput = TaskService.GenericOutput;\n\nconst ENTRY_NOT_FOUND = \"Cms/Entry/NotFound\";\n\nconst toTaskError = (error: {\n code?: string;\n}): TaskNotFoundError | BackgroundTaskPersistenceError => {\n if (error.code === ENTRY_NOT_FOUND) {\n return new TaskNotFoundError();\n }\n return BackgroundTaskPersistenceError.from(error);\n};\n\n/*\n * Tasks are stored as entries of a private CMS model. The calls run without authorization: who may\n * see or run a task is decided by the task use cases and the GraphQL layer, not by CMS permissions.\n */\nclass TasksRepositoryImpl implements Abstraction.Interface {\n constructor(\n private readonly identityContext: IdentityContext.Interface,\n private readonly modelProvider: TaskModelProvider.Interface,\n private readonly getEntryById: GetEntryByIdUseCase.Interface,\n private readonly listLatestEntries: ListLatestEntriesUseCase.Interface,\n private readonly createEntry: CreateEntryUseCase.Interface,\n private readonly updateEntry: UpdateEntryUseCase.Interface,\n private readonly deleteEntry: DeleteEntryUseCase.Interface,\n private readonly whereMapper: CmsWhereMapper.Interface\n ) {}\n\n async get<I extends TaskInput = TaskInput, O extends TaskOutput = TaskOutput>(id: string) {\n try {\n const result = await this.identityContext.withoutAuthorization(async () => {\n const model = await this.modelProvider.get();\n return this.getEntryById.execute(model, createRevisionId(id));\n });\n if (result.isFail()) {\n return Result.fail(toTaskError(result.error));\n }\n return Result.ok(entryToTask<I, O>(result.value));\n } catch (error) {\n return Result.fail(BackgroundTaskPersistenceError.from(error));\n }\n }\n\n async list<I extends TaskInput = TaskInput, O extends TaskOutput = TaskOutput>(\n params?: IListTaskParams\n ) {\n try {\n const result = await this.identityContext.withoutAuthorization(async () => {\n const model = await this.modelProvider.get();\n return this.listLatestEntries.execute(model, {\n ...params,\n where: this.whereMapper.map({\n input: params?.where,\n fields: model.fields\n })\n });\n });\n if (result.isFail()) {\n return Result.fail(BackgroundTaskPersistenceError.from(result.error));\n }\n const { entries, meta } = result.value;\n return Result.ok({\n items: entries.map(entry => entryToTask<I, O>(entry)),\n meta\n });\n } catch (error) {\n return Result.fail(BackgroundTaskPersistenceError.from(error));\n }\n }\n\n async create<I extends TaskInput = TaskInput>(data: ITaskCreateData<I>) {\n try {\n const result = await this.identityContext.withoutAuthorization(async () => {\n const model = await this.modelProvider.get();\n return this.createEntry.execute(model, {\n values: {\n ...data,\n iterations: 0,\n taskStatus: TaskDataStatus.PENDING\n }\n });\n });\n if (result.isFail()) {\n return Result.fail(BackgroundTaskPersistenceError.from(result.error));\n }\n return Result.ok(entryToTask<I>(result.value));\n } catch (error) {\n return Result.fail(BackgroundTaskPersistenceError.from(error));\n }\n }\n\n async update<I extends TaskInput = TaskInput, O extends TaskOutput = TaskOutput>(\n id: string,\n data: Partial<ITaskUpdateData<I, O>>\n ) {\n try {\n const result = await this.identityContext.withoutAuthorization(async () => {\n const model = await this.modelProvider.get();\n return this.updateEntry.execute(model, createRevisionId(id), {\n values: { ...data }\n });\n });\n if (result.isFail()) {\n return Result.fail(toTaskError(result.error));\n }\n return Result.ok(entryToTask<I, O>(result.value));\n } catch (error) {\n return Result.fail(BackgroundTaskPersistenceError.from(error));\n }\n }\n\n async delete(id: string) {\n try {\n const result = await this.identityContext.withoutAuthorization(async () => {\n const model = await this.modelProvider.get();\n return this.deleteEntry.execute(model, createRevisionId(id));\n });\n if (result.isFail()) {\n return Result.fail(toTaskError(result.error));\n }\n return Result.ok();\n } catch (error) {\n return Result.fail(BackgroundTaskPersistenceError.from(error));\n }\n }\n}\n\nexport const TasksRepository = Abstraction.createImplementation({\n implementation: TasksRepositoryImpl,\n dependencies: [\n IdentityContext,\n TaskModelProvider,\n GetEntryByIdUseCase,\n ListLatestEntriesUseCase,\n CreateEntryUseCase,\n UpdateEntryUseCase,\n DeleteEntryUseCase,\n CmsWhereMapper\n ]\n});\n"],"names":["ENTRY_NOT_FOUND","toTaskError","error","TaskNotFoundError","BackgroundTaskPersistenceError","TasksRepositoryImpl","identityContext","modelProvider","getEntryById","listLatestEntries","createEntry","updateEntry","deleteEntry","whereMapper","id","result","model","createRevisionId","Result","entryToTask","params","entries","meta","entry","data","TaskDataStatus","TasksRepository","Abstraction","IdentityContext","TaskModelProvider","GetEntryByIdUseCase","ListLatestEntriesUseCase","CreateEntryUseCase","UpdateEntryUseCase","DeleteEntryUseCase","CmsWhereMapper"],"mappings":"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"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { type Result } from "@webiny/feature/api";
|
|
2
|
+
import type { CmsModel } from "@webiny/api-headless-cms/types/index.js";
|
|
3
|
+
import type { TaskService } from "@webiny/api-core/features/task/TaskService/index.js";
|
|
4
|
+
import type { IListTaskLogParams, IListTaskLogsResponse, IListTaskParams, IListTasksResponse, ITaskCreateData, ITaskLog, ITaskLogCreateInput, ITaskLogUpdateInput, ITaskUpdateData } from "../../../api/types.js";
|
|
5
|
+
import type { BackgroundTaskPersistenceError, TaskLogNotFoundError, TaskNotFoundError } from "../../../api/domain/errors.js";
|
|
6
|
+
export interface ITaskModelProvider {
|
|
7
|
+
get(): Promise<CmsModel>;
|
|
8
|
+
}
|
|
9
|
+
/** The private CMS model that stores background tasks. */
|
|
10
|
+
export declare const TaskModelProvider: import("@webiny/di").Abstraction<ITaskModelProvider>;
|
|
11
|
+
export declare namespace TaskModelProvider {
|
|
12
|
+
type Interface = ITaskModelProvider;
|
|
13
|
+
}
|
|
14
|
+
export interface ITaskLogModelProvider {
|
|
15
|
+
get(): Promise<CmsModel>;
|
|
16
|
+
}
|
|
17
|
+
/** The private CMS model that stores background task logs. */
|
|
18
|
+
export declare const TaskLogModelProvider: import("@webiny/di").Abstraction<ITaskLogModelProvider>;
|
|
19
|
+
export declare namespace TaskLogModelProvider {
|
|
20
|
+
type Interface = ITaskLogModelProvider;
|
|
21
|
+
}
|
|
22
|
+
type TaskInput = TaskService.TaskInput;
|
|
23
|
+
type TaskOutput = TaskService.GenericOutput;
|
|
24
|
+
export interface ITasksRepository {
|
|
25
|
+
get<I extends TaskInput = TaskInput, O extends TaskOutput = TaskOutput>(id: string): Promise<Result<TaskService.Task<I, O>, TaskNotFoundError | BackgroundTaskPersistenceError>>;
|
|
26
|
+
list<I extends TaskInput = TaskInput, O extends TaskOutput = TaskOutput>(params?: IListTaskParams): Promise<Result<IListTasksResponse<I, O>, BackgroundTaskPersistenceError>>;
|
|
27
|
+
create<I extends TaskInput = TaskInput>(data: ITaskCreateData<I>): Promise<Result<TaskService.Task<I>, BackgroundTaskPersistenceError>>;
|
|
28
|
+
update<I extends TaskInput = TaskInput, O extends TaskOutput = TaskOutput>(id: string, data: Partial<ITaskUpdateData<I, O>>): Promise<Result<TaskService.Task<I, O>, TaskNotFoundError | BackgroundTaskPersistenceError>>;
|
|
29
|
+
delete(id: string): Promise<Result<void, TaskNotFoundError | BackgroundTaskPersistenceError>>;
|
|
30
|
+
}
|
|
31
|
+
/** Reads and writes background tasks. Publishes no events; the task use cases do that. */
|
|
32
|
+
export declare const TasksRepository: import("@webiny/di").Abstraction<ITasksRepository>;
|
|
33
|
+
export declare namespace TasksRepository {
|
|
34
|
+
type Interface = ITasksRepository;
|
|
35
|
+
}
|
|
36
|
+
type LogError = TaskLogNotFoundError | BackgroundTaskPersistenceError;
|
|
37
|
+
export interface ITaskLogsRepository {
|
|
38
|
+
create(task: Pick<TaskService.Task, "id">, data: ITaskLogCreateInput): Promise<Result<ITaskLog, BackgroundTaskPersistenceError>>;
|
|
39
|
+
update(id: string, data: ITaskLogUpdateInput): Promise<Result<ITaskLog, LogError>>;
|
|
40
|
+
delete(id: string): Promise<Result<void, LogError>>;
|
|
41
|
+
get(id: string): Promise<Result<ITaskLog, LogError>>;
|
|
42
|
+
getLatest(taskId: string): Promise<Result<ITaskLog, LogError>>;
|
|
43
|
+
list(params: IListTaskLogParams): Promise<Result<IListTaskLogsResponse, BackgroundTaskPersistenceError>>;
|
|
44
|
+
}
|
|
45
|
+
/** Reads and writes the logs of background tasks. */
|
|
46
|
+
export declare const TaskLogsRepository: import("@webiny/di").Abstraction<ITaskLogsRepository>;
|
|
47
|
+
export declare namespace TaskLogsRepository {
|
|
48
|
+
type Interface = ITaskLogsRepository;
|
|
49
|
+
}
|
|
50
|
+
export {};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { createAbstraction } from "@webiny/feature/api";
|
|
2
|
+
const TaskModelProvider = createAbstraction("Tasks/TaskModelProvider");
|
|
3
|
+
const TaskLogModelProvider = createAbstraction("Tasks/TaskLogModelProvider");
|
|
4
|
+
const TasksRepository = createAbstraction("Tasks/TasksRepository");
|
|
5
|
+
const TaskLogsRepository = createAbstraction("Tasks/TaskLogsRepository");
|
|
6
|
+
export { TaskLogModelProvider, TaskLogsRepository, TaskModelProvider, TasksRepository };
|
|
7
|
+
|
|
8
|
+
//# sourceMappingURL=abstractions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api/domain/task/abstractions.js","sources":["../../../../src/api/domain/task/abstractions.ts"],"sourcesContent":["import { createAbstraction, type Result } from \"@webiny/feature/api\";\nimport type { CmsModel } from \"@webiny/api-headless-cms/types/index.js\";\nimport type { TaskService } from \"@webiny/api-core/features/task/TaskService/index.js\";\nimport type {\n IListTaskLogParams,\n IListTaskLogsResponse,\n IListTaskParams,\n IListTasksResponse,\n ITaskCreateData,\n ITaskLog,\n ITaskLogCreateInput,\n ITaskLogUpdateInput,\n ITaskUpdateData\n} from \"~/api/types.js\";\nimport type {\n BackgroundTaskPersistenceError,\n TaskLogNotFoundError,\n TaskNotFoundError\n} from \"~/api/domain/errors.js\";\n\n// ============================================================================\n// Model providers\n// ============================================================================\n\nexport interface ITaskModelProvider {\n get(): Promise<CmsModel>;\n}\n\n/** The private CMS model that stores background tasks. */\nexport const TaskModelProvider = createAbstraction<ITaskModelProvider>(\"Tasks/TaskModelProvider\");\n\nexport namespace TaskModelProvider {\n export type Interface = ITaskModelProvider;\n}\n\nexport interface ITaskLogModelProvider {\n get(): Promise<CmsModel>;\n}\n\n/** The private CMS model that stores background task logs. */\nexport const TaskLogModelProvider = createAbstraction<ITaskLogModelProvider>(\n \"Tasks/TaskLogModelProvider\"\n);\n\nexport namespace TaskLogModelProvider {\n export type Interface = ITaskLogModelProvider;\n}\n\n// ============================================================================\n// Tasks repository\n// ============================================================================\n\ntype TaskInput = TaskService.TaskInput;\ntype TaskOutput = TaskService.GenericOutput;\n\nexport interface ITasksRepository {\n get<I extends TaskInput = TaskInput, O extends TaskOutput = TaskOutput>(\n id: string\n ): Promise<Result<TaskService.Task<I, O>, TaskNotFoundError | BackgroundTaskPersistenceError>>;\n list<I extends TaskInput = TaskInput, O extends TaskOutput = TaskOutput>(\n params?: IListTaskParams\n ): Promise<Result<IListTasksResponse<I, O>, BackgroundTaskPersistenceError>>;\n create<I extends TaskInput = TaskInput>(\n data: ITaskCreateData<I>\n ): Promise<Result<TaskService.Task<I>, BackgroundTaskPersistenceError>>;\n update<I extends TaskInput = TaskInput, O extends TaskOutput = TaskOutput>(\n id: string,\n data: Partial<ITaskUpdateData<I, O>>\n ): Promise<Result<TaskService.Task<I, O>, TaskNotFoundError | BackgroundTaskPersistenceError>>;\n delete(id: string): Promise<Result<void, TaskNotFoundError | BackgroundTaskPersistenceError>>;\n}\n\n/** Reads and writes background tasks. Publishes no events; the task use cases do that. */\nexport const TasksRepository = createAbstraction<ITasksRepository>(\"Tasks/TasksRepository\");\n\nexport namespace TasksRepository {\n export type Interface = ITasksRepository;\n}\n\n// ============================================================================\n// Task logs repository\n// ============================================================================\n\ntype LogError = TaskLogNotFoundError | BackgroundTaskPersistenceError;\n\nexport interface ITaskLogsRepository {\n create(\n task: Pick<TaskService.Task, \"id\">,\n data: ITaskLogCreateInput\n ): Promise<Result<ITaskLog, BackgroundTaskPersistenceError>>;\n update(id: string, data: ITaskLogUpdateInput): Promise<Result<ITaskLog, LogError>>;\n delete(id: string): Promise<Result<void, LogError>>;\n get(id: string): Promise<Result<ITaskLog, LogError>>;\n getLatest(taskId: string): Promise<Result<ITaskLog, LogError>>;\n list(\n params: IListTaskLogParams\n ): Promise<Result<IListTaskLogsResponse, BackgroundTaskPersistenceError>>;\n}\n\n/** Reads and writes the logs of background tasks. */\nexport const TaskLogsRepository = createAbstraction<ITaskLogsRepository>(\n \"Tasks/TaskLogsRepository\"\n);\n\nexport namespace TaskLogsRepository {\n export type Interface = ITaskLogsRepository;\n}\n"],"names":["TaskModelProvider","createAbstraction","TaskLogModelProvider","TasksRepository","TaskLogsRepository"],"mappings":";AA6BO,MAAMA,oBAAoBC,kBAAsC;AAWhE,MAAMC,uBAAuBD,kBAChC;AAgCG,MAAME,kBAAkBF,kBAAoC;AA2B5D,MAAMG,qBAAqBH,kBAC9B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const createRevisionId: (id: string) => string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api/domain/task/createRevisionId.js","sources":["../../../../src/api/domain/task/createRevisionId.ts"],"sourcesContent":["import { parseIdentifier } from \"@webiny/utils\";\n\n// Tasks and logs are never revised, so every entry lives in its first revision.\nexport const createRevisionId = (id: string): string => {\n const { id: entryId } = parseIdentifier(id);\n return `${entryId}#0001`;\n};\n"],"names":["createRevisionId","id","entryId","parseIdentifier"],"mappings":";AAGO,MAAMA,mBAAmB,CAACC;IAC7B,MAAM,EAAE,IAAIC,OAAO,EAAE,GAAGC,gBAAgBF;IACxC,OAAO,GAAGC,QAAQ,KAAK,CAAC;AAC5B"}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { CmsEntry } from "@webiny/api-headless-cms/types/index.js";
|
|
2
|
+
import type { TaskService } from "@webiny/api-core/features/task/TaskService/index.js";
|
|
3
|
+
export declare const entryToTask: <I extends TaskService.TaskInput = TaskService.TaskInput, O extends TaskService.GenericOutput = TaskService.GenericOutput>(entry: CmsEntry) => TaskService.Task<I, O>;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
const entryToTask = (entry)=>({
|
|
2
|
+
id: entry.entryId,
|
|
3
|
+
createdOn: entry.createdOn,
|
|
4
|
+
savedOn: entry.savedOn,
|
|
5
|
+
createdBy: entry.createdBy,
|
|
6
|
+
name: entry.values.name,
|
|
7
|
+
definitionId: entry.values.definitionId,
|
|
8
|
+
input: entry.values.input,
|
|
9
|
+
output: entry.values.output,
|
|
10
|
+
taskStatus: entry.values.taskStatus,
|
|
11
|
+
executionName: entry.values.executionName || "",
|
|
12
|
+
eventResponse: entry.values.eventResponse,
|
|
13
|
+
startedOn: entry.values.startedOn,
|
|
14
|
+
finishedOn: entry.values.finishedOn,
|
|
15
|
+
iterations: entry.values.iterations,
|
|
16
|
+
parentId: entry.values.parentId
|
|
17
|
+
});
|
|
18
|
+
export { entryToTask };
|
|
19
|
+
|
|
20
|
+
//# sourceMappingURL=entryToTask.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api/domain/task/entryToTask.js","sources":["../../../../src/api/domain/task/entryToTask.ts"],"sourcesContent":["import type { CmsEntry } from \"@webiny/api-headless-cms/types/index.js\";\nimport type { TaskService } from \"@webiny/api-core/features/task/TaskService/index.js\";\n\nexport const entryToTask = <\n I extends TaskService.TaskInput = TaskService.TaskInput,\n O extends TaskService.GenericOutput = TaskService.GenericOutput\n>(\n entry: CmsEntry\n): TaskService.Task<I, O> => {\n return {\n id: entry.entryId,\n createdOn: entry.createdOn,\n savedOn: entry.savedOn,\n createdBy: entry.createdBy,\n name: entry.values.name,\n definitionId: entry.values.definitionId,\n input: entry.values.input,\n output: entry.values.output,\n taskStatus: entry.values.taskStatus,\n executionName: entry.values.executionName || \"\",\n eventResponse: entry.values.eventResponse,\n startedOn: entry.values.startedOn,\n finishedOn: entry.values.finishedOn,\n iterations: entry.values.iterations,\n parentId: entry.values.parentId\n };\n};\n"],"names":["entryToTask","entry"],"mappings":"AAGO,MAAMA,cAAc,CAIvBC,QAEO;QACH,IAAIA,MAAM,OAAO;QACjB,WAAWA,MAAM,SAAS;QAC1B,SAASA,MAAM,OAAO;QACtB,WAAWA,MAAM,SAAS;QAC1B,MAAMA,MAAM,MAAM,CAAC,IAAI;QACvB,cAAcA,MAAM,MAAM,CAAC,YAAY;QACvC,OAAOA,MAAM,MAAM,CAAC,KAAK;QACzB,QAAQA,MAAM,MAAM,CAAC,MAAM;QAC3B,YAAYA,MAAM,MAAM,CAAC,UAAU;QACnC,eAAeA,MAAM,MAAM,CAAC,aAAa,IAAI;QAC7C,eAAeA,MAAM,MAAM,CAAC,aAAa;QACzC,WAAWA,MAAM,MAAM,CAAC,SAAS;QACjC,YAAYA,MAAM,MAAM,CAAC,UAAU;QACnC,YAAYA,MAAM,MAAM,CAAC,UAAU;QACnC,UAAUA,MAAM,MAAM,CAAC,QAAQ;IACnC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
const entryToTaskLog = (entry)=>({
|
|
2
|
+
id: entry.entryId,
|
|
3
|
+
createdOn: entry.createdOn,
|
|
4
|
+
createdBy: entry.createdBy,
|
|
5
|
+
executionName: entry.values.executionName,
|
|
6
|
+
task: entry.values.task,
|
|
7
|
+
iteration: entry.values.iteration,
|
|
8
|
+
items: entry.values.items || []
|
|
9
|
+
});
|
|
10
|
+
export { entryToTaskLog };
|
|
11
|
+
|
|
12
|
+
//# sourceMappingURL=entryToTaskLog.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api/domain/task/entryToTaskLog.js","sources":["../../../../src/api/domain/task/entryToTaskLog.ts"],"sourcesContent":["import type { CmsEntry } from \"@webiny/api-headless-cms/types/index.js\";\nimport type { ITaskLog } from \"~/api/types.js\";\n\nexport const entryToTaskLog = (entry: CmsEntry): ITaskLog => {\n return {\n id: entry.entryId,\n createdOn: entry.createdOn,\n createdBy: entry.createdBy,\n executionName: entry.values.executionName,\n task: entry.values.task,\n iteration: entry.values.iteration,\n items: entry.values.items || []\n };\n};\n"],"names":["entryToTaskLog","entry"],"mappings":"AAGO,MAAMA,iBAAiB,CAACC,QACpB;QACH,IAAIA,MAAM,OAAO;QACjB,WAAWA,MAAM,SAAS;QAC1B,WAAWA,MAAM,SAAS;QAC1B,eAAeA,MAAM,MAAM,CAAC,aAAa;QACzC,MAAMA,MAAM,MAAM,CAAC,IAAI;QACvB,WAAWA,MAAM,MAAM,CAAC,SAAS;QACjC,OAAOA,MAAM,MAAM,CAAC,KAAK,IAAI,EAAE;IACnC"}
|
|
@@ -2,6 +2,8 @@ import { Result } from "@webiny/feature/api";
|
|
|
2
2
|
import { TaskService } from "@webiny/api-core/features/task/TaskService/index.js";
|
|
3
3
|
import { TaskDefinition } from "@webiny/api-core/features/task/TaskDefinition/index.js";
|
|
4
4
|
import { TaskDefinitionNotFoundError, TaskNotFoundError, TaskAbortError } from "../../../api/domain/errors.js";
|
|
5
|
+
import { TaskLogNotFoundError } from "../../../api/domain/errors.js";
|
|
6
|
+
import { BackgroundTaskPersistenceError } from "../../../api/domain/errors.js";
|
|
5
7
|
export interface IAbortTaskUseCase {
|
|
6
8
|
execute<I extends TaskDefinition.TaskInput = TaskDefinition.TaskInput, O extends TaskService.GenericOutput = TaskService.GenericOutput>(params: AbortTaskParams): Promise<Result<TaskService.Task<I, O>, UseCaseError>>;
|
|
7
9
|
}
|
|
@@ -13,6 +15,8 @@ export interface IAbortTaskUseCaseErrors {
|
|
|
13
15
|
definitionNotFound: TaskDefinitionNotFoundError;
|
|
14
16
|
taskNotFound: TaskNotFoundError;
|
|
15
17
|
abortError: TaskAbortError;
|
|
18
|
+
logNotFound: TaskLogNotFoundError;
|
|
19
|
+
persistence: BackgroundTaskPersistenceError;
|
|
16
20
|
}
|
|
17
21
|
type UseCaseError = IAbortTaskUseCaseErrors[keyof IAbortTaskUseCaseErrors];
|
|
18
22
|
export declare const AbortTaskUseCase: import("@webiny/di").Abstraction<IAbortTaskUseCase>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"api/features/AbortTask/abstractions.js","sources":["../../../../src/api/features/AbortTask/abstractions.ts"],"sourcesContent":["import { createAbstraction, Result } from \"@webiny/feature/api\";\nimport { TaskService } from \"@webiny/api-core/features/task/TaskService/index.js\";\nimport { TaskDefinition } from \"@webiny/api-core/features/task/TaskDefinition/index.js\";\nimport {\n TaskDefinitionNotFoundError,\n TaskNotFoundError,\n TaskAbortError\n} from \"~/api/domain/errors.js\";\n\nexport interface IAbortTaskUseCase {\n execute<\n I extends TaskDefinition.TaskInput = TaskDefinition.TaskInput,\n O extends TaskService.GenericOutput = TaskService.GenericOutput\n >(\n params: AbortTaskParams\n ): Promise<Result<TaskService.Task<I, O>, UseCaseError>>;\n}\n\nexport interface AbortTaskParams {\n id: string;\n message?: string;\n}\n\nexport interface IAbortTaskUseCaseErrors {\n definitionNotFound: TaskDefinitionNotFoundError;\n taskNotFound: TaskNotFoundError;\n abortError: TaskAbortError;\n}\n\ntype UseCaseError = IAbortTaskUseCaseErrors[keyof IAbortTaskUseCaseErrors];\n\nexport const AbortTaskUseCase = createAbstraction<IAbortTaskUseCase>(\"Tasks/AbortTaskUseCase\");\n\nexport namespace AbortTaskUseCase {\n export type Interface = IAbortTaskUseCase;\n export type Params = AbortTaskParams;\n\n export type Error = UseCaseError;\n export type Return<\n I extends TaskDefinition.TaskInput = TaskDefinition.TaskInput,\n O extends TaskService.GenericOutput = TaskService.GenericOutput\n > = Promise<Result<TaskService.Task<I, O>, UseCaseError>>;\n}\n"],"names":["AbortTaskUseCase","createAbstraction"],"mappings":";
|
|
1
|
+
{"version":3,"file":"api/features/AbortTask/abstractions.js","sources":["../../../../src/api/features/AbortTask/abstractions.ts"],"sourcesContent":["import { createAbstraction, Result } from \"@webiny/feature/api\";\nimport { TaskService } from \"@webiny/api-core/features/task/TaskService/index.js\";\nimport { TaskDefinition } from \"@webiny/api-core/features/task/TaskDefinition/index.js\";\nimport {\n TaskDefinitionNotFoundError,\n TaskNotFoundError,\n TaskAbortError\n} from \"~/api/domain/errors.js\";\nimport { TaskLogNotFoundError } from \"~/api/domain/errors.js\";\nimport { BackgroundTaskPersistenceError } from \"~/api/domain/errors.js\";\n\nexport interface IAbortTaskUseCase {\n execute<\n I extends TaskDefinition.TaskInput = TaskDefinition.TaskInput,\n O extends TaskService.GenericOutput = TaskService.GenericOutput\n >(\n params: AbortTaskParams\n ): Promise<Result<TaskService.Task<I, O>, UseCaseError>>;\n}\n\nexport interface AbortTaskParams {\n id: string;\n message?: string;\n}\n\nexport interface IAbortTaskUseCaseErrors {\n definitionNotFound: TaskDefinitionNotFoundError;\n taskNotFound: TaskNotFoundError;\n abortError: TaskAbortError;\n logNotFound: TaskLogNotFoundError;\n persistence: BackgroundTaskPersistenceError;\n}\n\ntype UseCaseError = IAbortTaskUseCaseErrors[keyof IAbortTaskUseCaseErrors];\n\nexport const AbortTaskUseCase = createAbstraction<IAbortTaskUseCase>(\"Tasks/AbortTaskUseCase\");\n\nexport namespace AbortTaskUseCase {\n export type Interface = IAbortTaskUseCase;\n export type Params = AbortTaskParams;\n\n export type Error = UseCaseError;\n export type Return<\n I extends TaskDefinition.TaskInput = TaskDefinition.TaskInput,\n O extends TaskService.GenericOutput = TaskService.GenericOutput\n > = Promise<Result<TaskService.Task<I, O>, UseCaseError>>;\n}\n"],"names":["AbortTaskUseCase","createAbstraction"],"mappings":";AAmCO,MAAMA,mBAAmBC,kBAAqC"}
|
|
@@ -1,7 +1,27 @@
|
|
|
1
|
+
import { Logger } from "@webiny/api-core/features/logger/index.js";
|
|
1
2
|
import { CleanupTaskSubtreeUseCase as UseCaseAbstraction } from "./abstractions.js";
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
import { TaskLogsRepository, TasksRepository } from "../../../api/domain/task/abstractions.js";
|
|
4
|
+
import { DeleteTaskUseCase } from "../../../api/features/DeleteTask/index.js";
|
|
5
|
+
/**
|
|
6
|
+
* Deletes the task identified by `rootId`, all its descendants, and their logs. The logs are listed
|
|
7
|
+
* by task id whatever the owning definition says (`databaseLogs`, or no registered definition): a
|
|
8
|
+
* self-cleaning task writes none of its own, but an abort still writes one log row, and cleanup
|
|
9
|
+
* removes whatever exists. Bottom-up and best-effort: a record that fails to delete is logged and
|
|
10
|
+
* skipped, and the use case never throws.
|
|
11
|
+
*/
|
|
12
|
+
declare class CleanupTaskSubtreeUseCaseImpl implements UseCaseAbstraction.Interface {
|
|
13
|
+
private readonly tasks;
|
|
14
|
+
private readonly logs;
|
|
15
|
+
private readonly deleteTask;
|
|
16
|
+
private readonly logger;
|
|
17
|
+
constructor(tasks: TasksRepository.Interface, logs: TaskLogsRepository.Interface, deleteTask: DeleteTaskUseCase.Interface, logger: Logger.Interface);
|
|
18
|
+
execute(rootId: string): Promise<void>;
|
|
19
|
+
private collectSubtree;
|
|
20
|
+
private listChildren;
|
|
21
|
+
private listLogs;
|
|
22
|
+
private deleteTaskLogs;
|
|
7
23
|
}
|
|
24
|
+
export declare const CleanupTaskSubtreeUseCase: typeof CleanupTaskSubtreeUseCaseImpl & {
|
|
25
|
+
__abstraction: import("@webiny/di").Abstraction<import("./abstractions.js").ICleanupTaskSubtreeUseCase>;
|
|
26
|
+
};
|
|
27
|
+
export {};
|
|
@@ -1,11 +1,117 @@
|
|
|
1
|
+
import { Logger } from "@webiny/api-core/features/logger/index.js";
|
|
2
|
+
import { CleanupTaskSubtreeUseCase } from "./abstractions.js";
|
|
3
|
+
import { TaskLogsRepository, TasksRepository } from "../../domain/task/abstractions.js";
|
|
4
|
+
import { DeleteTaskUseCase } from "../DeleteTask/index.js";
|
|
5
|
+
const PAGE_SIZE = 100;
|
|
1
6
|
class CleanupTaskSubtreeUseCaseImpl {
|
|
2
|
-
constructor(
|
|
3
|
-
this.
|
|
7
|
+
constructor(tasks, logs, deleteTask, logger){
|
|
8
|
+
this.tasks = tasks;
|
|
9
|
+
this.logs = logs;
|
|
10
|
+
this.deleteTask = deleteTask;
|
|
11
|
+
this.logger = logger;
|
|
4
12
|
}
|
|
5
|
-
async execute(
|
|
6
|
-
|
|
13
|
+
async execute(rootId) {
|
|
14
|
+
let ordered;
|
|
15
|
+
try {
|
|
16
|
+
ordered = await this.collectSubtree(rootId);
|
|
17
|
+
} catch (error) {
|
|
18
|
+
this.logger.warn({
|
|
19
|
+
error
|
|
20
|
+
}, `cleanupTaskSubtree: failed to collect the subtree of task "${rootId}", skipping.`);
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
for (const task of ordered){
|
|
24
|
+
await this.deleteTaskLogs(task);
|
|
25
|
+
const result = await this.deleteTask.execute(task.id);
|
|
26
|
+
if (result.isFail()) this.logger.warn({
|
|
27
|
+
error: result.error
|
|
28
|
+
}, `cleanupTaskSubtree: failed to delete task "${task.id}".`);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
async collectSubtree(rootId) {
|
|
32
|
+
const root = await this.tasks.get(rootId);
|
|
33
|
+
if (root.isFail()) return [];
|
|
34
|
+
const order = [
|
|
35
|
+
root.value
|
|
36
|
+
];
|
|
37
|
+
const seen = new Set([
|
|
38
|
+
root.value.id
|
|
39
|
+
]);
|
|
40
|
+
let i = 0;
|
|
41
|
+
while(i < order.length){
|
|
42
|
+
const current = order[i++];
|
|
43
|
+
for (const child of (await this.listChildren(current.id)))if (!seen.has(child.id)) {
|
|
44
|
+
seen.add(child.id);
|
|
45
|
+
order.push(child);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return order.reverse();
|
|
49
|
+
}
|
|
50
|
+
async listChildren(parentId) {
|
|
51
|
+
const children = [];
|
|
52
|
+
let after = null;
|
|
53
|
+
do {
|
|
54
|
+
const params = {
|
|
55
|
+
where: {
|
|
56
|
+
parentId
|
|
57
|
+
},
|
|
58
|
+
limit: PAGE_SIZE,
|
|
59
|
+
after
|
|
60
|
+
};
|
|
61
|
+
const result = await this.tasks.list(params);
|
|
62
|
+
if (result.isFail()) throw result.error;
|
|
63
|
+
const { items, meta } = result.value;
|
|
64
|
+
children.push(...items);
|
|
65
|
+
after = meta.hasMoreItems ? meta.cursor : null;
|
|
66
|
+
}while (after)
|
|
67
|
+
return children;
|
|
68
|
+
}
|
|
69
|
+
async listLogs(taskId) {
|
|
70
|
+
const logs = [];
|
|
71
|
+
let after = null;
|
|
72
|
+
do {
|
|
73
|
+
const params = {
|
|
74
|
+
where: {
|
|
75
|
+
task: taskId
|
|
76
|
+
},
|
|
77
|
+
limit: PAGE_SIZE,
|
|
78
|
+
after
|
|
79
|
+
};
|
|
80
|
+
const result = await this.logs.list(params);
|
|
81
|
+
if (result.isFail()) throw result.error;
|
|
82
|
+
const { items, meta } = result.value;
|
|
83
|
+
logs.push(...items);
|
|
84
|
+
after = meta.hasMoreItems ? meta.cursor : null;
|
|
85
|
+
}while (after)
|
|
86
|
+
return logs;
|
|
87
|
+
}
|
|
88
|
+
async deleteTaskLogs(task) {
|
|
89
|
+
let logs;
|
|
90
|
+
try {
|
|
91
|
+
logs = await this.listLogs(task.id);
|
|
92
|
+
} catch (error) {
|
|
93
|
+
this.logger.warn({
|
|
94
|
+
error
|
|
95
|
+
}, `cleanupTaskSubtree: failed to list logs for task "${task.id}".`);
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
for (const log of logs){
|
|
99
|
+
const result = await this.logs.delete(log.id);
|
|
100
|
+
if (result.isFail()) this.logger.warn({
|
|
101
|
+
error: result.error
|
|
102
|
+
}, `cleanupTaskSubtree: failed to delete log "${log.id}" for task "${task.id}".`);
|
|
103
|
+
}
|
|
7
104
|
}
|
|
8
105
|
}
|
|
9
|
-
|
|
106
|
+
const CleanupTaskSubtreeUseCase_CleanupTaskSubtreeUseCase = CleanupTaskSubtreeUseCase.createImplementation({
|
|
107
|
+
implementation: CleanupTaskSubtreeUseCaseImpl,
|
|
108
|
+
dependencies: [
|
|
109
|
+
TasksRepository,
|
|
110
|
+
TaskLogsRepository,
|
|
111
|
+
DeleteTaskUseCase,
|
|
112
|
+
Logger
|
|
113
|
+
]
|
|
114
|
+
});
|
|
115
|
+
export { CleanupTaskSubtreeUseCase_CleanupTaskSubtreeUseCase as CleanupTaskSubtreeUseCase };
|
|
10
116
|
|
|
11
117
|
//# sourceMappingURL=CleanupTaskSubtreeUseCase.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"api/features/CleanupTaskSubtree/CleanupTaskSubtreeUseCase.js","sources":["../../../../src/api/features/CleanupTaskSubtree/CleanupTaskSubtreeUseCase.ts"],"sourcesContent":["import { CleanupTaskSubtreeUseCase as UseCaseAbstraction } from \"./abstractions.js\";\nimport type {
|
|
1
|
+
{"version":3,"file":"api/features/CleanupTaskSubtree/CleanupTaskSubtreeUseCase.js","sources":["../../../../src/api/features/CleanupTaskSubtree/CleanupTaskSubtreeUseCase.ts"],"sourcesContent":["import { Logger } from \"@webiny/api-core/features/logger/index.js\";\nimport { CleanupTaskSubtreeUseCase as UseCaseAbstraction } from \"./abstractions.js\";\nimport { TaskLogsRepository, TasksRepository } from \"~/api/domain/task/abstractions.js\";\nimport { DeleteTaskUseCase } from \"~/api/features/DeleteTask/index.js\";\nimport type { ITask } from \"~/api/types.js\";\nimport type { ITaskLog } from \"~/api/types.js\";\nimport type { IListTaskParams } from \"~/api/types.js\";\nimport type { IListTaskLogParams } from \"~/api/types.js\";\n\nconst PAGE_SIZE = 100;\n\n/**\n * Deletes the task identified by `rootId`, all its descendants, and their logs. The logs are listed\n * by task id whatever the owning definition says (`databaseLogs`, or no registered definition): a\n * self-cleaning task writes none of its own, but an abort still writes one log row, and cleanup\n * removes whatever exists. Bottom-up and best-effort: a record that fails to delete is logged and\n * skipped, and the use case never throws.\n */\nclass CleanupTaskSubtreeUseCaseImpl implements UseCaseAbstraction.Interface {\n public constructor(\n private readonly tasks: TasksRepository.Interface,\n private readonly logs: TaskLogsRepository.Interface,\n private readonly deleteTask: DeleteTaskUseCase.Interface,\n private readonly logger: Logger.Interface\n ) {}\n\n public async execute(rootId: string): Promise<void> {\n let ordered: ITask[];\n try {\n ordered = await this.collectSubtree(rootId);\n } catch (error) {\n // Without the full subtree, deleting anything could orphan the tasks we didn't find.\n this.logger.warn(\n { error },\n `cleanupTaskSubtree: failed to collect the subtree of task \"${rootId}\", skipping.`\n );\n return;\n }\n for (const task of ordered) {\n await this.deleteTaskLogs(task);\n const result = await this.deleteTask.execute(task.id);\n if (result.isFail()) {\n this.logger.warn(\n { error: result.error },\n `cleanupTaskSubtree: failed to delete task \"${task.id}\".`\n );\n }\n }\n }\n\n // Breadth-first from the root, then reversed, so children are deleted before their parents.\n private async collectSubtree(rootId: string): Promise<ITask[]> {\n const root = await this.tasks.get(rootId);\n if (root.isFail()) {\n return [];\n }\n const order: ITask[] = [root.value];\n const seen = new Set<string>([root.value.id]);\n let i = 0;\n while (i < order.length) {\n const current = order[i++];\n for (const child of await this.listChildren(current.id)) {\n if (seen.has(child.id)) {\n continue;\n }\n seen.add(child.id);\n order.push(child);\n }\n }\n return order.reverse();\n }\n\n // Every page, not just the first: a parent can have more children than one page holds.\n private async listChildren(parentId: string): Promise<ITask[]> {\n const children: ITask[] = [];\n let after: string | null = null;\n do {\n const params: IListTaskParams = { where: { parentId }, limit: PAGE_SIZE, after };\n const result = await this.tasks.list(params);\n if (result.isFail()) {\n throw result.error;\n }\n const { items, meta } = result.value;\n children.push(...items);\n after = meta.hasMoreItems ? meta.cursor : null;\n } while (after);\n return children;\n }\n\n // All pages are read before anything is deleted, so deleting doesn't shift what a cursor sees.\n private async listLogs(taskId: string): Promise<ITaskLog[]> {\n const logs: ITaskLog[] = [];\n let after: string | null = null;\n do {\n const params: IListTaskLogParams = { where: { task: taskId }, limit: PAGE_SIZE, after };\n const result = await this.logs.list(params);\n if (result.isFail()) {\n throw result.error;\n }\n const { items, meta } = result.value;\n logs.push(...items);\n after = meta.hasMoreItems ? meta.cursor : null;\n } while (after);\n return logs;\n }\n\n private async deleteTaskLogs(task: ITask): Promise<void> {\n let logs: ITaskLog[];\n try {\n logs = await this.listLogs(task.id);\n } catch (error) {\n this.logger.warn(\n { error },\n `cleanupTaskSubtree: failed to list logs for task \"${task.id}\".`\n );\n return;\n }\n for (const log of logs) {\n const result = await this.logs.delete(log.id);\n if (result.isFail()) {\n this.logger.warn(\n { error: result.error },\n `cleanupTaskSubtree: failed to delete log \"${log.id}\" for task \"${task.id}\".`\n );\n }\n }\n }\n}\n\nexport const CleanupTaskSubtreeUseCase = UseCaseAbstraction.createImplementation({\n implementation: CleanupTaskSubtreeUseCaseImpl,\n dependencies: [TasksRepository, TaskLogsRepository, DeleteTaskUseCase, Logger]\n});\n"],"names":["PAGE_SIZE","CleanupTaskSubtreeUseCaseImpl","tasks","logs","deleteTask","logger","rootId","ordered","error","task","result","root","order","seen","Set","i","current","child","parentId","children","after","params","items","meta","taskId","log","CleanupTaskSubtreeUseCase","UseCaseAbstraction","TasksRepository","TaskLogsRepository","DeleteTaskUseCase","Logger"],"mappings":"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"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { createFeature } from "@webiny/feature/api";
|
|
2
|
+
import { CleanupTaskSubtreeUseCase } from "./CleanupTaskSubtreeUseCase.js";
|
|
3
|
+
const CleanupTaskSubtreeFeature = createFeature({
|
|
4
|
+
name: "CleanupTaskSubtree",
|
|
5
|
+
register (container) {
|
|
6
|
+
container.register(CleanupTaskSubtreeUseCase);
|
|
7
|
+
}
|
|
8
|
+
});
|
|
9
|
+
export { CleanupTaskSubtreeFeature };
|
|
10
|
+
|
|
11
|
+
//# sourceMappingURL=feature.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api/features/CleanupTaskSubtree/feature.js","sources":["../../../../src/api/features/CleanupTaskSubtree/feature.ts"],"sourcesContent":["import { createFeature } from \"@webiny/feature/api\";\nimport { CleanupTaskSubtreeUseCase } from \"./CleanupTaskSubtreeUseCase.js\";\n\nexport const CleanupTaskSubtreeFeature = createFeature({\n name: \"CleanupTaskSubtree\",\n register(container) {\n container.register(CleanupTaskSubtreeUseCase);\n }\n});\n"],"names":["CleanupTaskSubtreeFeature","createFeature","container","CleanupTaskSubtreeUseCase"],"mappings":";;AAGO,MAAMA,4BAA4BC,cAAc;IACnD,MAAM;IACN,UAASC,SAAS;QACdA,UAAU,QAAQ,CAACC;IACvB;AACJ"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export { CleanupTaskSubtreeUseCase } from "./abstractions.js";
|
|
2
|
-
export {
|
|
2
|
+
export { CleanupTaskSubtreeFeature } from "./feature.js";
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export { CleanupTaskSubtreeUseCase } from "./abstractions.js";
|
|
2
|
-
export {
|
|
2
|
+
export { CleanupTaskSubtreeFeature } from "./feature.js";
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Result } from "@webiny/feature/api";
|
|
2
|
+
import { EventPublisher } from "@webiny/api-core/features/eventPublisher/index.js";
|
|
3
|
+
import type { TaskService } from "@webiny/api-core/features/task/TaskService/index.js";
|
|
4
|
+
import { CreateTaskUseCase as UseCaseAbstraction } from "./abstractions.js";
|
|
5
|
+
import { GetRunnableTaskDefinitionUseCase } from "../../../api/features/GetRunnableTaskDefinition/index.js";
|
|
6
|
+
import { TasksRepository } from "../../../api/domain/task/abstractions.js";
|
|
7
|
+
declare class CreateTaskUseCaseImpl implements UseCaseAbstraction.Interface {
|
|
8
|
+
private readonly getDefinition;
|
|
9
|
+
private readonly eventPublisher;
|
|
10
|
+
private readonly repository;
|
|
11
|
+
constructor(getDefinition: GetRunnableTaskDefinitionUseCase.Interface, eventPublisher: EventPublisher.Interface, repository: TasksRepository.Interface);
|
|
12
|
+
execute<I extends TaskService.TaskInput = TaskService.TaskInput>(data: UseCaseAbstraction.Params<I>): Promise<Result<TaskService.Task<I>, UseCaseAbstraction.Error>>;
|
|
13
|
+
}
|
|
14
|
+
export declare const CreateTaskUseCase: typeof CreateTaskUseCaseImpl & {
|
|
15
|
+
__abstraction: import("@webiny/di").Abstraction<import("./abstractions.js").ICreateTaskUseCase>;
|
|
16
|
+
};
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import zod from "zod";
|
|
2
|
+
import { Result } from "@webiny/feature/api";
|
|
3
|
+
import { EventPublisher } from "@webiny/api-core/features/eventPublisher/index.js";
|
|
4
|
+
import { CreateTaskUseCase } from "./abstractions.js";
|
|
5
|
+
import { GetRunnableTaskDefinitionUseCase } from "../GetRunnableTaskDefinition/index.js";
|
|
6
|
+
import { TasksRepository } from "../../domain/task/abstractions.js";
|
|
7
|
+
import { TaskInputValidationError } from "../../domain/errors.js";
|
|
8
|
+
import { TaskAfterCreateEvent, TaskBeforeCreateEvent } from "../../events/index.js";
|
|
9
|
+
const toZodSchema = (schema)=>{
|
|
10
|
+
if (!schema) return zod.looseObject({});
|
|
11
|
+
else if (schema instanceof zod.ZodObject) return schema.loose();
|
|
12
|
+
else if (schema instanceof zod.ZodType) return schema;
|
|
13
|
+
return zod.looseObject(schema);
|
|
14
|
+
};
|
|
15
|
+
const validateInput = async (definition, input)=>{
|
|
16
|
+
if (!definition.createInputValidation) return null;
|
|
17
|
+
const schema = toZodSchema(definition.createInputValidation({
|
|
18
|
+
validator: zod
|
|
19
|
+
}));
|
|
20
|
+
const result = await schema.safeParseAsync(input);
|
|
21
|
+
return result.success ? null : result.error;
|
|
22
|
+
};
|
|
23
|
+
class CreateTaskUseCaseImpl {
|
|
24
|
+
constructor(getDefinition, eventPublisher, repository){
|
|
25
|
+
this.getDefinition = getDefinition;
|
|
26
|
+
this.eventPublisher = eventPublisher;
|
|
27
|
+
this.repository = repository;
|
|
28
|
+
}
|
|
29
|
+
async execute(data) {
|
|
30
|
+
const definition = this.getDefinition.execute(data.definitionId);
|
|
31
|
+
if (definition.isFail()) return Result.fail(definition.error);
|
|
32
|
+
const invalid = await validateInput(definition.value, data.input);
|
|
33
|
+
if (invalid) return Result.fail(new TaskInputValidationError(invalid));
|
|
34
|
+
await this.eventPublisher.publish(new TaskBeforeCreateEvent({
|
|
35
|
+
input: data
|
|
36
|
+
}));
|
|
37
|
+
const result = await this.repository.create(data);
|
|
38
|
+
if (result.isFail()) return Result.fail(result.error);
|
|
39
|
+
await this.eventPublisher.publish(new TaskAfterCreateEvent({
|
|
40
|
+
input: data,
|
|
41
|
+
task: result.value
|
|
42
|
+
}));
|
|
43
|
+
return Result.ok(result.value);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
const CreateTaskUseCase_CreateTaskUseCase = CreateTaskUseCase.createImplementation({
|
|
47
|
+
implementation: CreateTaskUseCaseImpl,
|
|
48
|
+
dependencies: [
|
|
49
|
+
GetRunnableTaskDefinitionUseCase,
|
|
50
|
+
EventPublisher,
|
|
51
|
+
TasksRepository
|
|
52
|
+
]
|
|
53
|
+
});
|
|
54
|
+
export { CreateTaskUseCase_CreateTaskUseCase as CreateTaskUseCase };
|
|
55
|
+
|
|
56
|
+
//# sourceMappingURL=CreateTaskUseCase.js.map
|