Skip to content

Commit 399ef13

Browse files
slapec93Gergely Békési
andauthored
feat: make readiness logic configurable (#29)
* feat: make readiness logic configurable * fix: readiness logic * docs: update readme --------- Co-authored-by: Gergely Békési <gergely.bekesi@ethswarm.org>
1 parent d9cba1c commit 399ef13

4 files changed

Lines changed: 18 additions & 6 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ When `HOSTNAME` is set, the gateway resolves subdomains against the Bee node:
7070
| DATABASE_CONFIG || MySQL connection as JSON: `{"user":"","password":"","host":"","port":3306,"database":"","ssl":{"ca":"..."}}`. If unset, all database-backed features are disabled. |
7171
| DATABASE_PASSWORD || MySQL password (overrides the `password` field in `DATABASE_CONFIG`) |
7272
| MATTERMOST_WEBHOOK_URL || URL of the Mattermost incoming webhook used for alerts |
73+
| READINESS_MODE | `normal` | `normal`: check only Bee readiness, `strict`: check Bee readiness and Bee depth |
7374

7475
## Maintainers
7576

src/config.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
import { Duration } from '@ethersphere/bee-js'
22
import { Types } from 'cafe-utility'
33

4+
export enum ReadinessMode {
5+
Strict = 'strict',
6+
Normal = 'normal'
7+
}
8+
49
export interface AppConfig {
510
beeApiUrl: string
611
hostname: string
712
authorization?: string
813
instanceName?: string
914
moderationSecret?: string
1015
removePinHeader?: boolean
11-
readinessCheck?: boolean
16+
readinessMode?: ReadinessMode
1217
homepage?: string
1318
mattermostWebhookUrl?: string
1419
}
@@ -39,6 +44,7 @@ export type EnvironmentVariables = Partial<{
3944
// Server
4045
PORT: string
4146
HOSTNAME: string
47+
READINESS_MODE: ReadinessMode
4248

4349
// Moderation
4450
MODERATION_SECRET: string
@@ -87,6 +93,7 @@ export function getAppConfig(env: EnvironmentVariables): AppConfig {
8793
removePinHeader: env.REMOVE_PIN_HEADER ? env.REMOVE_PIN_HEADER === 'true' : true,
8894
homepage: env.HOMEPAGE,
8995
mattermostWebhookUrl: env.MATTERMOST_WEBHOOK_URL,
96+
readinessMode: env.READINESS_MODE === ReadinessMode.Strict ? ReadinessMode.Strict : ReadinessMode.Normal
9097
}
9198
}
9299

src/readiness.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@ import { Bee } from '@ethersphere/bee-js'
22
import { READINESS_TIMEOUT_MS } from './config'
33
import { logger } from './logger'
44

5-
export async function checkReadiness(bee: Bee): Promise<boolean> {
5+
export async function checkReadiness(bee: Bee, strict: boolean): Promise<boolean> {
66
try {
7-
const health = await bee.getHealth({ timeout: READINESS_TIMEOUT_MS })
7+
const readiness = await bee.getReadiness({ timeout: READINESS_TIMEOUT_MS })
8+
const isReady = readiness.status === 'ready'
9+
if (!strict) {
10+
return isReady
11+
}
812
const topology = await bee.getTopology({ timeout: READINESS_TIMEOUT_MS })
9-
return health.status === 'ok' && topology.depth >= 1 && topology.depth < 31
13+
return isReady && topology.depth >= 1 && topology.depth < 31
1014
} catch (error) {
1115
logger.error('failed to check readiness', error)
1216
return false

src/server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import bodyParser from 'body-parser'
44
import { Arrays, Strings, Types } from 'cafe-utility'
55
import express, { Application, NextFunction, Request, Response } from 'express'
66
import { checkChallenge, createChallenge } from './challenge'
7-
import { AppConfig } from './config'
7+
import { AppConfig, ReadinessMode } from './config'
88
import { ApprovalRequests } from './database/ApprovalRequests'
99
import { runQuery } from './database/Database'
1010
import { Reports } from './database/Reports'
@@ -78,7 +78,7 @@ export function createApp(config: AppConfig, stampManager: StampManager): Applic
7878
}
7979

8080
app.get('/readiness', async (_req, res) => {
81-
const ready = await checkReadiness(bee)
81+
const ready = await checkReadiness(bee, config.readinessMode === ReadinessMode.Strict)
8282

8383
if (ready) {
8484
res.sendStatus(200)

0 commit comments

Comments
 (0)