Skip to content

Commit

Permalink
fix(api): downed network status
Browse files Browse the repository at this point in the history
  • Loading branch information
hbriese committed Dec 14, 2023
1 parent a72c420 commit 1214350
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 23 deletions.
4 changes: 2 additions & 2 deletions api/src/features/health/health.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class HealthController {
private health: HealthCheckService,
private dbHealth: DatabaseHealthIndicator,
private redisHealth: RedisHealthIndicator,
private providerHealth: NetworksHealthIndicator,
private networksHealth: NetworksHealthIndicator,
@InjectRedis()
private readonly redis: Redis,
@InjectRedisSubscriber()
Expand All @@ -29,7 +29,7 @@ export class HealthController {
() => this.dbHealth.check('Database'),
() => this.redisHealth.check('Redis::default', this.redis),
() => this.redisHealth.check('Redis::subscriber', this.redisSub),
() => this.providerHealth.check('Provider'),
() => this.networksHealth.check('Networks'),
]);
}
}
38 changes: 17 additions & 21 deletions api/src/features/util/networks/networks.health.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Injectable } from '@nestjs/common';
import { HealthIndicator } from '@nestjs/terminus';
import { Network, NetworksService } from './networks.service';
import { fromPromise } from 'neverthrow';
import { NetworksService } from './networks.service';

@Injectable()
export class NetworksHealthIndicator extends HealthIndicator {
Expand All @@ -10,31 +9,28 @@ export class NetworksHealthIndicator extends HealthIndicator {
}

async check(key: string) {
const networks: Network[] = []; // Array.fromAsync(...) when supported
for await (const network of this.networks) {
networks.push(network);
}

const statuses = Object.fromEntries(
await Promise.all(
networks.map(async (c) => {
const r = await fromPromise(
(async () => ({ blockNumber: await c.getBlockNumber() }))(),
(e) => ({ error: e as Error }),
);
[...this.networks.all()]
.filter((n) => n.chain.key !== 'zksync-local')
.map(async (c) => {
const status = await c.status();

return [
c.chain.key,
{
healthy: r.isOk(),
...(r.isOk() ? r.value : r.error),
},
] as const;
}),
return [
c.chain.key,
status === 'healthy'
? { status: 'up' }
: {
status: 'down',
error: status.name,
message: status.message,
},
] as const;
}),
),
);

const healthy = Object.values(statuses).every((r) => r.healthy);
const healthy = Object.values(statuses).every((r) => r.status === 'up');

return this.getStatus(key, healthy, statuses);
}
Expand Down

0 comments on commit 1214350

Please sign in to comment.