Skip to content

Commit

Permalink
Try to catch error if create server failed, also give retry build sch…
Browse files Browse the repository at this point in the history
…ema (#1153)

* Try to catch error if create server failed, also give retry build schema

* Throw error
  • Loading branch information
jiqiang90 committed Jul 4, 2022
1 parent 0a75bdb commit f2a9e4b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
6 changes: 6 additions & 0 deletions packages/common/src/project/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,9 @@ export class SemverVersionValidator implements ValidatorConstraintInterface {
return 'Version number must follow Semver rules';
}
}

export async function delay(sec: number): Promise<void> {
return new Promise((resolve) => {
setTimeout(resolve, sec * 1000);
});
}
1 change: 1 addition & 0 deletions packages/node/src/utils/promise.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2020-2022 OnFinality Limited authors & contributors
// SPDX-License-Identifier: Apache-2.0

//deprecate method, use delay from @subql/common
export async function delay(sec: number): Promise<void> {
return new Promise((resolve) => {
setTimeout(resolve, sec * 1000);
Expand Down
35 changes: 29 additions & 6 deletions packages/query/src/graphql/graphql.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
import PgPubSub from '@graphile/pg-pubsub';
import {Module, OnModuleDestroy, OnModuleInit} from '@nestjs/common';
import {HttpAdapterHost} from '@nestjs/core';
import {delay} from '@subql/common';
import {
ApolloServerPluginCacheControl,
ApolloServerPluginLandingPageDisabled,
ApolloServerPluginLandingPageGraphQLPlayground,
} from 'apollo-server-core';
import {ApolloServer} from 'apollo-server-express';
import ExpressPinoLogger from 'express-pino-logger';
import {execute, subscribe} from 'graphql';
import {execute, GraphQLSchema, subscribe} from 'graphql';
import {Pool} from 'pg';
import {makePluginHook} from 'postgraphile';
import {getPostGraphileBuilder, PostGraphileCoreOptions} from 'postgraphile-core';
Expand All @@ -25,7 +26,8 @@ import {queryComplexityPlugin} from './plugins/QueryComplexityPlugin';
import {ProjectService} from './project.service';

const {argv} = getYargsOption();

const SCHEMA_RETRY_INTERVAL = 10; //seconds
const SCHEMA_RETRY_NUMBER = 5;
@Module({
providers: [ProjectService],
})
Expand All @@ -43,13 +45,36 @@ export class GraphqlModule implements OnModuleInit, OnModuleDestroy {
if (!this.httpAdapterHost) {
return;
}
this.apolloServer = await this.createServer();
try {
this.apolloServer = await this.createServer();
} catch (e) {
throw new Error(`create apollo server failed, ${e.message}`);
}
}

async onModuleDestroy(): Promise<void> {
return this.apolloServer?.stop();
}

private async buildSchema(
dbSchema: string,
options: PostGraphileCoreOptions,
retries: number
): Promise<GraphQLSchema> {
if (retries > 0) {
try {
const builder = await getPostGraphileBuilder(this.pgPool, [dbSchema], options);
const graphqlSchema = builder.buildSchema();
return graphqlSchema;
} catch (e) {
await delay(SCHEMA_RETRY_INTERVAL);
return this.buildSchema(dbSchema, options, --retries);
}
} else {
throw new Error(`Failed to build schema ${dbSchema} ${SCHEMA_RETRY_NUMBER} times`);
}
}

private async createServer() {
const app = this.httpAdapterHost.httpAdapter.getInstance();
const httpServer = this.httpAdapterHost.httpAdapter.getHttpServer();
Expand All @@ -72,9 +97,7 @@ export class GraphqlModule implements OnModuleInit, OnModuleDestroy {
}
}

const builder = await getPostGraphileBuilder(this.pgPool, [dbSchema], options);

const schema = builder.buildSchema();
const schema = await this.buildSchema(dbSchema, options, SCHEMA_RETRY_NUMBER);

const apolloServerPlugins = [
ApolloServerPluginCacheControl({
Expand Down

0 comments on commit f2a9e4b

Please sign in to comment.