mirror of
https://github.com/cerc-io/watcher-ts
synced 2026-09-08 08:54:05 +00:00
Move event watcher to util (#262)
This commit is contained in:
@@ -1,21 +0,0 @@
|
||||
//
|
||||
// Copyright 2021 Vulcanize, Inc.
|
||||
//
|
||||
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import Handlebars from 'handlebars';
|
||||
import { Writable } from 'stream';
|
||||
|
||||
const TEMPLATE_FILE = './templates/events-template.handlebars';
|
||||
|
||||
/**
|
||||
* Writes the events file generated from a template to a stream.
|
||||
* @param outStream A writable output stream to write the events file to.
|
||||
*/
|
||||
export function exportEvents (outStream: Writable): void {
|
||||
const templateString = fs.readFileSync(path.resolve(__dirname, TEMPLATE_FILE)).toString();
|
||||
const template = Handlebars.compile(templateString);
|
||||
const events = template({});
|
||||
outStream.write(events);
|
||||
}
|
||||
@@ -24,7 +24,6 @@ import { generateArtifacts } from './artifacts';
|
||||
import { exportPackage } from './package';
|
||||
import { exportTSConfig } from './tsconfig';
|
||||
import { exportReadme } from './readme';
|
||||
import { exportEvents } from './events';
|
||||
import { exportJobRunner } from './job-runner';
|
||||
import { exportWatchContract } from './watch-contract';
|
||||
import { exportLint } from './lint';
|
||||
@@ -226,11 +225,6 @@ function generateWatcher (visitor: Visitor, contracts: any[], config: any) {
|
||||
: process.stdout;
|
||||
exportReadme(path.basename(outputDir), config.port, outStream);
|
||||
|
||||
outStream = outputDir
|
||||
? fs.createWriteStream(path.join(outputDir, 'src/events.ts'))
|
||||
: process.stdout;
|
||||
exportEvents(outStream);
|
||||
|
||||
outStream = outputDir
|
||||
? fs.createWriteStream(path.join(outputDir, 'src/job-runner.ts'))
|
||||
: process.stdout;
|
||||
|
||||
@@ -43,5 +43,6 @@ export const handler = async (argv: any): Promise<void> => {
|
||||
|
||||
{{/if}}
|
||||
await createCheckpointCmd.initIndexer(Indexer{{#if (subgraphPath)}}, graphWatcher{{/if}});
|
||||
|
||||
await createCheckpointCmd.exec();
|
||||
};
|
||||
|
||||
@@ -35,5 +35,6 @@ export const handler = async (argv: any): Promise<void> => {
|
||||
);
|
||||
|
||||
await verifyCheckpointCmd.initIndexer(Indexer, graphWatcher);
|
||||
|
||||
await verifyCheckpointCmd.exec(graphDb);
|
||||
};
|
||||
|
||||
@@ -1,70 +0,0 @@
|
||||
//
|
||||
// Copyright 2021 Vulcanize, Inc.
|
||||
//
|
||||
|
||||
import assert from 'assert';
|
||||
import { PubSub } from 'graphql-subscriptions';
|
||||
|
||||
import { EthClient } from '@cerc-io/ipld-eth-client';
|
||||
import {
|
||||
JobQueue,
|
||||
EventWatcher as BaseEventWatcher,
|
||||
EventWatcherInterface,
|
||||
QUEUE_BLOCK_PROCESSING,
|
||||
QUEUE_EVENT_PROCESSING,
|
||||
IndexerInterface
|
||||
} from '@cerc-io/util';
|
||||
|
||||
import { Indexer } from './indexer';
|
||||
|
||||
export class EventWatcher implements EventWatcherInterface {
|
||||
_ethClient: EthClient
|
||||
_indexer: Indexer
|
||||
_subscription: ZenObservable.Subscription | undefined
|
||||
_baseEventWatcher: BaseEventWatcher
|
||||
_pubsub: PubSub
|
||||
_jobQueue: JobQueue
|
||||
|
||||
constructor (ethClient: EthClient, indexer: IndexerInterface, pubsub: PubSub, jobQueue: JobQueue) {
|
||||
assert(ethClient);
|
||||
assert(indexer);
|
||||
|
||||
this._ethClient = ethClient;
|
||||
this._indexer = indexer as Indexer;
|
||||
this._pubsub = pubsub;
|
||||
this._jobQueue = jobQueue;
|
||||
this._baseEventWatcher = new BaseEventWatcher(this._ethClient, this._indexer, this._pubsub, this._jobQueue);
|
||||
}
|
||||
|
||||
getEventIterator (): AsyncIterator<any> {
|
||||
return this._baseEventWatcher.getEventIterator();
|
||||
}
|
||||
|
||||
getBlockProgressEventIterator (): AsyncIterator<any> {
|
||||
return this._baseEventWatcher.getBlockProgressEventIterator();
|
||||
}
|
||||
|
||||
async start (): Promise<void> {
|
||||
assert(!this._subscription, 'subscription already started');
|
||||
|
||||
await this.initBlockProcessingOnCompleteHandler();
|
||||
await this.initEventProcessingOnCompleteHandler();
|
||||
this._baseEventWatcher.startBlockProcessing();
|
||||
}
|
||||
|
||||
async stop (): Promise<void> {
|
||||
this._baseEventWatcher.stop();
|
||||
}
|
||||
|
||||
async initBlockProcessingOnCompleteHandler (): Promise<void> {
|
||||
this._jobQueue.onComplete(QUEUE_BLOCK_PROCESSING, async (job) => {
|
||||
await this._baseEventWatcher.blockProcessingCompleteHandler(job);
|
||||
});
|
||||
}
|
||||
|
||||
async initEventProcessingOnCompleteHandler (): Promise<void> {
|
||||
await this._jobQueue.onComplete(QUEUE_EVENT_PROCESSING, async (job) => {
|
||||
await this._baseEventWatcher.eventProcessingCompleteHandler(job);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -31,6 +31,7 @@ const main = async (): Promise<void> => {
|
||||
|
||||
{{/if}}
|
||||
await exportStateCmd.initIndexer(Indexer{{#if (subgraphPath)}}, graphWatcher{{/if}});
|
||||
|
||||
await exportStateCmd.exec();
|
||||
};
|
||||
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
// Copyright 2021 Vulcanize, Inc.
|
||||
//
|
||||
|
||||
import assert from 'assert';
|
||||
import 'reflect-metadata';
|
||||
import debug from 'debug';
|
||||
|
||||
@@ -14,7 +13,6 @@ import { getGraphDbAndWatcher } from '@cerc-io/graph-node';
|
||||
|
||||
import { Database{{#if (subgraphPath)}}, ENTITY_QUERY_TYPE_MAP, ENTITY_TO_LATEST_ENTITY_MAP{{/if}} } from './database';
|
||||
import { Indexer } from './indexer';
|
||||
import { EventWatcher } from './events';
|
||||
|
||||
const log = debug('vulcanize:fill');
|
||||
|
||||
@@ -33,7 +31,7 @@ export const main = async (): Promise<any> => {
|
||||
);
|
||||
|
||||
{{/if}}
|
||||
await fillCmd.initIndexer(Indexer, EventWatcher{{#if (subgraphPath)}}, graphWatcher{{/if}});
|
||||
await fillCmd.initIndexer(Indexer{{#if (subgraphPath)}}, graphWatcher{{/if}});
|
||||
|
||||
{{#if (subgraphPath)}}
|
||||
// Get contractEntitiesMap required for fill-state
|
||||
|
||||
@@ -12,7 +12,6 @@ import { getGraphDbAndWatcher } from '@cerc-io/graph-node';
|
||||
|
||||
import { Database{{#if (subgraphPath)}}, ENTITY_QUERY_TYPE_MAP, ENTITY_TO_LATEST_ENTITY_MAP{{/if}} } from '../database';
|
||||
import { Indexer } from '../indexer';
|
||||
import { EventWatcher } from '../events';
|
||||
import { State } from '../entity/State';
|
||||
|
||||
const log = debug('vulcanize:import-state');
|
||||
@@ -32,7 +31,8 @@ export const main = async (): Promise<any> => {
|
||||
);
|
||||
|
||||
{{/if}}
|
||||
await importStateCmd.initIndexer(Indexer, EventWatcher{{#if (subgraphPath)}}, graphWatcher{{/if}});
|
||||
await importStateCmd.initIndexer(Indexer{{#if (subgraphPath)}}, graphWatcher{{/if}});
|
||||
|
||||
await importStateCmd.exec(State{{#if (subgraphPath)}}, graphDb{{/if}});
|
||||
};
|
||||
|
||||
|
||||
@@ -31,6 +31,7 @@ const main = async (): Promise<void> => {
|
||||
|
||||
{{/if}}
|
||||
await indexBlockCmd.initIndexer(Indexer{{#if (subgraphPath)}}, graphWatcher{{/if}});
|
||||
|
||||
await indexBlockCmd.exec();
|
||||
};
|
||||
|
||||
|
||||
@@ -31,6 +31,7 @@ const main = async (): Promise<void> => {
|
||||
|
||||
{{/if}}
|
||||
await inspectCIDCmd.initIndexer(Indexer{{#if (subgraphPath)}}, graphWatcher{{/if}});
|
||||
|
||||
await inspectCIDCmd.exec();
|
||||
};
|
||||
|
||||
|
||||
@@ -54,7 +54,6 @@
|
||||
"ethers": "^5.4.4",
|
||||
"express": "^4.18.2",
|
||||
"graphql": "^15.5.0",
|
||||
"graphql-subscriptions": "^2.0.0",
|
||||
"json-bigint": "^1.0.0",
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"typeorm": "^0.2.32",
|
||||
|
||||
@@ -36,5 +36,6 @@ export const handler = async (argv: any): Promise<void> => {
|
||||
|
||||
{{/if}}
|
||||
await resetWatcherCmd.initIndexer(Indexer{{#if (subgraphPath)}}, graphWatcher{{/if}});
|
||||
|
||||
await resetWatcherCmd.exec();
|
||||
};
|
||||
|
||||
@@ -17,7 +17,7 @@ import {
|
||||
getResultState,
|
||||
setGQLCacheHints,
|
||||
IndexerInterface,
|
||||
EventWatcherInterface
|
||||
EventWatcher
|
||||
} from '@cerc-io/util';
|
||||
|
||||
import { Indexer } from './indexer';
|
||||
@@ -31,9 +31,8 @@ import { {{query.entityName}} } from './entity/{{query.entityName}}';
|
||||
|
||||
const log = debug('vulcanize:resolver');
|
||||
|
||||
export const createResolvers = async (indexerArg: IndexerInterface, eventWatcherArg: EventWatcherInterface): Promise<any> => {
|
||||
export const createResolvers = async (indexerArg: IndexerInterface, eventWatcher: EventWatcher): Promise<any> => {
|
||||
const indexer = indexerArg as Indexer;
|
||||
const eventWatcher = eventWatcherArg as EventWatcher;
|
||||
|
||||
const gqlCacheConfig = indexer.serverConfig.gqlCache;
|
||||
|
||||
|
||||
@@ -15,7 +15,6 @@ import { getGraphDbAndWatcher } from '@cerc-io/graph-node';
|
||||
import { createResolvers } from './resolvers';
|
||||
import { Indexer } from './indexer';
|
||||
import { Database{{#if (subgraphPath)}}, ENTITY_QUERY_TYPE_MAP, ENTITY_TO_LATEST_ENTITY_MAP{{/if}} } from './database';
|
||||
import { EventWatcher } from './events';
|
||||
|
||||
const log = debug('vulcanize:server');
|
||||
|
||||
@@ -34,9 +33,10 @@ export const main = async (): Promise<any> => {
|
||||
);
|
||||
|
||||
{{/if}}
|
||||
await serverCmd.initIndexer(Indexer, EventWatcher{{#if (subgraphPath)}}, graphWatcher{{/if}});
|
||||
await serverCmd.initIndexer(Indexer{{#if (subgraphPath)}}, graphWatcher{{/if}});
|
||||
|
||||
const typeDefs = fs.readFileSync(path.join(__dirname, 'schema.gql')).toString();
|
||||
|
||||
return serverCmd.exec(createResolvers, typeDefs);
|
||||
};
|
||||
|
||||
|
||||
@@ -31,6 +31,7 @@ const main = async (): Promise<void> => {
|
||||
|
||||
{{/if}}
|
||||
await watchContractCmd.initIndexer(Indexer{{#if (subgraphPath)}}, graphWatcher{{/if}});
|
||||
|
||||
await watchContractCmd.exec();
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user