Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ export class AppController {
}

@Get('health')
getHealth(): { status: string; timestamp: string } {
async getHealth(): Promise<{
status: string;
timestamp: string;
database: { status: 'up' | 'down'; details?: string };
redis: { status: 'up' | 'down'; details?: string };
}> {
return this.appService.getHealth();
}
}
35 changes: 31 additions & 4 deletions src/app.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ describe('AppService', () => {
let service: AppService;

beforeEach(() => {
service = new AppService();
service = new AppService(
{
query: jest.fn().mockResolvedValue([{ '?column?': 1 }]),
} as any,
{
ping: jest.fn().mockResolvedValue('PONG'),
} as any,
);
});

it('reports service info', () => {
Expand All @@ -13,9 +20,29 @@ describe('AppService', () => {
expect(info.name).toContain('InterChangableTrade');
});

it('reports health with a timestamp', () => {
const health = service.getHealth();
expect(health.status).toBe('healthy');
it('reports app health with live dependency checks', async () => {
const health = await service.getHealth();

expect(health.status).toBe('ok');
expect(health.database).toEqual({ status: 'up' });
expect(health.redis).toEqual({ status: 'up' });
expect(() => new Date(health.timestamp)).not.toThrow();
});

it('marks app health as degraded when dependencies fail', async () => {
const failingService = new AppService(
{
query: jest.fn().mockRejectedValue(new Error('db down')),
} as any,
{
ping: jest.fn().mockRejectedValue(new Error('redis down')),
} as any,
);

const health = await failingService.getHealth();

expect(health.status).toBe('degraded');
expect(health.database.status).toBe('down');
expect(health.redis.status).toBe('down');
});
});
56 changes: 52 additions & 4 deletions src/app.service.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,66 @@
import { Injectable } from '@nestjs/common';
import { Inject, Injectable } from '@nestjs/common';
import { InjectDataSource } from '@nestjs/typeorm';
import { DataSource } from 'typeorm';
import Redis from 'ioredis';
import { REDIS_CLIENT } from './redis/redis.module';

export type AppHealthStatus = 'ok' | 'degraded';

export type DependencyHealth = {
status: 'up' | 'down';
details?: string;
};

@Injectable()
export class AppService {
constructor(
@InjectDataSource() private readonly dataSource: DataSource,
@Inject(REDIS_CLIENT) private readonly redisClient: Redis,
) {}

getInfo(): { name: string; status: string } {
return {
name: 'InterChangableTrade-Core',
status: 'ok',
};
}

getHealth(): { status: string; timestamp: string } {
async getHealth(): Promise<{
status: AppHealthStatus;
timestamp: string;
database: DependencyHealth;
redis: DependencyHealth;
}> {
const timestamp = new Date().toISOString();

let databaseStatus: DependencyHealth = { status: 'down' };
let redisStatus: DependencyHealth = { status: 'down' };

try {
await this.dataSource.query('SELECT 1');
databaseStatus = { status: 'up' };
} catch (error) {
databaseStatus = {
status: 'down',
details: error instanceof Error ? error.message : 'Database check failed',
};
}

try {
await this.redisClient.ping();
redisStatus = { status: 'up' };
} catch (error) {
redisStatus = {
status: 'down',
details: error instanceof Error ? error.message : 'Redis check failed',
};
}

return {
status: 'healthy',
timestamp: new Date().toISOString(),
status: databaseStatus.status === 'up' && redisStatus.status === 'up' ? 'ok' : 'degraded',
timestamp,
database: databaseStatus,
redis: redisStatus,
};
}
}
Loading