Skip to content

Open Source Wealth Management Software. Angular + NestJS + Prisma + Nx + TypeScript 🤍

Feature/add error handling for redis connections (#2179)

Thomas Kauld9ced885e12d
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a98558fc97..60dd52304d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Unreleased
+### Added
+
+- Added error handling for the _Redis_ connections to keep the app running if the connection fails
+
### Fixed
- Fixed the missing values in the holdings table
diff --git a/apps/api/src/app/redis-cache/interfaces/redis-cache.interface.ts b/apps/api/src/app/redis-cache/interfaces/redis-cache.interface.ts
new file mode 100644
index 0000000000..194da0bc84
--- /dev/null
+++ b/apps/api/src/app/redis-cache/interfaces/redis-cache.interface.ts
@@ -0,0 +1,7 @@
+import { Cache } from 'cache-manager';
+
+import type { RedisStore } from './redis-store.interface';
+
+export interface RedisCache extends Cache {
+ store: RedisStore;
+}
diff --git a/apps/api/src/app/redis-cache/interfaces/redis-store.interface.ts b/apps/api/src/app/redis-cache/interfaces/redis-store.interface.ts
new file mode 100644
index 0000000000..83c20eeb0f
--- /dev/null
+++ b/apps/api/src/app/redis-cache/interfaces/redis-store.interface.ts
@@ -0,0 +1,8 @@
+import { Store } from 'cache-manager';
+import Redis from 'redis';
+
+export interface RedisStore extends Store {
+ getClient: () => Redis.RedisClient;
+ isCacheableValue: (value: any) => boolean;
+ name: 'redis';
+}
diff --git a/apps/api/src/app/redis-cache/redis-cache.service.ts b/apps/api/src/app/redis-cache/redis-cache.service.ts
index fb75460ed9..865a23aead 100644
--- a/apps/api/src/app/redis-cache/redis-cache.service.ts
+++ b/apps/api/src/app/redis-cache/redis-cache.service.ts
@@ -1,14 +1,21 @@
import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service';
import { UniqueAsset } from '@ghostfolio/common/interfaces';
-import { CACHE_MANAGER, Inject, Injectable } from '@nestjs/common';
-import { Cache } from 'cache-manager';
+import { CACHE_MANAGER, Inject, Injectable, Logger } from '@nestjs/common';
+
+import type { RedisCache } from './interfaces/redis-cache.interface';
@Injectable()
export class RedisCacheService {
public constructor(
- @Inject(CACHE_MANAGER) private readonly cache: Cache,
+ @Inject(CACHE_MANAGER) private readonly cache: RedisCache,
private readonly configurationService: ConfigurationService
- ) {}
+ ) {
+ const client = cache.store.getClient();
+
+ client.on('error', (error) => {
+ Logger.error(error, 'RedisCacheService');
+ });
+ }
public async get(key: string): Promise<string> {
return await this.cache.get(key);