Skip to content

Commit

Permalink
refactor(grpc-sdk,core): getRedisDetails() (#696)
Browse files Browse the repository at this point in the history
  • Loading branch information
kon14 authored Oct 5, 2023
1 parent 9f3ed29 commit dde5597
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 46 deletions.
19 changes: 7 additions & 12 deletions libraries/grpc-sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { Client } from 'nice-grpc';
import { status } from '@grpc/grpc-js';
import {
ConduitModuleDefinition,
GetRedisDetailsResponse,
HealthCheckResponse_ServingStatus,
HealthDefinition,
ModuleListResponse_ModuleResponse,
Expand Down Expand Up @@ -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!;
}
});
}
Expand Down
23 changes: 21 additions & 2 deletions libraries/grpc-sdk/src/modules/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof ConfigDefinition> {
private readonly emitter = new EventEmitter();
Expand Down Expand Up @@ -76,9 +77,27 @@ export class Config extends ConduitModule<typeof ConfigDefinition> {
});
}

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(
Expand Down
1 change: 1 addition & 0 deletions packages/core/README.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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` |
Expand Down
42 changes: 11 additions & 31 deletions packages/core/src/config-manager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -148,39 +148,19 @@ export default class ConfigManager implements IConfigManager {
call: GrpcRequest<null>,
callback: GrpcCallback<GetRedisDetailsResponse>,
) {
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,
});
}
}
Expand Down
7 changes: 6 additions & 1 deletion packages/core/src/core.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit dde5597

Please sign in to comment.