Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(core): Move typeorm operators from WaitTracker to ExecutionRepository (no-changelog) #8163

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 1 addition & 27 deletions packages/cli/src/WaitTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ import {
WorkflowOperationError,
} from 'n8n-workflow';
import { Container, Service } from 'typedi';
import type { FindManyOptions, ObjectLiteral } from 'typeorm';
import { Not, LessThanOrEqual } from 'typeorm';
import { DateUtils } from 'typeorm/util/DateUtils';

import config from '@/config';
import * as ResponseHelper from '@/ResponseHelper';
import type {
IExecutionResponse,
Expand All @@ -18,7 +13,6 @@ import type {
import { WorkflowRunner } from '@/WorkflowRunner';
import { recoverExecutionDataFromEventLogMessages } from './eventbus/MessageEventBus/recoverEvents';
import { ExecutionRepository } from '@db/repositories/execution.repository';
import type { ExecutionEntity } from '@db/entities/ExecutionEntity';
import { OwnershipService } from './services/ownership.service';
import { Logger } from '@/Logger';

Expand Down Expand Up @@ -48,28 +42,8 @@ export class WaitTracker {

async getWaitingExecutions() {
this.logger.debug('Wait tracker querying database for waiting executions');
// Find all the executions which should be triggered in the next 70 seconds
const findQuery: FindManyOptions<ExecutionEntity> = {
select: ['id', 'waitTill'],
where: {
waitTill: LessThanOrEqual(new Date(Date.now() + 70000)),
status: Not('crashed'),
},
order: {
waitTill: 'ASC',
},
};

const dbType = config.getEnv('database.type');
if (dbType === 'sqlite') {
// This is needed because of issue in TypeORM <> SQLite:
// https://github.com/typeorm/typeorm/issues/2286
(findQuery.where! as ObjectLiteral).waitTill = LessThanOrEqual(
DateUtils.mixedDateToUtcDatetimeString(new Date(Date.now() + 70000)),
);
}

const executions = await this.executionRepository.findMultipleExecutions(findQuery);
const executions = await this.executionRepository.getWaitingExecutions();

if (executions.length === 0) {
return;
Expand Down
24 changes: 24 additions & 0 deletions packages/cli/src/databases/repositories/execution.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -516,4 +516,28 @@ export class ExecutionRepository extends Repository<ExecutionEntity> {
async deleteByIds(executionIds: string[]) {
return this.delete({ id: In(executionIds) });
}

async getWaitingExecutions() {
netroy marked this conversation as resolved.
Show resolved Hide resolved
// Find all the executions which should be triggered in the next 70 seconds
const waitTill = new Date(Date.now() + 70000);
const where: FindOptionsWhere<ExecutionEntity> = {
waitTill: LessThanOrEqual(waitTill),
status: Not('crashed'),
};

const dbType = config.getEnv('database.type');
if (dbType === 'sqlite') {
// This is needed because of issue in TypeORM <> SQLite:
// https://github.com/typeorm/typeorm/issues/2286
where.waitTill = LessThanOrEqual(DateUtils.mixedDateToUtcDatetimeString(waitTill));
}

return this.findMultipleExecutions({
select: ['id', 'waitTill'],
where,
order: {
waitTill: 'ASC',
},
});
}
}
52 changes: 52 additions & 0 deletions packages/cli/test/unit/repositories/execution.repository.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { mock } from 'jest-mock-extended';
import Container from 'typedi';
import type { EntityMetadata } from 'typeorm';
import { EntityManager, DataSource, Not, LessThanOrEqual } from 'typeorm';

import config from '@/config';
import { ExecutionEntity } from '@db/entities/ExecutionEntity';
import { ExecutionRepository } from '@db/repositories/execution.repository';

import { mockInstance } from '../../shared/mocking';

describe('ExecutionRepository', () => {
const entityManager = mockInstance(EntityManager);
const dataSource = mockInstance(DataSource, { manager: entityManager });
dataSource.getMetadata.mockReturnValue(mock<EntityMetadata>({ target: ExecutionEntity }));
Object.assign(entityManager, { connection: dataSource });

const executionRepository = Container.get(ExecutionRepository);
const mockDate = new Date('2023-12-28 12:34:56.789Z');

beforeAll(() => {
jest.clearAllMocks();
jest.useFakeTimers().setSystemTime(mockDate);
});

afterAll(() => jest.useRealTimers());

describe('getWaitingExecutions()', () => {
test.each(['sqlite', 'postgres'])(
'on %s, should be called with expected args',
async (dbType) => {
jest.spyOn(config, 'getEnv').mockReturnValueOnce(dbType);
entityManager.find.mockResolvedValueOnce([]);

await executionRepository.getWaitingExecutions();

expect(entityManager.find).toHaveBeenCalledWith(ExecutionEntity, {
order: { waitTill: 'ASC' },
select: ['id', 'waitTill'],
where: {
status: Not('crashed'),
waitTill: LessThanOrEqual(
dbType === 'sqlite'
? '2023-12-28 12:36:06.789'
: new Date('2023-12-28T12:36:06.789Z'),
),
},
});
},
);
});
});
Loading