Skip to content

Commit

Permalink
[core] add GET /health
Browse files Browse the repository at this point in the history
  • Loading branch information
allburov committed Feb 25, 2024
1 parent b210841 commit 591ca51
Show file tree
Hide file tree
Showing 13 changed files with 403 additions and 2 deletions.
8 changes: 8 additions & 0 deletions docs/site/content/en/docs/how-to/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ you'll get a webhook event with `hasMedia: True` field, but with no `media.url`.
}
```

### Health Check
<b>Health check is available in [WAHA Plus ![](/images/versions/plus.png)]({{< relref "/docs/how-to/plus-version" >}}) only.</b>

The following environment variables can be used to configure the [Health Check ->]({{< relref "/docs/how-to/other" >}}):
- `WHATSAPP_HEALTH_MEDIA_FILES_THRESHOLD_MB` - the threshold in MB for the media files storage. The default value is `100`.
- `WHATSAPP_HEALTH_SESSIONS_FILES_THRESHOLD_MB` - the threshold in MB for the sessions files storage. The default value is `100`.
- `WHATSAPP_HEALTH_MONGODB_TIMEOUT` - the timeout in milliseconds for the MongoDB health check. The default value is `5000`.

## Examples

#### Debug Mode
Expand Down
3 changes: 2 additions & 1 deletion docs/site/content/en/docs/how-to/engines/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ please [create an issue](https://github.com/devlikeapro/whatsapp-http-api/issues
| `GET /api/{session}/presence/{chatId}` || ✔️ ||
| `POST /api/{session}/presence/{chatId}/subscribe` || ✔️ ||
| **Other** | | | |
| `POST /api/version` || ✔️ ||
| `GET /api/version` || ✔️ ||
| `GET /health` ![](/images/versions/plus.png) | ✔️ | ✔️ | ✔️ |

| **Webhooks** | WEBJS | NOWEB | VENOM |
|-----------------------------------------------------|:-----:|:-----:|:-----:|
Expand Down
192 changes: 192 additions & 0 deletions docs/site/content/en/docs/how-to/other/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
---
title: "Other"
description: "Other features and API"
lead: ""
date: 2020-10-06T08:48:45+00:00
lastmod: 2020-10-06T08:48:45+00:00
draft: false
images: [ ]
weight: 802
---

This page provides useful information about other features and API that are not covered in the other sections.

## Health Check
<b>Health check is available in [WAHA Plus ![](/images/versions/plus.png)]({{< relref "/docs/how-to/plus-version" >}}) only.</b>

The health check endpoint is used to determine the health of the service.

```
GET /health
```

It returns a **200 OK** status code if the service is healthy.

The response format:
```json
{
"status": "ok",
"info": {
"metric1": {
"field": "value"
},
"metric2": {
"field": "value"
}
},
"error": {},
"details": {}
}
```

Where:
- `status`: `'error' | 'ok' | 'shutting_down'` - If any health indicator failed the status will be `'error'`. If the app is shutting down but still accepting HTTP requests, the health check will have the `'shutting_down'` status.
- `info`: Object containing information of each health indicator which is of status `'up'`, or in other words "healthy".
- `error`: Object containing information of each health indicator which is of status `'down'`, or in other words "unhealthy".
- `details`: Object containing detailed information of each health indicator.

### Health Check Indicators
Few things we check in the health check:
- Media files storage space - `mediaFiles.space`
- Sessions files storage space - `sessionsFiles.space`
- MongoDB connection - `mongodb`

### Configuration
The following environment variables can be used to configure the health check:
- `WHATSAPP_HEALTH_MEDIA_FILES_THRESHOLD_MB` - the threshold in MB for the media files storage. The default value is `100`.
- `WHATSAPP_HEALTH_SESSIONS_FILES_THRESHOLD_MB` - the threshold in MB for the sessions files storage. The default value is `100`.
- `WHATSAPP_HEALTH_MONGODB_TIMEOUT` - the timeout in milliseconds for the MongoDB health check. The default value is `5000`.

### Examples
**Healthy response** when you use [Local Storage]({{< relref "/docs/how-to/storages#sessions" >}}) for session authentication:

**200 OK**
```json
{
"status": "ok",
"info": {
"mediaFiles.space": {
"status": "up",
"path": "/tmp/whatsapp-files",
"diskPath": "/",
"free": 132979355648,
"threshold": 104857600
},
"sessionsFiles.space": {
"status": "up",
"path": "/app/.sessions",
"diskPath": "/",
"free": 132979355648,
"threshold": 104857600
}
},
"error": {},
"details": {
"mediaFiles.space": {
"status": "up",
"path": "/tmp/whatsapp-files",
"diskPath": "/",
"free": 132979355648,
"threshold": 104857600
},
"sessionsFiles.space": {
"status": "up",
"path": "/app/.sessions",
"diskPath": "/",
"free": 132979355648,
"threshold": 104857600
}
}
}
```

**Healthy response** when you use [MongoDB Storage]({{< relref "/docs/how-to/storages#sessions" >}}) for session authentication:

**200 OK**
```json
{
"status": "ok",
"info": {
"mediaFiles.space": {
"status": "up",
"path": "/tmp/whatsapp-files",
"diskPath": "/",
"free": 132977496064,
"threshold": 104857600
},
"mongodb": {
"status": "up",
"message": "Up and running"
}
},
"error": {},
"details": {
"mediaFiles.space": {
"status": "up",
"path": "/tmp/whatsapp-files",
"diskPath": "/",
"free": 132977496064,
"threshold": 104857600
},
"mongodb": {
"status": "up",
"message": "Up and running"
}
}
}
```

**Unhealthy response example**

**503 Service Unavailable**
```json
{
"status": "error",
"info": {
"mediaFiles.space": {
"status": "up",
"path": "/tmp/whatsapp-files",
"diskPath": "/",
"free": 132976623616,
"threshold": 104857600
}
},
"error": {
"mongodb": {
"status": "down",
"error": "Timeout"
}
},
"details": {
"mediaFiles.space": {
"status": "up",
"path": "/tmp/whatsapp-files",
"diskPath": "/",
"free": 132976623616,
"threshold": 104857600
},
"mongodb": {
"status": "down",
"error": "Timeout"
}
}
}
```



## Get version
Returns the version of the installed docker image.
```
GET /api/version
```

```json
{
"version": "2024.2.3",
"engine": "NOWEB",
"tier": "PLUS",
"browser": "/usr/bin/google-chrome-stable"
}
```

8 changes: 8 additions & 0 deletions docs/site/content/en/docs/how-to/storages/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ Under the hood, the WAHA stores the session data in the following directory stru
when you "logout" the session using [POST /api/sessions/logout]({{< relref "/docs/how-to/sessions#logout">}}) or providing `logout: True` in [POST /api/sessions/stop]({{< relref "/docs/how-to/sessions#stop">}})
it removes the directory with the session data.

### Health Check
The [WAHA Plus ![](/images/versions/plus.png)]({{< relref "/docs/how-to/plus-version" >}}) provides [the health check endpoint]({{< relref "/docs/how-to/other" >}}) that checks the local storage.

## Sessions - MongoDB
If you want to use the MongoDB to store the session data, you need to:
Expand Down Expand Up @@ -103,6 +105,9 @@ If you use the [MongoDB Atlas](https://www.mongodb.com/atlas/database) you must

For production please consider running the MongoDB server close to the WAHA server for the best performance and security reasons.

### Health Check
The [WAHA Plus ![](/images/versions/plus.png)]({{< relref "/docs/how-to/plus-version" >}}) provides [the health check endpoint]({{< relref "/docs/how-to/other" >}}) that checks the MongoDB connection.

## Media
When your WhatsApp instance receives media files, it stores them in the media storage.

Expand Down Expand Up @@ -136,6 +141,9 @@ Here's all the steps in one command:
docker run -v /path/to/on/host/.media:/app/.media -e WHATSAPP_FILES_FOLDER=/app/.media -e WHATSAPP_FILES_LIFETIME=0 -p 3000:3000/tcp devlikeapro/whatsapp-http-api-plus
```

