Skip to content

Commit

Permalink
feature: report sync progress in logs during initialisation
Browse files Browse the repository at this point in the history
  • Loading branch information
rhyslbw committed Jan 27, 2021
1 parent d6649cf commit 83279c3
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 3 deletions.
Binary file added packages-cache/set-interval-async-1.0.34.tgz
Binary file not shown.
3 changes: 1 addition & 2 deletions packages/api-cardano-db-hasura/src/CardanoNodeClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class CardanoNodeClient {
}

public async getTip () {
const tip = this.cardanoCli.getTip()
const tip = await this.cardanoCli.getTip()
this.logger.debug('getTip', { module: 'CardanoNodeClient', value: tip })
return tip
}
Expand All @@ -46,7 +46,6 @@ export class CardanoNodeClient {
await fs.stat(process.env.CARDANO_NODE_SOCKET_PATH)
const { slotNo } = await this.getTip()
if (slotNo < this.currentEraFirstSlot) {
this.logger.debug('cardano-node tip', { module: 'CardanoNodeClient', value: slotNo })
this.logger.debug('currentEraFirstSlot', { module: 'CardanoNodeClient', value: this.currentEraFirstSlot })
this.logger.warn('cardano-node is still synchronizing', { module: 'CardanoNodeClient' })
throw new Error()
Expand Down
1 change: 1 addition & 0 deletions packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"graphql": "14.5.8",
"graphql-depth-limit": "^1.1.0",
"prom-client": "^11.5.3",
"set-interval-async": "^1.0.34",
"ts-custom-error": "^3.2.0",
"ts-log": "^2.2.3"
},
Expand Down
29 changes: 28 additions & 1 deletion packages/server/src/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { prometheusMetricsPlugin } from './apollo_server_plugins'
import { IntrospectionNotPermitted, TracingRequired } from './errors'
import { allowListMiddleware } from './express_middleware'
import { dummyLogger, Logger } from 'ts-log'
import { setIntervalAsync, SetIntervalAsyncTimer } from 'set-interval-async/dynamic'
import { clearIntervalAsync } from 'set-interval-async'

export type Config = {
allowIntrospection: boolean
Expand All @@ -31,6 +33,7 @@ export class Server {
private apolloServer: ApolloServer
private httpServer: http.Server
private schemas: GraphQLSchema[]
private syncProgress: SetIntervalAsyncTimer

constructor (
schemas: GraphQLSchema[],
Expand Down Expand Up @@ -94,9 +97,33 @@ export class Server {
this.logger.info(`GraphQL HTTP server at http://${this.config.listenAddress}:` +
`${this.config.apiPort}${this.apolloServer.graphqlPath} started`
)
this.logger.debug('Checking DB status', { module: 'Server' })
this.syncProgress = setIntervalAsync(async () => {
const result = await this.apolloServer.executeOperation(
{
query: `query getSyncStatus {
cardanoDbMeta {
initialized
syncPercentage
}
}`
}
)
if (result.errors !== undefined) {
this.logger.debug(JSON.stringify(result.errors))
return
}
if (result.data.cardanoDbMeta.initialized) {
this.logger.info('CardanoDB is initialized')
await clearIntervalAsync(this.syncProgress)
} else {
this.logger.info(`SyncPercentage: ${result.data.cardanoDbMeta.syncPercentage}`)
}
}, 5000)
}

shutdown () {
async shutdown () {
await clearIntervalAsync(this.syncProgress)
if (this.httpServer !== undefined) {
this.httpServer.close()
this.logger.info(`GraphQL HTTP server at http://${this.config.listenAddress}:` +
Expand Down
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8075,6 +8075,13 @@ set-interval-async@^1.0.33:
dependencies:
"@babel/runtime" "7.5.0"

set-interval-async@^1.0.34:
version "1.0.34"
resolved "https://registry.yarnpkg.com/set-interval-async/-/set-interval-async-1.0.34.tgz#3dcd299ce0067f11b73b23b69aa68872d46ce805"
integrity sha512-wwDVmeXwOaumkZ7FizY0ux8N0fk4KM1Bq7DfBGHxjD/NSSjcvSlj4D6HHIxzu415QngUr0YW97TXl/s1Ou/BHQ==
dependencies:
"@babel/runtime" "7.5.0"

set-value@^2.0.0, set-value@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/set-value/-/set-value-2.0.1.tgz#a18d40530e6f07de4228c7defe4227af8cad005b"
Expand Down

0 comments on commit 83279c3

Please sign in to comment.