Skip to content

Commit ff81de3

Browse files
authored
fix(core): Ensure TTL safeguard for test webhooks applies only to multi-main setup (#9062)
1 parent ff77ef4 commit ff81de3

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

packages/cli/src/services/cache/cache.service.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import EventEmitter from 'node:events';
22

33
import { Service } from 'typedi';
44
import { caching } from 'cache-manager';
5-
import { jsonStringify } from 'n8n-workflow';
5+
import { ApplicationError, jsonStringify } from 'n8n-workflow';
66

77
import config from '@/config';
88
import { getDefaultRedisClient, getRedisPrefix } from '@/services/redis/RedisServiceHelper';
@@ -137,10 +137,9 @@ export class CacheService extends EventEmitter {
137137
if (!key?.length) return;
138138

139139
if (this.cache.kind === 'memory') {
140-
setTimeout(async () => {
141-
await this.cache.store.del(key);
142-
}, ttlMs);
143-
return;
140+
throw new ApplicationError('Method `expire` not yet implemented for in-memory cache', {
141+
level: 'warning',
142+
});
144143
}
145144

146145
await this.cache.store.expire(key, ttlMs / TIME.SECOND);

packages/cli/src/services/test-webhook-registrations.service.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { CacheService } from '@/services/cache/cache.service';
33
import type { IWebhookData } from 'n8n-workflow';
44
import type { IWorkflowDb } from '@/Interfaces';
55
import { TEST_WEBHOOK_TIMEOUT, TEST_WEBHOOK_TIMEOUT_BUFFER } from '@/constants';
6+
import { OrchestrationService } from './orchestration.service';
67

78
export type TestWebhookRegistration = {
89
pushRef?: string;
@@ -13,7 +14,10 @@ export type TestWebhookRegistration = {
1314

1415
@Service()
1516
export class TestWebhookRegistrationsService {
16-
constructor(private readonly cacheService: CacheService) {}
17+
constructor(
18+
private readonly cacheService: CacheService,
19+
private readonly orchestrationService: OrchestrationService,
20+
) {}
1721

1822
private readonly cacheKey = 'test-webhooks';
1923

@@ -22,6 +26,8 @@ export class TestWebhookRegistrationsService {
2226

2327
await this.cacheService.setHash(this.cacheKey, { [hashKey]: registration });
2428

29+
if (!this.orchestrationService.isMultiMainSetupEnabled) return;
30+
2531
/**
2632
* Multi-main setup: In a manual webhook execution, the main process that
2733
* handles a webhook might not be the same as the main process that created

packages/cli/test/unit/services/test-webhook-registrations.service.test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import type { CacheService } from '@/services/cache/cache.service';
2+
import type { OrchestrationService } from '@/services/orchestration.service';
23
import type { TestWebhookRegistration } from '@/services/test-webhook-registrations.service';
34
import { TestWebhookRegistrationsService } from '@/services/test-webhook-registrations.service';
45
import { mock } from 'jest-mock-extended';
56

67
describe('TestWebhookRegistrationsService', () => {
78
const cacheService = mock<CacheService>();
8-
const registrations = new TestWebhookRegistrationsService(cacheService);
9+
const registrations = new TestWebhookRegistrationsService(
10+
cacheService,
11+
mock<OrchestrationService>({ isMultiMainSetupEnabled: false }),
12+
);
913

1014
const registration = mock<TestWebhookRegistration>({
1115
webhook: { httpMethod: 'GET', path: 'hello', webhookId: undefined },
@@ -20,6 +24,12 @@ describe('TestWebhookRegistrationsService', () => {
2024

2125
expect(cacheService.setHash).toHaveBeenCalledWith(cacheKey, { [webhookKey]: registration });
2226
});
27+
28+
test('should skip setting TTL in single-main setup', async () => {
29+
await registrations.register(registration);
30+
31+
expect(cacheService.expire).not.toHaveBeenCalled();
32+
});
2333
});
2434

2535
describe('deregister()', () => {

0 commit comments

Comments
 (0)