This repository has been archived by the owner on May 29, 2023. It is now read-only.
generated from well-known-components/template-server
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement cluster-status endpoint (#80)
* feat: implement cluster-status endpoint * chore: setup subscription on connected event
- Loading branch information
1 parent
72c0ecd
commit cdce1ca
Showing
10 changed files
with
120 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { IHttpServerComponent } from '@well-known-components/interfaces' | ||
import { GlobalContext } from '../../types' | ||
|
||
// handlers arguments only type what they need, to make unit testing easier | ||
export async function clusterStatusHandler(context: IHttpServerComponent.DefaultContext<GlobalContext>) { | ||
const clusterStatus = await context.components.serviceDiscovery.getClusterStatus() | ||
return { | ||
body: clusterStatus | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { IBaseComponent } from '@well-known-components/interfaces' | ||
import { JSONCodec } from 'nats' | ||
import { BaseComponents, Subscription } from '../types' | ||
|
||
export type ServiceDiscoveryMessage = { | ||
serverName: string | ||
status: any | ||
} | ||
|
||
export type ClusterStatus = Map<string, any> | ||
export type LastStatusUpdate = Map<string, number> | ||
|
||
export type IServiceDiscoveryComponent = IBaseComponent & { | ||
getClusterStatus(): Promise<any> | ||
stop(): Promise<void> | ||
} | ||
|
||
export async function createServiceDiscoveryComponent( | ||
components: Pick<BaseComponents, 'messageBroker' | 'logs' | 'config'> | ||
): Promise<IServiceDiscoveryComponent> { | ||
let healthCheckTimer: NodeJS.Timer | ||
let subscription: Subscription | ||
|
||
const { messageBroker, logs, config } = components | ||
const logger = logs.getLogger('Service Discovery') | ||
const jsonCodec = JSONCodec() | ||
|
||
const clusterStatus = new Map<string, any>() | ||
const lastStatusUpdate = new Map<string, number>() | ||
|
||
messageBroker.events.on('connected', async () => { | ||
await setupServiceDiscovery() | ||
await setupHealthCheck() | ||
}) | ||
|
||
async function setupServiceDiscovery() { | ||
subscription = messageBroker.subscribe('service.discovery') | ||
;(async () => { | ||
for await (const message of subscription.generator) { | ||
try { | ||
const discoveryMsg = jsonCodec.decode(message.data) as ServiceDiscoveryMessage | ||
clusterStatus.set(discoveryMsg.serverName, discoveryMsg.status) | ||
lastStatusUpdate.set(discoveryMsg.serverName, Date.now()) | ||
} catch (err: any) { | ||
logger.error(`Could not decode status discovery message: ${err.message}`) | ||
} | ||
} | ||
})().catch((err: any) => logger.error(`error processing subscription message; ${err.toString()}`)) | ||
} | ||
|
||
async function setupHealthCheck() { | ||
const interval = await config.requireNumber('SERVICE_DISCOVERY_HEALTH_CHECK_INTERVAL') | ||
healthCheckTimer = setInterval(() => { | ||
for (const [serverName, lastUpdate] of lastStatusUpdate) { | ||
const unhealthy = lastUpdate < Date.now() - interval | ||
if (unhealthy) { | ||
clusterStatus.delete(serverName) | ||
lastStatusUpdate.delete(serverName) | ||
} | ||
} | ||
}, interval) | ||
} | ||
|
||
async function getClusterStatus() { | ||
return Object.fromEntries(clusterStatus) | ||
} | ||
|
||
async function stop() { | ||
clearInterval(healthCheckTimer) | ||
subscription?.unsubscribe() | ||
} | ||
|
||
return { | ||
getClusterStatus, | ||
stop | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters