Skip to content

Commit

Permalink
use enum for health
Browse files Browse the repository at this point in the history
  • Loading branch information
mhagel committed Feb 8, 2024
1 parent 8b8309a commit 974d1fa
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
5 changes: 5 additions & 0 deletions libs/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,8 @@ export type ChainEventNotification = {
created_at: Date;
ChainEvent: ChainEventAttributes;
};

export enum NodeHealth {
Failed = 'failed',
Healthy = 'healthy',
}
6 changes: 3 additions & 3 deletions libs/model/src/models/chain_node.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { BalanceType } from '@hicommonwealth/core';
import type { BalanceType, NodeHealthState } from '@hicommonwealth/core';
import type * as Sequelize from 'sequelize'; // must use "* as" to avoid scope errors
import type { DataTypes } from 'sequelize';
import type { ModelInstance, ModelStatic } from './types';
Expand All @@ -15,7 +15,7 @@ export type ChainNodeAttributes = {
ss58?: number;
name: string;
description?: string;
health?: string;
health?: NodeHealthState;
updated_at?: Date;
};

Expand Down Expand Up @@ -43,7 +43,7 @@ export default (
balance_type: { type: dataTypes.STRING, allowNull: false },
name: { type: dataTypes.STRING, allowNull: false },
description: { type: dataTypes.TEXT, allowNull: true },
health: { type: dataTypes.TEXT, allowNull: true },
health: { type: dataTypes.STRING, allowNull: true },
ss58: { type: dataTypes.INTEGER, allowNull: true },
bech32: { type: dataTypes.STRING, allowNull: true },
created_at: { type: dataTypes.DATE, allowNull: false },
Expand Down
10 changes: 5 additions & 5 deletions packages/commonwealth/server/util/cosmosProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
CacheDecorator,
lookupKeyDurationInReq,
} from '@hicommonwealth/adapters';
import { AppError, logger } from '@hicommonwealth/core';
import { AppError, NodeHealth, logger } from '@hicommonwealth/core';
import { DB } from '@hicommonwealth/model';
import axios from 'axios';
import bodyParser from 'body-parser';
Expand Down Expand Up @@ -49,7 +49,7 @@ function setupCosmosProxy(
chainNodeUrl = chainNode?.url?.trim();
let useProxy = !chainNodeUrl;
cosmos_chain_id = chainNode?.cosmos_chain_id;
previouslyFailed = chainNode?.health?.includes('failed');
previouslyFailed = chainNode?.health === NodeHealth.Failed;
if (previouslyFailed) {
const lastUpdate = chainNode?.updated_at?.getTime();
const healthPauseTimeout = new Date(
Expand Down Expand Up @@ -131,7 +131,7 @@ function setupCosmosProxy(
cosmos_chain_id = chainNode.cosmos_chain_id;
chainNodeRestUrl = chainNode.alt_wallet_url?.trim();
let useProxy = !chainNodeRestUrl;
previouslyFailed = chainNode.health?.includes('failed');
previouslyFailed = chainNode?.health === NodeHealth.Failed;

if (previouslyFailed) {
const lastUpdate = chainNode.updated_at?.getTime();
Expand Down Expand Up @@ -230,7 +230,7 @@ function setupCosmosProxy(
const failedChainNode = await models.ChainNode.findOne({
where: { cosmos_chain_id },
});
failedChainNode.health = 'failed';
failedChainNode.health = NodeHealth.Failed;
await failedChainNode.save();
log.trace(
`Problem with endpoint ${failedUrl}. Marking node as 'failed' for ${FALLBACK_NODE_DURATION} seconds.`,
Expand All @@ -249,7 +249,7 @@ function setupCosmosProxy(
const healthyChainNode = await models.ChainNode.findOne({
where: { cosmos_chain_id },
});
healthyChainNode.health = 'healthy';
healthyChainNode.health = NodeHealth.Healthy;
await healthyChainNode.save();
}
};
Expand Down

0 comments on commit 974d1fa

Please sign in to comment.