Skip to content

Commit

Permalink
Add db HC
Browse files Browse the repository at this point in the history
  • Loading branch information
SepehrGanji committed Feb 22, 2024
1 parent 6586ab5 commit 11d04de
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
6 changes: 6 additions & 0 deletions src/context/database-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { TokenRepository } from "./token-repository";
import { BlocksRepository } from "./blocks-repository";

export class DatabaseContext {
private dataSource: DataSource;
public readonly transactions!: TransactionRepository;
public readonly blockInfo!: BlocksRepository;
public readonly boxes!: BoxRepository;
Expand All @@ -28,6 +29,7 @@ export class DatabaseContext {
public readonly unconfirmedInputs!: UnconfirmedInputRepository;

constructor(dataSource: DataSource) {
this.dataSource = dataSource;
const context: RepositoryDataContext = {
dataSource,
graphQLDataLoader: new GraphQLDatabaseLoader(dataSource, { disableCache: true })
Expand All @@ -48,4 +50,8 @@ export class DatabaseContext {
this.dataInputs = new BaseRepository(DataInputEntity, "dti", { context, defaults });
this.inputs = new BaseRepository(InputEntity, "input", { context, defaults });
}

checkConnection = () => {
return this.dataSource.isInitialized;
};
}
22 changes: 15 additions & 7 deletions src/health-check.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
export const checkHealth = () => {
const checks = {};
import { DatabaseContext } from "./context/database-context";

const healthy = Object.values(checks).every(() => true);
export const checkHealth = async(dataContext: DatabaseContext): Promise<boolean> => {
/**
* Things to check:
* - Database connection
* - Redis connection
* - Node connection
* - Block watcher
*/
const checks = {
db: dataContext.checkConnection
};

return {
msg: "Custom message",
healthy
}
// const healthy = Object.values(checks).every(() => true);
// There's no feature in apollo-server-express to return custom error messages, so we leave it empty
throw new Error("");
};
18 changes: 7 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ import responseCachePlugin from "apollo-server-plugin-response-cache";
import { GraphQLSchema } from "graphql";
import depthLimit from "graphql-depth-limit";
import { blockWatcher } from "./block-watcher";
// import { redisClient } from "./caching";
import { redisClient } from "./caching";
import { DEFAULT_MAX_QUERY_DEPTH, MAX_CACHE_AGE } from "./consts";
import { DatabaseContext } from "./context/database-context";
import { initializeDataSource } from "./data-source";
import { generateSchema } from "./graphql/schema";
import { nodeService } from "./services";
import { checkHealth } from "./health-check";

const { TS_NODE_DEV, MAX_QUERY_DEPTH } = process.env;

Expand All @@ -40,9 +41,9 @@ async function startServer(schema: GraphQLSchema, dataContext: DatabaseContext)
defaultMaxAge: MAX_CACHE_AGE,
calculateHttpHeaders: true
}),
// responseCachePlugin({
// cache: new BaseRedisCache({ client: redisClient })
// }),
responseCachePlugin({
cache: new BaseRedisCache({ client: redisClient })
}),
ApolloServerPluginLandingPageGraphQLPlayground()
],
validationRules: [
Expand All @@ -56,19 +57,14 @@ async function startServer(schema: GraphQLSchema, dataContext: DatabaseContext)
}
)
],
onHealthCheck: () => {
return new Promise((resolve, reject) => {
if(1) resolve("Something should go here");
else reject();
})
}
onHealthCheck: checkHealth.bind(null, dataContext)
});
const { url } = await server.listen({ port: 3000 });
console.log(`🚀 Server ready at ${url}`);
}

async function startBlockWatcher(dataContext: DatabaseContext) {
blockWatcher.start(dataContext.headers);
blockWatcher.start(dataContext.headers).onNewBlock(() => redisClient.flushdb());

console.log("🚀 Block watcher started");
}

0 comments on commit 11d04de

Please sign in to comment.