2024-01-22 09:25:36 -08:00
|
|
|
import { Config } from '@oclif/core';
|
2023-09-07 05:44:19 -07:00
|
|
|
import { Worker } from '@/commands/worker';
|
2023-09-26 04:58:06 -07:00
|
|
|
import config from '@/config';
|
2023-09-07 05:44:19 -07:00
|
|
|
import { Telemetry } from '@/telemetry';
|
|
|
|
import { ExternalSecretsManager } from '@/ExternalSecrets/ExternalSecretsManager.ee';
|
2023-09-22 08:22:12 -07:00
|
|
|
import { BinaryDataService } from 'n8n-core';
|
perf(core): Improve caching service (#8213)
Story: https://linear.app/n8n/issue/PAY-1188
- Implement Redis hashes on the caching service, based on Micha's work
in #7747, adapted from `node-cache-manager-ioredis-yet`. Optimize
workflow ownership lookups and manual webhook lookups with Redis hashes.
- Simplify the caching service by removing all currently unused methods
and options: `enable`, `disable`, `getCache`, `keys`, `keyValues`,
`refreshFunctionEach`, `refreshFunctionMany`, `refreshTtl`, etc.
- Remove the flag `N8N_CACHE_ENABLED`. Currently some features on
`master` are broken with caching disabled, and test webhooks now rely
entirely on caching, for multi-main setup support. We originally
introduced this flag to protect against excessive memory usage, but
total cache usage is low enough that we decided to drop this setting.
Apparently this flag was also never documented.
- Overall caching service refactor: use generics, reduce branching, add
discriminants for cache kinds for better type safety, type caching
events, improve readability, remove outdated docs, etc. Also refactor
and expand caching service tests.
Follow-up to: https://github.com/n8n-io/n8n/pull/8176
---------
Co-authored-by: Michael Auerswald <michael.auerswald@gmail.com>
2024-01-05 02:52:44 -08:00
|
|
|
import { CacheService } from '@/services/cache/cache.service';
|
2023-09-07 05:44:19 -07:00
|
|
|
import { RedisServicePubSubPublisher } from '@/services/redis/RedisServicePubSubPublisher';
|
|
|
|
import { RedisServicePubSubSubscriber } from '@/services/redis/RedisServicePubSubSubscriber';
|
|
|
|
import { MessageEventBus } from '@/eventbus/MessageEventBus/MessageEventBus';
|
|
|
|
import { LoadNodesAndCredentials } from '@/LoadNodesAndCredentials';
|
|
|
|
import { CredentialTypes } from '@/CredentialTypes';
|
|
|
|
import { NodeTypes } from '@/NodeTypes';
|
|
|
|
import { InternalHooks } from '@/InternalHooks';
|
|
|
|
import { PostHogClient } from '@/posthog';
|
|
|
|
import { RedisService } from '@/services/redis.service';
|
2023-10-06 04:58:11 -07:00
|
|
|
import { OrchestrationHandlerWorkerService } from '@/services/orchestration/worker/orchestration.handler.worker.service';
|
|
|
|
import { OrchestrationWorkerService } from '@/services/orchestration/worker/orchestration.worker.service';
|
2024-01-22 02:16:29 -08:00
|
|
|
import { OrchestrationService } from '@/services/orchestration.service';
|
2023-09-07 05:44:19 -07:00
|
|
|
|
2024-01-24 04:38:57 -08:00
|
|
|
import * as testDb from '../shared/testDb';
|
2023-11-10 06:04:26 -08:00
|
|
|
import { mockInstance } from '../../shared/mocking';
|
|
|
|
|
2024-01-22 09:25:36 -08:00
|
|
|
const oclifConfig = new Config({ root: __dirname });
|
2023-09-07 05:44:19 -07:00
|
|
|
|
|
|
|
beforeAll(async () => {
|
2023-09-26 04:58:06 -07:00
|
|
|
config.set('executions.mode', 'queue');
|
2023-10-05 06:25:17 -07:00
|
|
|
config.set('binaryDataManager.availableModes', 'filesystem');
|
2023-09-07 05:44:19 -07:00
|
|
|
mockInstance(Telemetry);
|
|
|
|
mockInstance(PostHogClient);
|
|
|
|
mockInstance(InternalHooks);
|
|
|
|
mockInstance(CacheService);
|
|
|
|
mockInstance(ExternalSecretsManager);
|
2023-09-22 08:22:12 -07:00
|
|
|
mockInstance(BinaryDataService);
|
2023-09-07 05:44:19 -07:00
|
|
|
mockInstance(MessageEventBus);
|
|
|
|
mockInstance(LoadNodesAndCredentials);
|
|
|
|
mockInstance(CredentialTypes);
|
|
|
|
mockInstance(NodeTypes);
|
|
|
|
mockInstance(RedisService);
|
|
|
|
mockInstance(RedisServicePubSubPublisher);
|
|
|
|
mockInstance(RedisServicePubSubSubscriber);
|
2024-01-22 02:16:29 -08:00
|
|
|
mockInstance(OrchestrationService);
|
2024-01-24 04:38:57 -08:00
|
|
|
await testDb.init();
|
2024-05-30 04:54:29 -07:00
|
|
|
await oclifConfig.load();
|
2024-01-24 04:38:57 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(async () => {
|
|
|
|
await testDb.terminate();
|
2023-09-07 05:44:19 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
test('worker initializes all its components', async () => {
|
2023-09-26 04:58:06 -07:00
|
|
|
const worker = new Worker([], oclifConfig);
|
2023-09-07 05:44:19 -07:00
|
|
|
|
|
|
|
jest.spyOn(worker, 'init');
|
|
|
|
jest.spyOn(worker, 'initLicense').mockImplementation(async () => {});
|
2023-09-22 08:22:12 -07:00
|
|
|
jest.spyOn(worker, 'initBinaryDataService').mockImplementation(async () => {});
|
2023-09-07 05:44:19 -07:00
|
|
|
jest.spyOn(worker, 'initExternalHooks').mockImplementation(async () => {});
|
|
|
|
jest.spyOn(worker, 'initExternalSecrets').mockImplementation(async () => {});
|
|
|
|
jest.spyOn(worker, 'initEventBus').mockImplementation(async () => {});
|
2023-10-06 04:58:11 -07:00
|
|
|
jest.spyOn(worker, 'initOrchestration');
|
2023-09-07 05:44:19 -07:00
|
|
|
jest
|
2023-10-06 04:58:11 -07:00
|
|
|
.spyOn(OrchestrationWorkerService.prototype, 'publishToEventLog')
|
2023-09-07 05:44:19 -07:00
|
|
|
.mockImplementation(async () => {});
|
|
|
|
jest
|
2023-10-06 04:58:11 -07:00
|
|
|
.spyOn(OrchestrationHandlerWorkerService.prototype, 'initSubscriber')
|
2023-09-07 05:44:19 -07:00
|
|
|
.mockImplementation(async () => {});
|
2023-10-06 04:58:11 -07:00
|
|
|
jest.spyOn(RedisServicePubSubPublisher.prototype, 'init').mockImplementation(async () => {});
|
2023-09-07 05:44:19 -07:00
|
|
|
jest.spyOn(worker, 'initQueue').mockImplementation(async () => {});
|
|
|
|
|
|
|
|
await worker.init();
|
|
|
|
|
2023-09-26 04:58:06 -07:00
|
|
|
expect(worker.queueModeId).toBeDefined();
|
|
|
|
expect(worker.queueModeId).toContain('worker');
|
|
|
|
expect(worker.queueModeId.length).toBeGreaterThan(15);
|
2023-12-14 07:16:12 -08:00
|
|
|
expect(worker.initLicense).toHaveBeenCalledTimes(1);
|
|
|
|
expect(worker.initBinaryDataService).toHaveBeenCalledTimes(1);
|
|
|
|
expect(worker.initExternalHooks).toHaveBeenCalledTimes(1);
|
|
|
|
expect(worker.initExternalSecrets).toHaveBeenCalledTimes(1);
|
|
|
|
expect(worker.initEventBus).toHaveBeenCalledTimes(1);
|
|
|
|
expect(worker.initOrchestration).toHaveBeenCalledTimes(1);
|
|
|
|
expect(OrchestrationHandlerWorkerService.prototype.initSubscriber).toHaveBeenCalledTimes(1);
|
|
|
|
expect(OrchestrationWorkerService.prototype.publishToEventLog).toHaveBeenCalledTimes(1);
|
|
|
|
expect(worker.initQueue).toHaveBeenCalledTimes(1);
|
2023-09-07 05:44:19 -07:00
|
|
|
|
|
|
|
jest.restoreAllMocks();
|
|
|
|
});
|