Skip to content

Commit

Permalink
Fix lint issues, move worker builder to node core
Browse files Browse the repository at this point in the history
  • Loading branch information
stwiname committed Jul 29, 2022
1 parent 119571e commit 6b972b1
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 20 deletions.
1 change: 1 addition & 0 deletions packages/node-core/src/indexer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ export * from './StoreOperations';
export * from './store.service';
export * from './poi.service';
export * from './mmr.service';
export * from './worker';
4 changes: 4 additions & 0 deletions packages/node-core/src/indexer/worker/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Copyright 2020-2022 OnFinality Limited authors & contributors
// SPDX-License-Identifier: Apache-2.0

export * from './worker.builder';
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// SPDX-License-Identifier: Apache-2.0

import * as workers from 'worker_threads';
import { Logger } from 'pino';
import { getLogger } from '@subql/node-core/logger';
import {Logger} from 'pino';
import {getLogger} from '../..//logger';

export type Request = {
id: number | string;
Expand All @@ -17,9 +17,7 @@ export type Response<T = any> = {
result?: T;
};

export type AsyncFunc<T = any> = (
...args: any[]
) => T | Promise<T | void> | void;
export type AsyncFunc<T = any> = (...args: any[]) => T | Promise<T | void> | void;
type AsyncMethods = Record<string, AsyncFunc>;

// TODO can we pass an event emitter rather than use parent port
Expand Down Expand Up @@ -71,10 +69,7 @@ export class Worker<T extends AsyncMethods> {
private worker: workers.Worker;
private logger: Logger;

private responseListeners: Record<
number | string,
(data?: any, error?: string) => void
> = {};
private responseListeners: Record<number | string, (data?: any, error?: string) => void> = {};

private _reqCounter = 0;

Expand Down Expand Up @@ -109,18 +104,15 @@ export class Worker<T extends AsyncMethods> {
});

// Add expected methods to class
fns.map((fn: string) => {
if (this[fn]) {
fns.map((fn) => {
if ((this as any)[fn]) {
throw new Error(`Method ${fn} is already defined`);
}
Object.assign(this, { [fn]: (...args: any[]) => this.execute(fn, args) });
Object.assign(this, {[fn]: (...args: any[]) => this.execute(fn, args)});
});
}

static create<T extends AsyncMethods>(
path: string,
fns: (keyof T)[],
): Worker<T> & T {
static create<T extends AsyncMethods>(path: string, fns: (keyof T)[]): Worker<T> & T {
const worker = new Worker(path, fns);

return worker as Worker<T> & T;
Expand All @@ -134,7 +126,7 @@ export class Worker<T extends AsyncMethods> {
return this.worker.terminate();
}

private async execute<T>(fnName: string, ...args: any[]): Promise<T> {
private async execute<T>(fnName: keyof T, ...args: any[]): Promise<T> {
const id = this.getReqId();

return new Promise<T>((resolve, reject) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { RuntimeVersion } from '@polkadot/types/interfaces';
import { hexToU8a, u8aEq } from '@polkadot/util';
import { NodeConfig } from '@subql/node-core/configure';
import { IndexerEvent } from '@subql/node-core/events';
import { Worker } from '@subql/node-core/indexer';
import { getLogger } from '@subql/node-core/logger';
import { SubstrateBlock } from '@subql/types';
import chalk from 'chalk';
Expand All @@ -29,7 +30,6 @@ import {
SetCurrentRuntimeVersion,
GetWorkerStatus,
} from './worker';
import { Worker } from './worker.builder';

const NULL_MERKEL_ROOT = hexToU8a('0x00');

Expand Down
2 changes: 1 addition & 1 deletion packages/node/src/indexer/worker/worker.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import { threadId } from 'node:worker_threads';
import { Injectable } from '@nestjs/common';
import { RuntimeVersion } from '@polkadot/types/interfaces';
import { getLogger } from '@subql/node-core/logger';
import { NodeConfig } from '@subql/node-core/configure';
import { getLogger } from '@subql/node-core/logger';
import { AutoQueue } from '../../utils/autoQueue';
import { fetchBlocksBatches } from '../../utils/substrate';
import { ApiService } from '../api.service';
Expand Down
2 changes: 1 addition & 1 deletion packages/node/src/indexer/worker/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import assert from 'assert';
import { threadId } from 'node:worker_threads';
import { INestApplication } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { registerWorker } from '@subql/node-core/indexer';
import { getLogger, NestLogger } from '@subql/node-core/logger';
import { IndexerManager } from '../indexer.manager';
import { registerWorker } from './worker.builder';
import { WorkerModule } from './worker.module';
import {
FetchBlockResponse,
Expand Down

0 comments on commit 6b972b1

Please sign in to comment.