From dde559791ec9154031f01b3c75798626277ee267 Mon Sep 17 00:00:00 2001 From: Konstantinos Feretos Date: Thu, 5 Oct 2023 10:44:19 +0300 Subject: [PATCH] refactor(grpc-sdk,core): getRedisDetails() (#696) --- libraries/grpc-sdk/src/index.ts | 19 ++++----- .../grpc-sdk/src/modules/config/index.ts | 23 +++++++++- packages/core/README.mdx | 1 + packages/core/src/config-manager/index.ts | 42 +++++-------------- packages/core/src/core.proto | 7 +++- 5 files changed, 46 insertions(+), 46 deletions(-) diff --git a/libraries/grpc-sdk/src/index.ts b/libraries/grpc-sdk/src/index.ts index 450427ee0..99d0fcee5 100644 --- a/libraries/grpc-sdk/src/index.ts +++ b/libraries/grpc-sdk/src/index.ts @@ -20,7 +20,6 @@ import { Client } from 'nice-grpc'; import { status } from '@grpc/grpc-js'; import { ConduitModuleDefinition, - GetRedisDetailsResponse, HealthCheckResponse_ServingStatus, HealthDefinition, ModuleListResponse_ModuleResponse, @@ -355,22 +354,18 @@ export default class ConduitGrpcSdk { this._redisDetails = { host: process.env.REDIS_HOST!, port: parseInt(process.env.REDIS_PORT!, 10), - username: process.env.REDIS_USERNAME, - password: process.env.REDIS_PASSWORD, + db: parseInt(process.env.REDIS_DB!, 10) || 0, + ...(process.env.REDIS_USERNAME ? { username: process.env.REDIS_USERNAME } : {}), + ...(process.env.REDIS_PASSWORD ? { username: process.env.REDIS_PASSWORD } : {}), }; } else { promise = promise .then(() => this.config.getRedisDetails()) - .then((r: GetRedisDetailsResponse) => { - if (r.redisConfig) { - this._redisDetails = JSON.parse(r.redisConfig); + .then(r => { + if (r.standalone) { + this._redisDetails = r.standalone; } else { - this._redisDetails = { - host: r.redisHost, - port: r.redisPort, - username: r.redisUsername, - password: r.redisPassword, - }; + this._redisDetails = r.cluster!; } }); } diff --git a/libraries/grpc-sdk/src/modules/config/index.ts b/libraries/grpc-sdk/src/modules/config/index.ts index f84ba5e2c..e1fe15871 100644 --- a/libraries/grpc-sdk/src/modules/config/index.ts +++ b/libraries/grpc-sdk/src/modules/config/index.ts @@ -8,6 +8,7 @@ import { } from '../../protoUtils/core'; import { Indexable } from '../../interfaces'; import ConduitGrpcSdk from '../../index'; +import { ClusterOptions, RedisOptions } from 'ioredis'; export class Config extends ConduitModule { private readonly emitter = new EventEmitter(); @@ -76,9 +77,27 @@ export class Config extends ConduitModule { }); } - getRedisDetails() { + async getRedisDetails(): Promise<{ + standalone?: RedisOptions; + cluster?: ClusterOptions; + redisHost?: string; + redisPort?: number; + redisUsername?: string; + redisPassword?: string; + redisConfig?: string; + }> { const request: Indexable = {}; - return this.client!.getRedisDetails(request); + const r = await this.client!.getRedisDetails(request); + return { + ...(r.standalone ? { standalone: JSON.parse(r.standalone) } : undefined), + ...(r.cluster ? { cluster: JSON.parse(r.cluster) } : undefined), + // maintain backwards compatibility with <=grpc-sdk-v0.16.0-alpha.20 + redisHost: r.redisHost, + redisPort: r.redisPort, + redisUsername: r.redisUsername, + redisPassword: r.redisPassword, + redisConfig: r.redisConfig, + }; } registerModule( diff --git a/packages/core/README.mdx b/packages/core/README.mdx index 64f6742ff..64fbb1f29 100755 --- a/packages/core/README.mdx +++ b/packages/core/README.mdx @@ -29,6 +29,7 @@ to find out more about Conduit's internals. |:-------------------:|:-----------------------------------------------------------|:--------:|:-----------:| | `REDIS_HOST` | Redis Address | True | `localhost` | | `REDIS_PORT` | Redis Port | True | `6379` | +| `REDIS_DB` | Redis Database | False | `0` | | `ADMIN_HTTP_PORT` | Port to be used by admin REST and GrahpQL APIs | False | `3030` | | `ADMIN_SOCKET_PORT` | Port to be used by admin WebSockets API | False | `3030` | | `GRPC_PORT` | The port number the gRPC server will listen to | False | `55190` | diff --git a/packages/core/src/config-manager/index.ts b/packages/core/src/config-manager/index.ts index 7240a2ff5..6d23ac3f5 100644 --- a/packages/core/src/config-manager/index.ts +++ b/packages/core/src/config-manager/index.ts @@ -21,9 +21,9 @@ import { ConfigStorage } from './config-storage'; import parseConfigSchema from '../utils'; import { IModuleConfig } from '../interfaces/IModuleConfig'; import convict from 'convict'; -import fs from 'fs-extra'; -import { isNil, merge } from 'lodash'; +import { merge } from 'lodash'; import { GrpcServer } from '@conduitplatform/module-tools'; +import { RedisOptions } from 'ioredis'; export default class ConfigManager implements IConfigManager { grpcSdk: ConduitGrpcSdk; @@ -148,39 +148,19 @@ export default class ConfigManager implements IConfigManager { call: GrpcRequest, callback: GrpcCallback, ) { - let redisJson; - const redisConfig = process.env.REDIS_CONFIG; - if (redisConfig) { - if (redisConfig.startsWith('{')) { - try { - redisJson = JSON.parse(redisConfig); - } catch (e) { - return callback({ - code: status.INTERNAL, - message: 'Failed to parse redis config', - }); - } - } else { - try { - redisJson = JSON.parse(fs.readFileSync(redisConfig, 'utf8')); - } catch (e) { - return callback({ - code: status.INTERNAL, - message: 'Failed to parse redis config', - }); - } - } - } - if (!isNil(redisJson)) { + const redisDetails = this.grpcSdk.redisDetails; + if (redisDetails.hasOwnProperty('nodes')) { callback(null, { - redisConfig: JSON.stringify(redisJson), + cluster: JSON.stringify(redisDetails), }); } else { callback(null, { - redisHost: process.env.REDIS_HOST!, - redisPort: parseInt(process.env.REDIS_PORT!), - redisPassword: process.env.REDIS_PASSWORD, - redisUsername: process.env.REDIS_USERNAME, + standalone: JSON.stringify(redisDetails), + // maintain backwards compatibility with <=grpc-sdk-v0.16.0-alpha.20 + redisHost: (redisDetails as RedisOptions).host, + redisPort: (redisDetails as RedisOptions).port, + redisUsername: (redisDetails as RedisOptions).username, + redisPassword: (redisDetails as RedisOptions).password, }); } } diff --git a/packages/core/src/core.proto b/packages/core/src/core.proto index 012c36104..78129e42c 100644 --- a/packages/core/src/core.proto +++ b/packages/core/src/core.proto @@ -121,13 +121,18 @@ message GetConfigRequest { string key = 1; } - message GetRedisDetailsResponse { optional string redisHost = 1; optional int32 redisPort = 2; optional string redisUsername = 3; optional string redisPassword = 4; optional string redisConfig = 5; + // temporarily keeping these^ around to avoid a breaking change + + oneof config { + string standalone = 6; + string cluster = 7; + } } message GetConfigResponse {