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

fix(core): Handle Redis disconnects gracefully #11007

Merged
merged 4 commits into from
Sep 30, 2024

Conversation

ivov
Copy link
Contributor

@ivov ivov commented Sep 30, 2024

Currently a Redis disconnect triggers all manner of logs from multiple related services and from all six Redis clients simultaneously. This PR handles Redis this scenario more gracefully.

@n8n-assistant n8n-assistant bot added core Enhancement outside /nodes-base and /editor-ui n8n team Authored by the n8n team labels Sep 30, 2024
Copy link
Collaborator

@tomi tomi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice improvement 👏 Couple questions / comments

packages/cli/src/decorators/debounce.ts Show resolved Hide resolved
packages/cli/src/services/redis-client.service.ts Outdated Show resolved Hide resolved
if (cumulativeTimeout > this.config.maxTimeout) {
const maxTimeout = Math.round(this.config.maxTimeout / 1000) + 's';
this.logger.error(`Unable to connect to Redis after trying to connect for ${maxTimeout}`);
this.logger.error('Exiting process due to Redis connection error');
process.exit(1);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally we should allow graceful exit of other parts of the app as well. Otherwise we might leave some resources in improper state, like DB connections etc. Just mentioning this, no need to act on it here

@@ -156,4 +190,33 @@ export class RedisClientService {
return { host, port: parseInt(port) };
});
}

@Debounce(1000)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need the debounce here?

Copy link
Contributor Author

@ivov ivov Sep 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debouncing prevents all six clients spamming the logs with their calls to retryStrategy in quick succession, i.e. we consolidate all logs from those calls on connection-lost and connection-recovered from six to one.

export type RedisClientType = N8nRedisClientType | BullRedisClientType;

/**
 * Redis client used by n8n.
 *
 * - `subscriber(n8n)` to listen for messages from scaling mode pubsub channels
 * - `publisher(n8n)` to send messages into scaling mode pubsub channels
 * - `cache(n8n)` for caching operations (variables, resource ownership, etc.)
 */
type N8nRedisClientType = 'subscriber(n8n)' | 'publisher(n8n)' | 'cache(n8n)';

/**
 * Redis client used internally by Bull. Suffixed with `(bull)` at `ScalingService.setupQueue`.
 *
 * - `subscriber(bull)` for event listening
 * - `client(bull)` for general queue operations
 * - `bclient(bull)` for blocking operations when processing jobs
 */
type BullRedisClientType = 'subscriber(bull)' | 'client(bull)' | 'bclient(bull)';

@ivov ivov requested a review from tomi September 30, 2024 11:47
Copy link
Collaborator

@tomi tomi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

Copy link

cypress bot commented Sep 30, 2024

n8n    Run #7102

Run Properties:  status check passed Passed #7102  •  git commit cd916480c2: 🌳 master 🖥️ browsers:node18.12.0-chrome107 🤖 PR User 🗃️ e2e/*
Project n8n
Branch Review master
Run status status check passed Passed #7102
Run duration 04m 24s
Commit git commit cd916480c2: 🌳 master 🖥️ browsers:node18.12.0-chrome107 🤖 PR User 🗃️ e2e/*
Committer Iván Ovejero
View all properties for this run ↗︎

Test results
Tests that failed  Failures 0
Tests that were flaky  Flaky 1
Tests that did not run due to a developer annotating a test with .skip  Pending 0
Tests that did not run due to a failure in a mocha hook  Skipped 0
Tests that passed  Passing 435
View all changes introduced in this branch ↗︎

Copy link
Contributor

✅ All Cypress E2E specs passed

@ivov ivov merged commit cd91648 into master Sep 30, 2024
56 of 57 checks passed
@ivov ivov deleted the handle-redis-disconnects-gracefully branch September 30, 2024 14:36
This was referenced Oct 2, 2024
@janober
Copy link
Member

janober commented Oct 2, 2024

Got released with n8n@1.62.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Enhancement outside /nodes-base and /editor-ui n8n team Authored by the n8n team Released
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants