Browse Source

Feature/improve cache verification in health check endpoint (#4868)

* Improve implementation of isHealthy() without using getKeys()

* Update changelog
pull/4871/head
Thomas Kaul 3 weeks ago
committed by GitHub
parent
commit
85aa323e84
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 1
      CHANGELOG.md
  2. 20
      apps/api/src/app/redis-cache/redis-cache.service.ts

1
CHANGELOG.md

@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Renamed the asset profile icon component to entity logo component and moved to `@ghostfolio/ui` - Renamed the asset profile icon component to entity logo component and moved to `@ghostfolio/ui`
- Renamed `Account` to `accounts` in the `User` database schema - Renamed `Account` to `accounts` in the `User` database schema
- Improved the cache verification in the health check endpoint (experimental)
- Improved the language localization for Catalan (`ca`) - Improved the language localization for Catalan (`ca`)
- Improved the language localization for French (`fr`) - Improved the language localization for French (`fr`)
- Improved the language localization for Polish (`pl`) - Improved the language localization for Polish (`pl`)

20
apps/api/src/app/redis-cache/redis-cache.service.ts

@ -79,12 +79,22 @@ export class RedisCacheService {
} }
public async isHealthy() { public async isHealthy() {
const testKey = '__health_check__';
const testValue = Date.now().toString();
try { try {
await Promise.race([ await Promise.race([
this.getKeys(), (async () => {
await this.set(testKey, testValue, ms('1 second'));
const result = await this.get(testKey);
if (result !== testValue) {
throw new Error('Redis health check failed: value mismatch');
}
})(),
new Promise((_, reject) => new Promise((_, reject) =>
setTimeout( setTimeout(
() => reject(new Error('Redis health check timeout')), () => reject(new Error('Redis health check failed: timeout')),
ms('2 seconds') ms('2 seconds')
) )
) )
@ -92,7 +102,13 @@ export class RedisCacheService {
return true; return true;
} catch (error) { } catch (error) {
Logger.error(error?.message, 'RedisCacheService');
return false; return false;
} finally {
try {
await this.remove(testKey);
} catch {}
} }
} }

Loading…
Cancel
Save