### Health Check
The [WAHA Plus ![](/images/versions/plus.png)]({{< relref "/docs/how-to/plus-version" >}}) provides [the health check endpoint]({{< relref "/docs/how-to/other" >}}) that checks the local storage.

## Media - S3
If you're interested in using the S3 storage or any other cloud storage (like [self-hosted S3 - Minio](https://min.io/)),
please create an issue or vote for the S3 issue in [the GitHub repository](https://github.com/devlikeapro/whatsapp-http-api/issues?q=is%3Aissue+is%3Aopen+S3).
1 change: 1 addition & 0 deletions docs/site/content/en/docs/overview/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ You even can **subscribe to get new updates** there!
- Add support for [MongoDB as storage for Session data]({{< relref "/docs/how-to/storages" >}})
- Support persistent file storage for media files - [now you can save media files between container restarts]({{< relref "/docs/how-to/storages#media" >}})
- If you set `WHATSAPP_FILES_LIFETIME=0` environment variable - media files will be never deleted.
- Add `GET /api/health` endpoint to [check the health of the service](https://waha.devlike.pro/docs/how-to/other/)

## 2024.1
- Implement [Patron Portal](https://portal.devlike.pro/) where you can get your personal API key and manage your perks.
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,18 @@
"dependencies": {
"@adiwajshing/baileys": "github:WhiskeySockets/Baileys",
"@adiwajshing/keyed-db": "^0.2.4",
"@nestjs/axios": "^3.0.2",
"@nestjs/common": "^9.0.9",
"@nestjs/config": "^0.5.0",
"@nestjs/core": "^9.0.9",
"@nestjs/passport": "^9.0.0",
"@nestjs/platform-express": "^9.0.9",
"@nestjs/serve-static": "^2.1.3",
"@nestjs/swagger": "^7.1.11",
"@nestjs/terminus": "^10.2.3",
"@types/lodash": "^4.14.194",
"@types/ws": "^8.5.4",
"check-disk-space": "^3.4.0",
"class-validator": "^0.12.2",
"del": "^6.0.0",
"express-basic-auth": "^1.2.1",
Expand Down
17 changes: 17 additions & 0 deletions src/api/health.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Controller, Get } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import { HealthCheck } from '@nestjs/terminus';

import { WAHAHealthCheckService } from '../core/abc/WAHAHealthCheckService';

@Controller('health')
@ApiTags('other')
export class HealthController {
constructor(private wahaHealth: WAHAHealthCheckService) {}

@Get()
@HealthCheck()
async check() {
return this.wahaHealth.check();
}
}
21 changes: 21 additions & 0 deletions src/config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,25 @@ export class WhatsappConfigService {
getSwaggerAdvancedConfigEnabled(): boolean {
return this.configService.get('WHATSAPP_SWAGGER_CONFIG_ADVANCED', false);
}

getHealthMediaFilesThreshold(): number {
return this.configService.get<number>(
'WHATSAPP_HEALTH_MEDIA_FILES_THRESHOLD_MB',
100,
);
}

getHealthSessionFilesThreshold(): number {
return this.configService.get<number>(
'WHATSAPP_HEALTH_SESSION_FILES_THRESHOLD_MB',
100,
);
}

getHealthMongoTimeout(): number {
return this.configService.get<number>(
'WHATSAPP_HEALTH_MONGO_TIMEOUT_MS',
3000,
);
}
}
18 changes: 18 additions & 0 deletions src/core/abc/WAHAHealthCheckService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { ConsoleLogger, Injectable } from '@nestjs/common';
import { DiskHealthIndicator, HealthCheckService } from '@nestjs/terminus';
import type { HealthCheckResult } from '@nestjs/terminus/dist/health-check/health-check-result.interface';

import { WhatsappConfigService } from '../../config.service';
import { SessionManager } from './manager.abc';

@Injectable()
export abstract class WAHAHealthCheckService {
constructor(
protected sessionManager: SessionManager,
protected health: HealthCheckService,
protected log: ConsoleLogger,
protected config: WhatsappConfigService,
) {}

abstract check(): Promise<HealthCheckResult>;
}
10 changes: 10 additions & 0 deletions src/core/app.module.core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import { ConsoleLogger, Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { PassportModule } from '@nestjs/passport';
import { ServeStaticModule } from '@nestjs/serve-static';
import { TerminusModule } from '@nestjs/terminus';

import { AuthController } from '../api/auth.controller';
import { ChatsController } from '../api/chats.controller';
import { ChattingController } from '../api/chatting.controller';
import { ContactsController } from '../api/contacts.controller';
import { GroupsController } from '../api/groups.controller';
import { HealthController } from '../api/health.controller';
import { PresenceController } from '../api/presence.controller';
import { ScreenshotController } from '../api/screenshot.controller';
import {
Expand All @@ -18,6 +20,8 @@ import { StatusController } from '../api/status.controller';
import { VersionController } from '../api/version.controller';
import { WhatsappConfigService } from '../config.service';
import { SessionManager } from './abc/manager.abc';
import { WAHAHealthCheckService } from './abc/WAHAHealthCheckService';
import { WAHAHealthCheckServiceCore } from './health/WAHAHealthCheckServiceCore';
import { SessionManagerCore } from './manager.core';

export const IMPORTS = [
Expand All @@ -38,6 +42,7 @@ export const IMPORTS = [
},
}),
PassportModule,
TerminusModule,
];
export const CONTROLLERS = [
AuthController,
Expand All @@ -51,12 +56,17 @@ export const CONTROLLERS = [
PresenceController,
ScreenshotController,
VersionController,
HealthController,
];
const PROVIDERS = [
{
provide: SessionManager,
useClass: SessionManagerCore,
},
{
provide: WAHAHealthCheckService,
useClass: WAHAHealthCheckServiceCore,
},
WhatsappConfigService,
ConsoleLogger,
];
Expand Down
12 changes: 12 additions & 0 deletions src/core/health/WAHAHealthCheckServiceCore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Injectable } from '@nestjs/common';
import { HealthCheckResult } from '@nestjs/terminus';

import { WAHAHealthCheckService } from '../abc/WAHAHealthCheckService';
import { AvailableInPlusVersion } from '../exceptions';

@Injectable()
export class WAHAHealthCheckServiceCore extends WAHAHealthCheckService {
check(): Promise<HealthCheckResult> {
throw new AvailableInPlusVersion();
}
}
Loading

0 comments on commit 591ca51

Please sign in to comment.