Refactor server CLI to cli package (#253)

* Refactor server CLI to cli package

* Use server CLI from cli package in all watchers
This commit is contained in:
prathamesh0
2022-11-23 12:57:59 +05:30
committed by GitHub
parent 122a64c2f9
commit 0b33cc98c9
17 changed files with 199 additions and 321 deletions
+10 -4
View File
@@ -78,7 +78,9 @@ export class BaseCmd {
jobQueue: JobQueue,
graphWatcher?: GraphWatcher
) => IndexerInterface,
clients: { [key: string]: any } = {}
clients: { [key: string]: any } = {},
entityQueryTypeMap?: Map<any, any>,
entityToLatestEntityMap?: Map<any, any>
): Promise<void> {
assert(this._config);
@@ -100,7 +102,7 @@ export class BaseCmd {
// Check if subgraph watcher.
if (this._config.server.subgraphPath) {
const graphWatcher = await this._getGraphWatcher(this._database.baseDatabase);
const graphWatcher = await this._getGraphWatcher(this._database.baseDatabase, entityQueryTypeMap, entityToLatestEntityMap);
this._indexer = new Indexer(this._config.server, this._database, this._clients, ethProvider, this._jobQueue, graphWatcher);
await this._indexer.init();
@@ -130,12 +132,16 @@ export class BaseCmd {
this._eventWatcher = new EventWatcher(this._clients.ethClient, this._indexer, pubsub, this._jobQueue);
}
async _getGraphWatcher (baseDatabase: BaseDatabase): Promise<GraphWatcher> {
async _getGraphWatcher (
baseDatabase: BaseDatabase,
entityQueryTypeMap?: Map<any, any>,
entityToLatestEntityMap?: Map<any, any>
): Promise<GraphWatcher> {
assert(this._config);
assert(this._clients?.ethClient);
assert(this._ethProvider);
this._graphDb = new GraphDatabase(this._config.server, baseDatabase);
this._graphDb = new GraphDatabase(this._config.server, baseDatabase, entityQueryTypeMap, entityToLatestEntityMap);
await this._graphDb.init();
return new GraphWatcher(this._graphDb, this._clients.ethClient, this._ethProvider, this._config.server);
+1
View File
@@ -10,3 +10,4 @@ export * from './checkpoint/verify';
export * from './inspect-cid';
export * from './import-state';
export * from './export-state';
export * from './server';
+128
View File
@@ -0,0 +1,128 @@
//
// Copyright 2022 Vulcanize, Inc.
//
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import 'reflect-metadata';
import fs from 'fs';
import path from 'path';
import assert from 'assert';
import { ConnectionOptions } from 'typeorm';
import { PubSub } from 'graphql-subscriptions';
import express, { Application } from 'express';
import { ApolloServer } from 'apollo-server-express';
import { JsonRpcProvider } from '@ethersproject/providers';
import { GraphWatcher } from '@cerc-io/graph-node';
import { EthClient } from '@cerc-io/ipld-eth-client';
import {
DEFAULT_CONFIG_PATH,
JobQueue,
DatabaseInterface,
IndexerInterface,
ServerConfig,
Clients,
EventWatcherInterface,
KIND_ACTIVE,
createAndStartServer,
startGQLMetricsServer
} from '@cerc-io/util';
import { TypeSource } from '@graphql-tools/utils';
import { BaseCmd } from './base';
interface Arguments {
configFile: string;
importFile: string;
}
export class ServerCmd {
_argv?: Arguments
_baseCmd: BaseCmd;
constructor () {
this._baseCmd = new BaseCmd();
}
async initConfig<ConfigType> (): Promise<ConfigType> {
this._argv = this._getArgv();
assert(this._argv);
return this._baseCmd.initConfig(this._argv.configFile);
}
async init (
Database: new (config: ConnectionOptions,
serverConfig?: ServerConfig
) => DatabaseInterface,
Indexer: new (
serverConfig: ServerConfig,
db: DatabaseInterface,
clients: Clients,
ethProvider: JsonRpcProvider,
jobQueue: JobQueue,
graphWatcher?: GraphWatcher
) => IndexerInterface,
EventWatcher: new(
ethClient: EthClient,
indexer: IndexerInterface,
pubsub: PubSub,
jobQueue: JobQueue
) => EventWatcherInterface,
clients: { [key: string]: any } = {},
entityQueryTypeMap?: Map<any, any>,
entityToLatestEntityMap?: Map<any, any>
): Promise<void> {
await this.initConfig();
await this._baseCmd.init(Database, Indexer, clients, entityQueryTypeMap, entityToLatestEntityMap);
await this._baseCmd.initEventWatcher(EventWatcher);
}
async exec (
createResolvers: (indexer: IndexerInterface, eventWatcher: EventWatcherInterface) => Promise<any>,
typeDefs: TypeSource
): Promise<{
app: Application,
server: ApolloServer
}> {
const config = this._baseCmd.config;
const jobQueue = this._baseCmd.jobQueue;
const indexer = this._baseCmd.indexer;
const eventWatcher = this._baseCmd.eventWatcher;
assert(config);
assert(jobQueue);
assert(indexer);
assert(eventWatcher);
if (config.server.kind === KIND_ACTIVE) {
// Delete jobs to prevent creating jobs after completion of processing previous block.
await jobQueue.deleteAllJobs();
await eventWatcher.start();
}
const resolvers = await createResolvers(indexer, eventWatcher);
// Create an Express app
const app: Application = express();
const server = await createAndStartServer(app, typeDefs, resolvers, config.server);
await startGQLMetricsServer(config);
return { app, server };
}
_getArgv (): any {
return yargs(hideBin(process.argv))
.option('f', {
alias: 'config-file',
demandOption: true,
describe: 'configuration file path (toml)',
type: 'string',
default: DEFAULT_CONFIG_PATH
})
.argv;
}
}