Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Jul 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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