-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
feat: support external Redis configuration #4443
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
Open
satoukouga
wants to merge
9
commits into
Dokploy:canary
Choose a base branch
from
satoukouga:feat/add-external-redis-feature
base: canary
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4da75ea
feat: support external Redis configuration
satoukouga a6e8530
feat: add external Redis configuration and refactor related imports
satoukouga 74dcfd9
feat: refactor Redis imports
satoukouga 9b91b6c
feat: update Redis command and args structure for consistency across …
satoukouga 7d958fd
feat: refactor Redis connection and initialization for external confi…
satoukouga fade860
feat: update redis-constants for improved configuration handling
satoukouga d85bc9c
feat: add support for external Redis configuration in initialization
satoukouga 8afc45c
feat: improve Redis configuration handling and enhance error checking…
satoukouga d096823
feat: streamline Redis configuration and enhance error handling in setup
satoukouga File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| import { afterEach, describe, expect, it, vi } from "vitest"; | ||
| import fs from "node:fs"; | ||
|
|
||
| // Mock fs for readSecret tests | ||
| vi.mock("node:fs"); | ||
|
|
||
| // Mock pullImage to avoid actual docker commands in unit tests | ||
| // We mock the relative path used in redis-setup.ts to ensure it is caught | ||
| vi.mock("@dokploy/server/utils/docker/utils", () => ({ | ||
| pullImage: vi.fn().mockResolvedValue({}), | ||
| })); | ||
|
|
||
| // Mock dockerode to verify service settings | ||
| const mockCreateService = vi.fn().mockResolvedValue({}); | ||
| const mockGetService = vi.fn().mockReturnValue({ | ||
| inspect: vi.fn().mockRejectedValue(new Error("Not found")), | ||
| update: vi.fn().mockResolvedValue({}), | ||
| }); | ||
|
|
||
| vi.mock("dockerode", () => { | ||
| return { | ||
| default: vi.fn().mockImplementation(function () { | ||
| return { | ||
| createService: mockCreateService, | ||
| getService: mockGetService, | ||
| pull: vi.fn().mockResolvedValue({}), | ||
| }; | ||
| }), | ||
| }; | ||
| }); | ||
|
|
||
| describe("redis-connection", () => { | ||
| afterEach(() => { | ||
| vi.resetModules(); | ||
| vi.unstubAllEnvs(); | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| it("should use REDIS_URL if provided", async () => { | ||
| vi.stubEnv("REDIS_URL", "redis://user:pass@remote-host:6379/1"); | ||
|
|
||
| const { redisConfig } = await import("../../server/queues/redis-connection"); | ||
|
|
||
| expect(redisConfig).toEqual({ | ||
| url: "redis://user:pass@remote-host:6379/1", | ||
| }); | ||
| }, 30000); | ||
|
|
||
| it("should use individual env vars if REDIS_URL is not provided", async () => { | ||
| vi.stubEnv("REDIS_HOST", "custom-host"); | ||
| vi.stubEnv("REDIS_PORT", "1234"); | ||
| vi.stubEnv("REDIS_DB_INDEX", "2"); | ||
| vi.stubEnv("REDIS_PASSWORD", "secret"); | ||
| vi.stubEnv("REDIS_USERNAME", "admin"); | ||
|
|
||
| const { redisConfig } = await import("../../server/queues/redis-connection"); | ||
|
|
||
| expect(redisConfig).toEqual({ | ||
| host: "custom-host", | ||
| port: 1234, | ||
| db: 2, | ||
| password: "secret", | ||
| username: "admin", | ||
| }); | ||
| }); | ||
|
|
||
| it("should read password from REDIS_PASSWORD_FILE if provided", async () => { | ||
| vi.stubEnv("REDIS_PASSWORD_FILE", "/tmp/password.txt"); | ||
| vi.mocked(fs.readFileSync).mockReturnValue("file-secret\n"); | ||
|
|
||
| const { redisConfig } = await import("../../server/queues/redis-connection"); | ||
|
|
||
| expect((redisConfig as any).password).toBe("file-secret"); | ||
| expect(fs.readFileSync).toHaveBeenCalledWith("/tmp/password.txt", "utf8"); | ||
| }); | ||
|
|
||
| it("should fallback to defaults in development", async () => { | ||
| vi.stubEnv("NODE_ENV", "development"); | ||
|
|
||
| const { redisConfig } = await import("../../server/queues/redis-connection"); | ||
|
|
||
| expect(redisConfig).toEqual({ | ||
| host: "127.0.0.1", | ||
| port: 6379, | ||
| db: 0, | ||
| }); | ||
| }); | ||
|
|
||
| it("should fallback to production defaults", async () => { | ||
| vi.stubEnv("NODE_ENV", "production"); | ||
|
|
||
| const { redisConfig } = await import("../../server/queues/redis-connection"); | ||
|
|
||
| expect(redisConfig).toEqual({ | ||
| host: "dokploy-redis", | ||
| port: 6379, | ||
| db: 0, | ||
| }); | ||
| }); | ||
|
|
||
| it("should fallback to defaults on non-numeric env vars", async () => { | ||
| vi.stubEnv("REDIS_PORT", "invalid"); | ||
| vi.stubEnv("REDIS_DB_INDEX", "not-a-number"); | ||
|
|
||
| const { redisConfig } = await import("../../server/queues/redis-connection"); | ||
|
|
||
| expect((redisConfig as any).port).toBe(6379); | ||
| expect((redisConfig as any).db).toBe(0); | ||
| }); | ||
|
|
||
| it("should verify initializeRedis creates service with correct Args (no Command override) when password is set", async () => { | ||
| vi.stubEnv("REDIS_PASSWORD", "test-pass"); | ||
| vi.stubEnv("NODE_ENV", "production"); | ||
|
|
||
| const { initializeRedis } = await import("@dokploy/server/setup/redis-setup"); | ||
|
|
||
| await initializeRedis(); | ||
|
|
||
| expect(mockCreateService).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| TaskTemplate: expect.objectContaining({ | ||
| ContainerSpec: expect.objectContaining({ | ||
| Args: ["redis-server", "--requirepass", "test-pass"], | ||
| }), | ||
| }), | ||
| }), | ||
| ); | ||
|
|
||
| // Ensure Command is NOT set to avoid overriding ENTRYPOINT | ||
| const lastCall = mockCreateService.mock.calls[0][0]; | ||
| expect(lastCall.TaskTemplate.ContainerSpec.Command).toBeUndefined(); | ||
| }, 30000); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,13 @@ | ||
| import { redisConfig as sharedRedisConfig } from "@dokploy/server/setup/redis-constants"; | ||
| import type { ConnectionOptions } from "bullmq"; | ||
|
|
||
| export const redisConfig: ConnectionOptions = { | ||
| host: | ||
| process.env.NODE_ENV === "production" | ||
| ? process.env.REDIS_HOST || "dokploy-redis" | ||
| : "127.0.0.1", | ||
| }; | ||
| export const redisConfig: ConnectionOptions = | ||
| "url" in sharedRedisConfig | ||
| ? { url: sharedRedisConfig.url as string } | ||
| : { | ||
| host: sharedRedisConfig.host as string, | ||
| port: sharedRedisConfig.port as number, | ||
| db: sharedRedisConfig.db as number, | ||
| password: sharedRedisConfig.password as string | undefined, | ||
| username: sharedRedisConfig.username as string | undefined, | ||
| }; | ||
|
satoukouga marked this conversation as resolved.
Outdated
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import fs from "node:fs"; | ||
|
|
||
| export const { | ||
| REDIS_URL, | ||
| REDIS_HOST, | ||
| REDIS_PORT, | ||
| REDIS_PASSWORD, | ||
| REDIS_PASSWORD_FILE, | ||
| REDIS_DB_INDEX, | ||
| REDIS_USERNAME, | ||
| } = process.env; | ||
|
|
||
| export function readSecret(path: string): string { | ||
| try { | ||
| return fs.readFileSync(path, "utf8").trim(); | ||
| } catch (error) { | ||
| throw new Error( | ||
| `Cannot read secret at ${path}: ${error instanceof Error ? error.message : String(error)}`, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| export const getRedisPassword = (): string | undefined => { | ||
| return REDIS_PASSWORD_FILE | ||
| ? readSecret(REDIS_PASSWORD_FILE) | ||
| : REDIS_PASSWORD; | ||
| }; | ||
|
|
||
| const parseNumeric = ( | ||
| value: string | undefined, | ||
| defaultValue: number, | ||
| ): number => { | ||
| if (!value) return defaultValue; | ||
| const parsed = Number.parseInt(value, 10); | ||
| return Number.isFinite(parsed) ? parsed : defaultValue; | ||
| }; | ||
|
|
||
| /** | ||
| * Common Redis configuration shape. | ||
| */ | ||
| export const redisConfig = REDIS_URL | ||
| ? { url: REDIS_URL } | ||
| : { | ||
| host: | ||
| REDIS_HOST || | ||
| (process.env.NODE_ENV === "production" ? "dokploy-redis" : "127.0.0.1"), | ||
| port: parseNumeric(REDIS_PORT, 6379), | ||
| db: parseNumeric(REDIS_DB_INDEX, 0), | ||
| password: getRedisPassword(), | ||
| username: REDIS_USERNAME, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.