refactor(core): Bring active executions into executions controller (no-changelog) (#8371)

This commit is contained in:
Iván Ovejero
2024-01-23 09:48:50 +01:00
committed by GitHub
parent 913c8c6b0c
commit 49b52c4f1d
22 changed files with 544 additions and 331 deletions

View File

@@ -0,0 +1,127 @@
import { mock, mockFn } from 'jest-mock-extended';
import { ActiveExecutionService } from '@/executions/active-execution.service';
import config from '@/config';
import type { ExecutionRepository } from '@db/repositories/execution.repository';
import type { ActiveExecutions } from '@/ActiveExecutions';
import type { Job, Queue } from '@/Queue';
import type { IExecutionBase, IExecutionsCurrentSummary } from '@/Interfaces';
import type { WaitTracker } from '@/WaitTracker';
describe('ActiveExecutionsService', () => {
const queue = mock<Queue>();
const activeExecutions = mock<ActiveExecutions>();
const executionRepository = mock<ExecutionRepository>();
const waitTracker = mock<WaitTracker>();
const jobIds = ['j1', 'j2'];
const jobs = jobIds.map((executionId) => mock<Job>({ data: { executionId } }));
const activeExecutionService = new ActiveExecutionService(
mock(),
queue,
activeExecutions,
executionRepository,
waitTracker,
);
const getEnv = mockFn<(typeof config)['getEnv']>();
config.getEnv = getEnv;
beforeEach(() => {
jest.clearAllMocks();
});
describe('stop()', () => {
describe('in regular mode', () => {
getEnv.calledWith('executions.mode').mockReturnValue('regular');
it('should call `ActiveExecutions.stopExecution()`', async () => {
const execution = mock<IExecutionBase>({ id: '123' });
await activeExecutionService.stop(execution);
expect(activeExecutions.stopExecution).toHaveBeenCalledWith(execution.id);
});
it('should call `WaitTracker.stopExecution()` if `ActiveExecutions.stopExecution()` found no execution', async () => {
activeExecutions.stopExecution.mockResolvedValue(undefined);
const execution = mock<IExecutionBase>({ id: '123' });
await activeExecutionService.stop(execution);
expect(waitTracker.stopExecution).toHaveBeenCalledWith(execution.id);
});
});
describe('in queue mode', () => {
it('should call `ActiveExecutions.stopExecution()`', async () => {
const execution = mock<IExecutionBase>({ id: '123' });
await activeExecutionService.stop(execution);
expect(activeExecutions.stopExecution).toHaveBeenCalledWith(execution.id);
});
it('should call `WaitTracker.stopExecution` if `ActiveExecutions.stopExecution()` found no execution', async () => {
activeExecutions.stopExecution.mockResolvedValue(undefined);
const execution = mock<IExecutionBase>({ id: '123' });
await activeExecutionService.stop(execution);
expect(waitTracker.stopExecution).toHaveBeenCalledWith(execution.id);
});
});
});
describe('findManyInQueueMode()', () => {
it('should query for active jobs, waiting jobs, and in-memory executions', async () => {
const sharedWorkflowIds = ['123'];
const filter = {};
const executionIds = ['e1', 'e2'];
const summaries = executionIds.map((e) => mock<IExecutionsCurrentSummary>({ id: e }));
activeExecutions.getActiveExecutions.mockReturnValue(summaries);
queue.getJobs.mockResolvedValue(jobs);
executionRepository.findMultipleExecutions.mockResolvedValue([]);
executionRepository.getManyActive.mockResolvedValue([]);
await activeExecutionService.findManyInQueueMode(filter, sharedWorkflowIds);
expect(queue.getJobs).toHaveBeenCalledWith(['active', 'waiting']);
expect(executionRepository.getManyActive).toHaveBeenCalledWith(
jobIds.concat(executionIds),
sharedWorkflowIds,
filter,
);
});
});
describe('findManyInRegularMode()', () => {
it('should return summaries of in-memory executions', async () => {
const sharedWorkflowIds = ['123'];
const filter = {};
const executionIds = ['e1', 'e2'];
const summaries = executionIds.map((e) =>
mock<IExecutionsCurrentSummary>({ id: e, workflowId: '123', status: 'running' }),
);
activeExecutions.getActiveExecutions.mockReturnValue(summaries);
const result = await activeExecutionService.findManyInRegularMode(filter, sharedWorkflowIds);
expect(result).toEqual([
expect.objectContaining({
id: 'e1',
workflowId: '123',
status: 'running',
}),
expect.objectContaining({
id: 'e2',
workflowId: '123',
status: 'running',
}),
]);
});
});
});

View File

@@ -0,0 +1,90 @@
import { mock, mockFn } from 'jest-mock-extended';
import config from '@/config';
import { NotFoundError } from '@/errors/response-errors/not-found.error';
import { ExecutionsController } from '@/executions/executions.controller';
import { License } from '@/License';
import { mockInstance } from '../../shared/mocking';
import type { IExecutionBase } from '@/Interfaces';
import type { ActiveExecutionService } from '@/executions/active-execution.service';
import type { ExecutionRequest } from '@/executions/execution.types';
import type { WorkflowSharingService } from '@/workflows/workflowSharing.service';
describe('ExecutionsController', () => {
const getEnv = mockFn<(typeof config)['getEnv']>();
config.getEnv = getEnv;
mockInstance(License);
const activeExecutionService = mock<ActiveExecutionService>();
const workflowSharingService = mock<WorkflowSharingService>();
const req = mock<ExecutionRequest.GetManyActive>({ query: { filter: '{}' } });
beforeEach(() => {
jest.clearAllMocks();
});
describe('getActive()', () => {
workflowSharingService.getSharedWorkflowIds.mockResolvedValue(['123']);
it('should call `ActiveExecutionService.findManyInQueueMode()`', async () => {
getEnv.calledWith('executions.mode').mockReturnValue('queue');
await new ExecutionsController(
mock(),
mock(),
workflowSharingService,
activeExecutionService,
).getActive(req);
expect(activeExecutionService.findManyInQueueMode).toHaveBeenCalled();
expect(activeExecutionService.findManyInRegularMode).not.toHaveBeenCalled();
});
it('should call `ActiveExecutionService.findManyInRegularMode()`', async () => {
getEnv.calledWith('executions.mode').mockReturnValue('regular');
await new ExecutionsController(
mock(),
mock(),
workflowSharingService,
activeExecutionService,
).getActive(req);
expect(activeExecutionService.findManyInQueueMode).not.toHaveBeenCalled();
expect(activeExecutionService.findManyInRegularMode).toHaveBeenCalled();
});
});
describe('stop()', () => {
const req = mock<ExecutionRequest.Stop>({ params: { id: '999' } });
const execution = mock<IExecutionBase>();
it('should 404 when execution is not found or inaccessible for user', async () => {
activeExecutionService.findOne.mockResolvedValue(undefined);
const promise = new ExecutionsController(
mock(),
mock(),
workflowSharingService,
activeExecutionService,
).stop(req);
await expect(promise).rejects.toThrow(NotFoundError);
expect(activeExecutionService.findOne).toHaveBeenCalledWith('999', ['123']);
});
it('should call `ActiveExecutionService.stop()`', async () => {
getEnv.calledWith('executions.mode').mockReturnValue('regular');
activeExecutionService.findOne.mockResolvedValue(execution);
await new ExecutionsController(
mock(),
mock(),
workflowSharingService,
activeExecutionService,
).stop(req);
expect(activeExecutionService.stop).toHaveBeenCalled();
});
});
});