-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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 Redis connections to use Redis URL - closes #7421 #7736
Refactor Redis connections to use Redis URL - closes #7421 #7736
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR Summary
This PR consolidates Redis connection parameters into a single REDIS_URL
environment variable across various configuration files and code modules, simplifying Redis connection handling and potentially enabling IPv6 support.
- Replaced separate Redis connection parameters (host, port, username, password) with
REDIS_URL
in environment files and Kubernetes configurations - Updated
cache-storage.module-factory.ts
andmessage-queue.module-factory.ts
to useREDIS_URL
for connection - Modified
environment-variables.ts
to validate the newREDIS_URL
format - Updated self-hosting documentation and upgrade guide to reflect the
REDIS_URL
change - Ensured consistency across Docker Compose, Kubernetes, and Terraform configurations
13 file(s) reviewed, 7 comment(s)
Edit PR Review Bot Settings | Greptile
packages/twenty-docker/.env.example
Outdated
@@ -5,8 +5,7 @@ TAG=latest | |||
PG_DATABASE_HOST=db:5432 | |||
|
|||
SERVER_URL=http://localhost:3000 | |||
# REDIS_HOST=redis | |||
# REDIS_PORT=6379 | |||
# REDIS_URL=redis://localhost:6379 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: Add a comment explaining the REDIS_URL format for clarity
@@ -25,8 +25,7 @@ services: | |||
PG_DATABASE_URL: postgres://twenty:twenty@${PG_DATABASE_HOST}/default | |||
SERVER_URL: ${SERVER_URL} | |||
FRONT_BASE_URL: ${FRONT_BASE_URL:-$SERVER_URL} | |||
REDIS_PORT: ${REDIS_PORT:-6379} | |||
REDIS_HOST: ${REDIS_HOST:-redis} | |||
REDIS_URL: ${REDIS_URL:-redis://localhost:6379} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: Consider using the redis service name instead of localhost in the default URL
name = "REDIS_PORT" | ||
value = 6379 | ||
name = "REDIS_URL" | ||
value = "redis://${kubernetes_service.twentycrm_redis.metadata.0.name}.${kubernetes_namespace.twentycrm.metadata.0.name}.svc.cluster.local:6379" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logic: The Redis URL format doesn't include authentication. If Redis requires auth, update the URL to include username and password
name = "REDIS_URL" | ||
value = "redis://${kubernetes_service.twentycrm_redis.metadata.0.name}.${kubernetes_namespace.twentycrm.metadata.0.name}.svc.cluster.local:6379" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: Consider using a separate variable for the Redis URL instead of constructing it inline. This would improve maintainability and allow for easier configuration changes.
throw new Error( | ||
`${cacheStorageType} cache storage requires host: ${host} and port: ${port} to be defined, check your .env file`, | ||
`${cacheStorageType} cache storage requires ${connectionString} to be defined, check your .env file`, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logic: Error message uses connectionString variable, which could be undefined. Use 'REDIS_URL' string instead
@@ -3,6 +3,7 @@ import { | |||
MessageQueueDriverType, | |||
MessageQueueModuleOptions, | |||
} from 'src/engine/core-modules/message-queue/interfaces'; | |||
import IORedis from 'ioredis'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: Consider adding a comment explaining why IORedis is being imported and used
const port = environmentService.get('REDIS_PORT'); | ||
const username = environmentService.get('REDIS_USERNAME'); | ||
const password = environmentService.get('REDIS_PASSWORD'); | ||
const connectionString = environmentService.get('REDIS_URL'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logic: Validate REDIS_URL before using it to prevent potential runtime errors
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clean change! I'm testing that it's working as expected locally
When I don't have the environment variable REDIS_URL set in my .env, I'm getting: |
packages/twenty-server/src/engine/core-modules/cache-storage/cache-storage.module-factory.ts
Outdated
Show resolved
Hide resolved
@thomasmol could you made the fix mentioned above and we are good to go :) |
Actually, can you try to also leverage the @ValidateIf decorator in environment-variables.ts, it's our recommended way to ask for an env variable to be set |
Also, when I run |
And the CI is red:
|
username, | ||
password, | ||
}, | ||
connection: new IORedis(connectionString) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this seems wrong actually, we should not instanciate here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
true, i changed it to just pass the connection string here. It works but there is a type error. Unsure why that is since passing a string should be OK as per: https://redis.github.io/ioredis/classes/Redis.html#constructor
changed:
Do you know of a fix for the type error in Updating the bullmq factory options interface to export interface BullMQDriverFactoryOptions {
type: MessageQueueDriverType.BullMQ;
options: BullMQDriverOptions & { connection: string };
} would work |
username, | ||
password, | ||
}, | ||
connection: connectionString as any, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's get rid of this any type
@Weiko is taking a look
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM to be merged once typing issue has been solved
@charlesBochet tried to fix typing but couldn't find a good solution, seems bullmq redis option works with a string but typing does not accept string. I've updated our custom type to avoid type assertion but this is not the perfect solution. Ideally we should open an issue on bullmq repo |
/award 300 |
Awarding thomasmol: 300 points 🕹️ Well done! Check out your new contribution on oss.gg/thomasmol |
Thanks a lot @thomasmol! |
Closes #7421
This pull request consolidates Redis connection parameters into a single
REDIS_URL
environment variable across various configuration files and code modules. The most important changes include updates to environment variable files, Docker and Kubernetes configurations, and code modules to utilize the newREDIS_URL
format.Environment Variable Updates:
packages/twenty-docker/.env.example
: ReplacedREDIS_HOST
andREDIS_PORT
withREDIS_URL
.packages/twenty-server/.env.example
: ReplacedREDIS_HOST
,REDIS_PORT
,REDIS_USERNAME
, andREDIS_PASSWORD
withREDIS_URL
.packages/twenty-server/.env.test
: ReplacedREDIS_HOST
,REDIS_PORT
,REDIS_USERNAME
, andREDIS_PASSWORD
withREDIS_URL
.Docker and Kubernetes Configuration Updates:
packages/twenty-docker/docker-compose.yml
: ReplacedREDIS_HOST
andREDIS_PORT
withREDIS_URL
in multiple service definitions. [1] [2]packages/twenty-docker/k8s/manifests/deployment-server.yaml
: ReplacedREDIS_HOST
andREDIS_PORT
withREDIS_URL
.packages/twenty-docker/k8s/manifests/deployment-worker.yaml
: ReplacedREDIS_HOST
andREDIS_PORT
withREDIS_URL
.packages/twenty-docker/k8s/terraform/deployment-server.tf
: ReplacedREDIS_HOST
andREDIS_PORT
withREDIS_URL
.packages/twenty-docker/k8s/terraform/deployment-worker.tf
: ReplacedREDIS_HOST
andREDIS_PORT
withREDIS_URL
.Code Module Updates:
packages/twenty-server/src/engine/core-modules/cache-storage/cache-storage.module-factory.ts
: ReplacedREDIS_HOST
andREDIS_PORT
withREDIS_URL
.packages/twenty-server/src/engine/core-modules/environment/environment-variables.ts
: Added validation forREDIS_URL
.packages/twenty-server/src/engine/core-modules/message-queue/message-queue.module-factory.ts
: ReplacedREDIS_HOST
,REDIS_PORT
,REDIS_USERNAME
, andREDIS_PASSWORD
withREDIS_URL
. [1] [2]Documentation Updates:
packages/twenty-website/src/content/developers/self-hosting/self-hosting-var.mdx
: Updated documentation to reflect the change toREDIS_URL
.packages/twenty-website/src/content/developers/self-hosting/upgrade-guide.mdx
: Added upgrade instructions for the newREDIS_URL
variable.