2023-11-08 07:29:39 -08:00
|
|
|
import Container from 'typedi';
|
2024-08-27 07:44:32 -07:00
|
|
|
import type { ExecutionData } from '@/databases/entities/execution-data';
|
|
|
|
import type { ExecutionEntity } from '@db/entities/execution-entity';
|
|
|
|
import type { WorkflowEntity } from '@db/entities/workflow-entity';
|
2023-11-10 06:04:26 -08:00
|
|
|
import { ExecutionRepository } from '@db/repositories/execution.repository';
|
2024-08-27 07:44:32 -07:00
|
|
|
import { ExecutionDataRepository } from '@/databases/repositories/execution-data.repository';
|
|
|
|
import { ExecutionMetadataRepository } from '@/databases/repositories/execution-metadata.repository';
|
2023-11-08 07:29:39 -08:00
|
|
|
|
|
|
|
export async function createManyExecutions(
|
|
|
|
amount: number,
|
|
|
|
workflow: WorkflowEntity,
|
|
|
|
callback: (workflow: WorkflowEntity) => Promise<ExecutionEntity>,
|
|
|
|
) {
|
2024-01-17 07:08:50 -08:00
|
|
|
const executionsRequests = [...Array(amount)].map(async (_) => await callback(workflow));
|
|
|
|
return await Promise.all(executionsRequests);
|
2023-11-08 07:29:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a execution in the DB and assign it to a workflow.
|
|
|
|
*/
|
|
|
|
export async function createExecution(
|
2024-05-22 08:20:01 -07:00
|
|
|
attributes: Partial<
|
|
|
|
Omit<ExecutionEntity, 'metadata'> &
|
|
|
|
ExecutionData & { metadata: Array<{ key: string; value: string }> }
|
|
|
|
>,
|
2023-11-08 07:29:39 -08:00
|
|
|
workflow: WorkflowEntity,
|
|
|
|
) {
|
2024-05-22 08:20:01 -07:00
|
|
|
const { data, finished, mode, startedAt, stoppedAt, waitTill, status, deletedAt, metadata } =
|
|
|
|
attributes;
|
2023-11-08 07:29:39 -08:00
|
|
|
|
|
|
|
const execution = await Container.get(ExecutionRepository).save({
|
|
|
|
finished: finished ?? true,
|
|
|
|
mode: mode ?? 'manual',
|
|
|
|
startedAt: startedAt ?? new Date(),
|
|
|
|
...(workflow !== undefined && { workflowId: workflow.id }),
|
|
|
|
stoppedAt: stoppedAt ?? new Date(),
|
|
|
|
waitTill: waitTill ?? null,
|
2024-05-22 07:56:05 -07:00
|
|
|
status: status ?? 'success',
|
2023-11-08 07:29:39 -08:00
|
|
|
deletedAt,
|
|
|
|
});
|
|
|
|
|
2024-05-22 08:20:01 -07:00
|
|
|
if (metadata?.length) {
|
|
|
|
const metadataToSave = metadata.map(({ key, value }) => ({
|
|
|
|
key,
|
|
|
|
value,
|
|
|
|
execution: { id: execution.id },
|
|
|
|
}));
|
|
|
|
|
|
|
|
await Container.get(ExecutionMetadataRepository).save(metadataToSave);
|
|
|
|
}
|
|
|
|
|
2023-11-08 07:29:39 -08:00
|
|
|
await Container.get(ExecutionDataRepository).save({
|
|
|
|
data: data ?? '[]',
|
|
|
|
workflowData: workflow ?? {},
|
|
|
|
executionId: execution.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
return execution;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a successful execution in the DB and assign it to a workflow.
|
|
|
|
*/
|
|
|
|
export async function createSuccessfulExecution(workflow: WorkflowEntity) {
|
2024-01-17 07:08:50 -08:00
|
|
|
return await createExecution({ finished: true, status: 'success' }, workflow);
|
2023-11-08 07:29:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store an error execution in the DB and assign it to a workflow.
|
|
|
|
*/
|
|
|
|
export async function createErrorExecution(workflow: WorkflowEntity) {
|
2024-01-17 07:08:50 -08:00
|
|
|
return await createExecution(
|
2024-03-25 09:52:07 -07:00
|
|
|
{ finished: false, stoppedAt: new Date(), status: 'error' },
|
2024-01-17 07:08:50 -08:00
|
|
|
workflow,
|
|
|
|
);
|
2023-11-08 07:29:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a waiting execution in the DB and assign it to a workflow.
|
|
|
|
*/
|
|
|
|
export async function createWaitingExecution(workflow: WorkflowEntity) {
|
2024-01-17 07:08:50 -08:00
|
|
|
return await createExecution(
|
|
|
|
{ finished: false, waitTill: new Date(), status: 'waiting' },
|
|
|
|
workflow,
|
|
|
|
);
|
2023-11-08 07:29:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function getAllExecutions() {
|
2024-01-17 07:08:50 -08:00
|
|
|
return await Container.get(ExecutionRepository).find();
|
2023-11-08 07:29:39 -08:00
|
|
|
}
|