@sentio/runtime 2.18.1-rc.7 → 2.18.1-rc.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,6 +4,7 @@ import fs from 'fs-extra';
4
4
  import { compressionAlgorithms } from '@grpc/grpc-js';
5
5
  import commandLineArgs from 'command-line-args';
6
6
  import { createServer } from 'nice-grpc';
7
+ import { errorDetailsServerMiddleware } from 'nice-grpc-error-details';
7
8
  import { registry as niceGrpcRegistry, prometheusServerMiddleware } from 'nice-grpc-prometheus';
8
9
  import { register as globalRegistry, Registry } from 'prom-client';
9
10
  import http from 'http';
@@ -63,7 +64,9 @@ const server = createServer({
63
64
  'grpc.max_send_message_length': 128 * 1024 * 1024,
64
65
  'grpc.max_receive_message_length': 128 * 1024 * 1024,
65
66
  'grpc.default_compression_algorithm': compressionAlgorithms.gzip,
66
- }).use(prometheusServerMiddleware());
67
+ })
68
+ .use(prometheusServerMiddleware())
69
+ .use(errorDetailsServerMiddleware);
67
70
  const baseService = new ProcessorServiceImpl(async () => {
68
71
  const m = await import(options.target);
69
72
  if (options.debug) {
@@ -95,11 +98,13 @@ process
95
98
  })
96
99
  .on('uncaughtException', (err) => {
97
100
  console.error('Uncaught Exception, please checking if await is properly used', err);
98
- shutdownServers(1);
101
+ baseService.unhandled = err;
102
+ // shutdownServers(1)
99
103
  })
100
104
  .on('unhandledRejection', (reason, p) => {
101
105
  console.error('Unhandled Rejection, please checking if await is properly', reason);
102
- shutdownServers(1);
106
+ baseService.unhandled = reason;
107
+ // shutdownServers(1)
103
108
  });
104
109
  function shutdownServers(exitCode) {
105
110
  server.forceShutdown();
@@ -1 +1 @@
1
- {"version":3,"file":"processor-runner.js","sourceRoot":"","sources":["../src/processor-runner.ts"],"names":[],"mappings":";AAEA,OAAO,IAAI,MAAM,MAAM,CAAA;AACvB,OAAO,EAAE,MAAM,UAAU,CAAA;AAEzB,OAAO,EAAE,qBAAqB,EAAE,MAAM,eAAe,CAAA;AACrD,OAAO,eAAe,MAAM,mBAAmB,CAAA;AAC/C,OAAO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c/grpc-js'\nimport commandLineArgs from 'command-line-args'\nimport { createServer } from 'nice-grpc'\nimport { registry as niceGrpcRegistry, prometheusServerMiddleware } from 'nice-grpc-prometheus'\nimport { register as globalRegistry, Registry } from 'prom-client'\nimport http from 'http'\n\nimport { ProcessorDefinition } from './gen/processor/protos/processor.js'\nimport { ProcessorServiceImpl } from './service.js'\nimport { Endpoints } from './endpoints.js'\nimport { FullProcessorServiceImpl } from './full-service.js'\nimport { ChainConfig } from './chain-config.js'\nimport { setupJsonLogger } from './logger.js'\n\nconst mergedRegistry = Registry.merge([globalRegistry, niceGrpcRegistry])\n\nconst optionDefinitions = [\n { name: 'target', type: String, defaultOption: true },\n { name: 'port', alias: 'p', type: String, defaultValue: '4000' },\n { name: 'concurrency', type: Number, defaultValue: 4 },\n // { name: 'use-chainserver', type: Boolean, defaultValue: false },\n {\n name: 'chains-config',\n alias: 'c',\n type: String,\n defaultValue: 'chains-config.json',\n },\n { name: 'chainquery-server', type: String, defaultValue: '' },\n { name: 'pricefeed-server', type: String, defaultValue: '' },\n { name: 'log-format', type: String, defaultValue: 'console' },\n { name: 'debug', type: Boolean, defaultValue: false },\n]\n\nconst options = commandLineArgs(optionDefinitions, { partial: true })\n\nif (options['log-format'] === 'json') {\n setupJsonLogger()\n}\nif (options.debug) {\n console.log('Starting with', options.target)\n}\n\nconst fullPath = path.resolve(options['chains-config'])\nconst chainsConfig = fs.readJsonSync(fullPath)\n\nEndpoints.INSTANCE.concurrency = options.concurrency\nEndpoints.INSTANCE.chainQueryAPI = options['chainquery-server']\nEndpoints.INSTANCE.priceFeedAPI = options['pricefeed-server']\n\nfor (const [id, config] of Object.entries(chainsConfig)) {\n const chainConfig = config as ChainConfig\n if (chainConfig.ChainServer) {\n Endpoints.INSTANCE.chainServer.set(id, chainConfig.ChainServer)\n } else {\n const http = chainConfig.Https?.[0]\n if (http) {\n Endpoints.INSTANCE.chainServer.set(id, http)\n } else {\n console.error('not valid config for chain', id)\n }\n }\n}\n\nif (options.debug) {\n console.log('Starting Server', options)\n}\n\nconst server = createServer({\n 'grpc.max_send_message_length': 128 * 1024 * 1024,\n 'grpc.max_receive_message_length': 128 * 1024 * 1024,\n 'grpc.default_compression_algorithm': compressionAlgorithms.gzip,\n}).use(prometheusServerMiddleware())\n\nconst baseService = new ProcessorServiceImpl(async () => {\n const m = await import(options.target)\n if (options.debug) {\n console.log('Module loaded', m)\n }\n return m\n}, server.shutdown)\nconst service = new FullProcessorServiceImpl(baseService)\n\nserver.add(ProcessorDefinition, service)\n\nserver.listen('0.0.0.0:' + options.port)\n\nconsole.log('Processor Server Started at:', options.port)\n\nconst metricsPort = 4040\nconst httpServer = http\n .createServer(async function (req, res) {\n if (req.url && new URL(req.url, `http://${req.headers.host}`).pathname === '/metrics') {\n const metrics = await mergedRegistry.metrics()\n res.write(metrics)\n } else {\n res.writeHead(404)\n }\n res.end()\n })\n .listen(metricsPort)\n\nconsole.log('Metric Server Started at:', metricsPort)\n\nprocess\n .on('SIGINT', function () {\n shutdownServers(0)\n })\n .on('uncaughtException', (err) => {\n console.error('Uncaught Exception, please checking if await is properly used', err)\n shutdownServers(1)\n })\n .on('unhandledRejection', (reason, p) => {\n console.error('Unhandled Rejection, please checking if await is properly', reason)\n shutdownServers(1)\n })\n\nfunction shutdownServers(exitCode: number) {\n server.forceShutdown()\n console.log('RPC server shut down')\n\n httpServer.close(function () {\n console.log('Http server shut down')\n process.exit(exitCode)\n })\n}\n"]}
1
+ {"version":3,"file":"processor-runner.js","sourceRoot":"","sources":["../src/processor-runner.ts"],"names":[],"mappings":";AAEA,OAAO,IAAI,MAAM,MAAM,CAAA;AACvB,OAAO,EAAE,MAAM,UAAU,CAAA;AAEzB,OAAO,EAAE,qBAAqB,EAAE,MAAM,eAAe,CAAA;AACrD,OAAO,eAAe,MAAM,mBAAmB,CAAA;AAC/C,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAA;AACxC,OAAO,EAAE,4BAA4B,EAAE,MAAM,yBAAyB,CAAA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 'fs-extra'\n\nimport { compressionAlgorithms } from '@grpc/grpc-js'\nimport commandLineArgs from 'command-line-args'\nimport { createServer } from 'nice-grpc'\nimport { errorDetailsServerMiddleware } from 'nice-grpc-error-details'\nimport { registry as niceGrpcRegistry, prometheusServerMiddleware } from 'nice-grpc-prometheus'\nimport { register as globalRegistry, Registry } from 'prom-client'\nimport http from 'http'\n\nimport { ProcessorDefinition } from './gen/processor/protos/processor.js'\nimport { ProcessorServiceImpl } from './service.js'\nimport { Endpoints } from './endpoints.js'\nimport { FullProcessorServiceImpl } from './full-service.js'\nimport { ChainConfig } from './chain-config.js'\nimport { setupJsonLogger } from './logger.js'\n\nconst mergedRegistry = Registry.merge([globalRegistry, niceGrpcRegistry])\n\nconst optionDefinitions = [\n { name: 'target', type: String, defaultOption: true },\n { name: 'port', alias: 'p', type: String, defaultValue: '4000' },\n { name: 'concurrency', type: Number, defaultValue: 4 },\n // { name: 'use-chainserver', type: Boolean, defaultValue: false },\n {\n name: 'chains-config',\n alias: 'c',\n type: String,\n defaultValue: 'chains-config.json',\n },\n { name: 'chainquery-server', type: String, defaultValue: '' },\n { name: 'pricefeed-server', type: String, defaultValue: '' },\n { name: 'log-format', type: String, defaultValue: 'console' },\n { name: 'debug', type: Boolean, defaultValue: false },\n]\n\nconst options = commandLineArgs(optionDefinitions, { partial: true })\n\nif (options['log-format'] === 'json') {\n setupJsonLogger()\n}\nif (options.debug) {\n console.log('Starting with', options.target)\n}\n\nconst fullPath = path.resolve(options['chains-config'])\nconst chainsConfig = fs.readJsonSync(fullPath)\n\nEndpoints.INSTANCE.concurrency = options.concurrency\nEndpoints.INSTANCE.chainQueryAPI = options['chainquery-server']\nEndpoints.INSTANCE.priceFeedAPI = options['pricefeed-server']\n\nfor (const [id, config] of Object.entries(chainsConfig)) {\n const chainConfig = config as ChainConfig\n if (chainConfig.ChainServer) {\n Endpoints.INSTANCE.chainServer.set(id, chainConfig.ChainServer)\n } else {\n const http = chainConfig.Https?.[0]\n if (http) {\n Endpoints.INSTANCE.chainServer.set(id, http)\n } else {\n console.error('not valid config for chain', id)\n }\n }\n}\n\nif (options.debug) {\n console.log('Starting Server', options)\n}\n\nconst server = createServer({\n 'grpc.max_send_message_length': 128 * 1024 * 1024,\n 'grpc.max_receive_message_length': 128 * 1024 * 1024,\n 'grpc.default_compression_algorithm': compressionAlgorithms.gzip,\n})\n .use(prometheusServerMiddleware())\n .use(errorDetailsServerMiddleware)\nconst baseService = new ProcessorServiceImpl(async () => {\n const m = await import(options.target)\n if (options.debug) {\n console.log('Module loaded', m)\n }\n return m\n}, server.shutdown)\nconst service = new FullProcessorServiceImpl(baseService)\n\nserver.add(ProcessorDefinition, service)\n\nserver.listen('0.0.0.0:' + options.port)\n\nconsole.log('Processor Server Started at:', options.port)\n\nconst metricsPort = 4040\nconst httpServer = http\n .createServer(async function (req, res) {\n if (req.url && new URL(req.url, `http://${req.headers.host}`).pathname === '/metrics') {\n const metrics = await mergedRegistry.metrics()\n res.write(metrics)\n } else {\n res.writeHead(404)\n }\n res.end()\n })\n .listen(metricsPort)\n\nconsole.log('Metric Server Started at:', metricsPort)\n\nprocess\n .on('SIGINT', function () {\n shutdownServers(0)\n })\n .on('uncaughtException', (err) => {\n console.error('Uncaught Exception, please checking if await is properly used', err)\n baseService.unhandled = err\n // shutdownServers(1)\n })\n .on('unhandledRejection', (reason, p) => {\n console.error('Unhandled Rejection, please checking if await is properly', reason)\n baseService.unhandled = reason as Error\n // shutdownServers(1)\n })\n\nfunction shutdownServers(exitCode: number) {\n server.forceShutdown()\n console.log('RPC server shut down')\n\n httpServer.close(function () {\n console.log('Http server shut down')\n process.exit(exitCode)\n })\n}\n"]}
package/lib/service.d.ts CHANGED
@@ -2,6 +2,7 @@ import { CallContext } from 'nice-grpc';
2
2
  import { DataBinding, ProcessBindingResponse, ProcessBindingsRequest, ProcessConfigRequest, ProcessConfigResponse, ProcessorServiceImplementation, ProcessResult, StartRequest, Empty } from '@sentio/protos';
3
3
  export declare class ProcessorServiceImpl implements ProcessorServiceImplementation {
4
4
  private started;
5
+ unhandled: Error;
5
6
  private readonly loader;
6
7
  private readonly shutdownHandler?;
7
8
  constructor(loader: () => Promise<any>, shutdownHandler?: () => void);
package/lib/service.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { ServerError, Status } from 'nice-grpc';
2
+ import { RichServerError, DebugInfo } from 'nice-grpc-error-details';
2
3
  import { ProcessConfigResponse, } from '@sentio/protos';
3
4
  import { PluginManager } from './plugin.js';
4
5
  import { errorString, mergeProcessResults } from './utils.js';
@@ -7,6 +8,8 @@ BigInt.prototype.toJSON = function () {
7
8
  };
8
9
  export class ProcessorServiceImpl {
9
10
  started = false;
11
+ // When there is unhandled error, stop process and return unavailable error
12
+ unhandled;
10
13
  // private processorConfig: ProcessConfigResponse
11
14
  loader;
12
15
  shutdownHandler;
@@ -71,9 +74,6 @@ export class ProcessorServiceImpl {
71
74
  return {};
72
75
  }
73
76
  async processBindings(request, options) {
74
- if (!this.started) {
75
- throw new ServerError(Status.UNAVAILABLE, 'Service Not started.');
76
- }
77
77
  const promises = request.bindings.map((binding) => this.processBinding(binding));
78
78
  let promise;
79
79
  try {
@@ -93,6 +93,17 @@ export class ProcessorServiceImpl {
93
93
  };
94
94
  }
95
95
  async processBinding(request, options) {
96
+ if (!this.started) {
97
+ throw new ServerError(Status.UNAVAILABLE, 'Service Not started.');
98
+ }
99
+ if (this.unhandled) {
100
+ throw new RichServerError(Status.UNAVAILABLE, 'Unhandled exception/rejection in previous request' + errorString(this.unhandled), [
101
+ DebugInfo.fromPartial({
102
+ detail: this.unhandled.message,
103
+ stackEntries: this.unhandled.stack?.split('\n'),
104
+ }),
105
+ ]);
106
+ }
96
107
  const result = await PluginManager.INSTANCE.processBinding(request);
97
108
  recordRuntimeInfo(result, request.handlerType);
98
109
  return result;
@@ -1 +1 @@
1
- {"version":3,"file":"service.js","sourceRoot":"","sources":["../src/service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAe,WAAW,EAAE,MA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AA;QAE3C,sBAAsB;QACtB,gEAAgE;QAChE,2BAA2B;QAC3B,mBAAmB;QACnB,IAAI;QAEJ,OAAO;YACL,MAAM;SACP,CAAA;IACH,CAAC;IAED,KAAK,CAAC,cAAc,CAAC,OAAoB,EAAE,OAAqB;QAC9D,MAAM,MAAM,GAAG,MAAM,aAAa,CAAC,QAAQ,CAAC,cAAc,CAAC,OAAO,CAAC,CAAA;QACnE,iBAAiB,CAAC,MAAM,EAAE,OAAO,CAAC,WAAW,CAAC,CAAA;QAC9C,OAAO,MAAM,CAAA;IACf,CAAC;IAED,KAAK,CAAC,CAAC,qBAAqB,CAAC,QAAoC,EAAE,OAAoB;QACrF,IAAI,KAAK,EAAE,MAAM,OAAO,IAAI,QAAQ,EAAE;YACpC,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC,CAAA;YACjD,sBAAsB;YACtB,gEAAgE;YAChE,2BAA2B;YAC3B,mBAAmB;YACnB,IAAI;YACJ,MAAM;gBACJ,MAAM;gBACN,aAAa,EAAE,MAAM,CAAC,MAAM,EAAE,aAAa,IAAI,KAAK;aACrD,CAAA;SACF;IACH,CAAC;CACF;AAED,SAAS,iBAAiB,CAAC,OAAsB,EAAE,WAAwB;IACzE,KAAK,MAAM,IAAI,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,OAAO,CAAC,QAAQ,EAAE,OAAO,CAAC,MAAM,EAAE,OAAO,CAAC,OAAO,CAAC,EAAE;QACtF,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE;YACjB,CAAC,CAAC,WAAW,GAAG;gBACd,IAAI,EAAE,WAAW;aAClB,CAAA;QACH,CAAC,CAAC,CAAA;KACH;AACH,CAAC","sourcesContent":["import { CallContext, ServerError, Status } from 'nice-grpc'\n\nimport {\n DataBinding,\n HandlerType,\n ProcessBindingResponse,\n ProcessBindingsRequest,\n ProcessConfigRequest,\n ProcessConfigResponse,\n ProcessorServiceImplementation,\n ProcessResult,\n StartRequest,\n Empty,\n} from '@sentio/protos'\n\nimport { PluginManager } from './plugin.js'\nimport { errorString, mergeProcessResults } from './utils.js'\n;(BigInt.prototype as any).toJSON = function () {\n return this.toString()\n}\n\nexport class ProcessorServiceImpl implements ProcessorServiceImplementation {\n private started = false\n // private processorConfig: ProcessConfigResponse\n\n private readonly loader: () => Promise<any>\n\n private readonly shutdownHandler?: () => void\n\n constructor(loader: () => Promise<any>, shutdownHandler?: () => void) {\n this.loader = loader\n this.shutdownHandler = shutdownHandler\n }\n\n async getConfig(request: ProcessConfigRequest, context: CallContext): Promise<ProcessConfigResponse> {\n if (!this.started) {\n throw new ServerError(Status.UNAVAILABLE, 'Service Not started.')\n }\n // if (!this.processorConfig) {\n // throw new ServerError(Status.INTERNAL, 'Process config empty.')\n // }\n\n // Don't use .create to keep compatiblity\n const newConfig = ProcessConfigResponse.fromPartial({})\n await PluginManager.INSTANCE.configure(newConfig)\n return newConfig\n }\n\n //\n // async configure() {\n // this.processorConfig = ProcessConfigResponse.fromPartial({})\n // await PluginManager.INSTANCE.configure(this.processorConfig)\n // }\n\n async start(request: StartRequest, context: CallContext): Promise<Empty> {\n if (this.started) {\n return {}\n }\n\n try {\n // for (const plugin of ['@sentio/sdk', '@sentio/sdk/eth']) {\n // try {\n // await import(plugin)\n // } catch (e) {\n // console.error('Failed to load plugin: ', plugin)\n // }\n // }\n //\n // for (const plugin of ['@sentio/sdk/aptos', '@sentio/sdk/solana']) {\n // try {\n // await import(plugin)\n // } catch (e) {}\n // }\n\n await this.loader()\n } catch (e) {\n throw new ServerError(Status.INVALID_ARGUMENT, 'Failed to load processor: ' + errorString(e))\n }\n\n await PluginManager.INSTANCE.start(request)\n\n // try {\n // await this.configure()\n // } catch (e) {\n // throw new ServerError(Status.INTERNAL, 'Failed to start processor : ' + errorString(e))\n // }\n this.started = true\n return {}\n }\n\n async stop(request: Empty, context: CallContext): Promise<Empty> {\n console.log('Server Shutting down in 5 seconds')\n if (this.shutdownHandler) {\n setTimeout(this.shutdownHandler, 5000)\n }\n return {}\n }\n\n async processBindings(request: ProcessBindingsRequest, options?: CallContext): Promise<ProcessBindingResponse> {\n if (!this.started) {\n throw new ServerError(Status.UNAVAILABLE, 'Service Not started.')\n }\n\n const promises = request.bindings.map((binding) => this.processBinding(binding))\n let promise\n try {\n promise = await Promise.all(promises)\n } catch (e) {\n throw e\n }\n const result = mergeProcessResults(promise)\n\n // let updated = false\n // if (PluginManager.INSTANCE.stateDiff(this.processorConfig)) {\n // await this.configure()\n // updated = true\n // }\n\n return {\n result,\n }\n }\n\n async processBinding(request: DataBinding, options?: CallContext): Promise<ProcessResult> {\n const result = await PluginManager.INSTANCE.processBinding(request)\n recordRuntimeInfo(result, request.handlerType)\n return result\n }\n\n async *processBindingsStream(requests: AsyncIterable<DataBinding>, context: CallContext) {\n for await (const request of requests) {\n const result = await this.processBinding(request)\n // let updated = false\n // if (PluginManager.INSTANCE.stateDiff(this.processorConfig)) {\n // await this.configure()\n // updated = true\n // }\n yield {\n result,\n configUpdated: result.states?.configUpdated || false,\n }\n }\n }\n}\n\nfunction recordRuntimeInfo(results: ProcessResult, handlerType: HandlerType) {\n for (const list of [results.gauges, results.counters, results.events, results.exports]) {\n list.forEach((e) => {\n e.runtimeInfo = {\n from: handlerType,\n }\n })\n }\n}\n"]}
1
+ {"version":3,"file":"service.js","sourceRoot":"","sources":["../src/service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAe,WAAW,EAAE,MAAM,EAAE,MAAM,WAAW,CAAA;AAC5D,OAAO,EAAE,eAAe,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AAEpE,OAAO,EAML,qBAAqB,GAKtB,MAAM,gBAAgB,CAAA;AAEvB,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAC3C,OAAO,EAAE,WAAW,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAC5D;AAAC,MAAM,CAAC,SAAiB,CAAC,MAAM,GAAG;IAClC,OAAO,IAAI,C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port { RichServerError, DebugInfo } from 'nice-grpc-error-details'\n\nimport {\n DataBinding,\n HandlerType,\n ProcessBindingResponse,\n ProcessBindingsRequest,\n ProcessConfigRequest,\n ProcessConfigResponse,\n ProcessorServiceImplementation,\n ProcessResult,\n StartRequest,\n Empty,\n} from '@sentio/protos'\n\nimport { PluginManager } from './plugin.js'\nimport { errorString, mergeProcessResults } from './utils.js'\n;(BigInt.prototype as any).toJSON = function () {\n return this.toString()\n}\n\nexport class ProcessorServiceImpl implements ProcessorServiceImplementation {\n private started = false\n // When there is unhandled error, stop process and return unavailable error\n unhandled: Error\n // private processorConfig: ProcessConfigResponse\n\n private readonly loader: () => Promise<any>\n\n private readonly shutdownHandler?: () => void\n\n constructor(loader: () => Promise<any>, shutdownHandler?: () => void) {\n this.loader = loader\n this.shutdownHandler = shutdownHandler\n }\n\n async getConfig(request: ProcessConfigRequest, context: CallContext): Promise<ProcessConfigResponse> {\n if (!this.started) {\n throw new ServerError(Status.UNAVAILABLE, 'Service Not started.')\n }\n // if (!this.processorConfig) {\n // throw new ServerError(Status.INTERNAL, 'Process config empty.')\n // }\n\n // Don't use .create to keep compatiblity\n const newConfig = ProcessConfigResponse.fromPartial({})\n await PluginManager.INSTANCE.configure(newConfig)\n return newConfig\n }\n\n //\n // async configure() {\n // this.processorConfig = ProcessConfigResponse.fromPartial({})\n // await PluginManager.INSTANCE.configure(this.processorConfig)\n // }\n\n async start(request: StartRequest, context: CallContext): Promise<Empty> {\n if (this.started) {\n return {}\n }\n\n try {\n // for (const plugin of ['@sentio/sdk', '@sentio/sdk/eth']) {\n // try {\n // await import(plugin)\n // } catch (e) {\n // console.error('Failed to load plugin: ', plugin)\n // }\n // }\n //\n // for (const plugin of ['@sentio/sdk/aptos', '@sentio/sdk/solana']) {\n // try {\n // await import(plugin)\n // } catch (e) {}\n // }\n\n await this.loader()\n } catch (e) {\n throw new ServerError(Status.INVALID_ARGUMENT, 'Failed to load processor: ' + errorString(e))\n }\n\n await PluginManager.INSTANCE.start(request)\n\n // try {\n // await this.configure()\n // } catch (e) {\n // throw new ServerError(Status.INTERNAL, 'Failed to start processor : ' + errorString(e))\n // }\n this.started = true\n return {}\n }\n\n async stop(request: Empty, context: CallContext): Promise<Empty> {\n console.log('Server Shutting down in 5 seconds')\n if (this.shutdownHandler) {\n setTimeout(this.shutdownHandler, 5000)\n }\n return {}\n }\n\n async processBindings(request: ProcessBindingsRequest, options?: CallContext): Promise<ProcessBindingResponse> {\n const promises = request.bindings.map((binding) => this.processBinding(binding))\n let promise\n try {\n promise = await Promise.all(promises)\n } catch (e) {\n throw e\n }\n const result = mergeProcessResults(promise)\n\n // let updated = false\n // if (PluginManager.INSTANCE.stateDiff(this.processorConfig)) {\n // await this.configure()\n // updated = true\n // }\n\n return {\n result,\n }\n }\n\n async processBinding(request: DataBinding, options?: CallContext): Promise<ProcessResult> {\n if (!this.started) {\n throw new ServerError(Status.UNAVAILABLE, 'Service Not started.')\n }\n if (this.unhandled) {\n throw new RichServerError(\n Status.UNAVAILABLE,\n 'Unhandled exception/rejection in previous request' + errorString(this.unhandled),\n [\n DebugInfo.fromPartial({\n detail: this.unhandled.message,\n stackEntries: this.unhandled.stack?.split('\\n'),\n }),\n ]\n )\n }\n const result = await PluginManager.INSTANCE.processBinding(request)\n recordRuntimeInfo(result, request.handlerType)\n return result\n }\n\n async *processBindingsStream(requests: AsyncIterable<DataBinding>, context: CallContext) {\n for await (const request of requests) {\n const result = await this.processBinding(request)\n // let updated = false\n // if (PluginManager.INSTANCE.stateDiff(this.processorConfig)) {\n // await this.configure()\n // updated = true\n // }\n yield {\n result,\n configUpdated: result.states?.configUpdated || false,\n }\n }\n }\n}\n\nfunction recordRuntimeInfo(results: ProcessResult, handlerType: HandlerType) {\n for (const list of [results.gauges, results.counters, results.events, results.exports]) {\n list.forEach((e) => {\n e.runtimeInfo = {\n from: handlerType,\n }\n })\n }\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sentio/runtime",
3
- "version": "2.18.1-rc.7",
3
+ "version": "2.18.1-rc.9",
4
4
  "license": "Apache-2.0",
5
5
  "type": "module",
6
6
  "exports": {
@@ -27,14 +27,15 @@
27
27
  "nice-grpc": "^2.1.0",
28
28
  "nice-grpc-client-middleware-retry": "^3.1.2",
29
29
  "nice-grpc-common": "^2.0.2",
30
+ "nice-grpc-error-details": "^0.2.0",
30
31
  "nice-grpc-prometheus": "^0.1.2",
31
32
  "prom-client": "^14.2.0",
32
33
  "protobufjs": "^7.2.3",
33
34
  "winston": "^3.8.2",
34
- "@sentio/protos": "2.18.1-rc.7"
35
+ "@sentio/protos": "2.18.1-rc.9"
35
36
  },
36
37
  "peerDependencies": {
37
- "@sentio/sdk": "^2.18.1-rc.7"
38
+ "@sentio/sdk": "^2.18.1-rc.9"
38
39
  },
39
40
  "devDependencies": {
40
41
  "@types/command-line-args": "^5.2.0",
@@ -6,6 +6,7 @@ import fs from 'fs-extra'
6
6
  import { compressionAlgorithms } from '@grpc/grpc-js'
7
7
  import commandLineArgs from 'command-line-args'
8
8
  import { createServer } from 'nice-grpc'
9
+ import { errorDetailsServerMiddleware } from 'nice-grpc-error-details'
9
10
  import { registry as niceGrpcRegistry, prometheusServerMiddleware } from 'nice-grpc-prometheus'
10
11
  import { register as globalRegistry, Registry } from 'prom-client'
11
12
  import http from 'http'
@@ -74,8 +75,9 @@ const server = createServer({
74
75
  'grpc.max_send_message_length': 128 * 1024 * 1024,
75
76
  'grpc.max_receive_message_length': 128 * 1024 * 1024,
76
77
  'grpc.default_compression_algorithm': compressionAlgorithms.gzip,
77
- }).use(prometheusServerMiddleware())
78
-
78
+ })
79
+ .use(prometheusServerMiddleware())
80
+ .use(errorDetailsServerMiddleware)
79
81
  const baseService = new ProcessorServiceImpl(async () => {
80
82
  const m = await import(options.target)
81
83
  if (options.debug) {
@@ -112,11 +114,13 @@ process
112
114
  })
113
115
  .on('uncaughtException', (err) => {
114
116
  console.error('Uncaught Exception, please checking if await is properly used', err)
115
- shutdownServers(1)
117
+ baseService.unhandled = err
118
+ // shutdownServers(1)
116
119
  })
117
120
  .on('unhandledRejection', (reason, p) => {
118
121
  console.error('Unhandled Rejection, please checking if await is properly', reason)
119
- shutdownServers(1)
122
+ baseService.unhandled = reason as Error
123
+ // shutdownServers(1)
120
124
  })
121
125
 
122
126
  function shutdownServers(exitCode: number) {
package/src/service.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { CallContext, ServerError, Status } from 'nice-grpc'
2
+ import { RichServerError, DebugInfo } from 'nice-grpc-error-details'
2
3
 
3
4
  import {
4
5
  DataBinding,
@@ -21,6 +22,8 @@ import { errorString, mergeProcessResults } from './utils.js'
21
22
 
22
23
  export class ProcessorServiceImpl implements ProcessorServiceImplementation {
23
24
  private started = false
25
+ // When there is unhandled error, stop process and return unavailable error
26
+ unhandled: Error
24
27
  // private processorConfig: ProcessConfigResponse
25
28
 
26
29
  private readonly loader: () => Promise<any>
@@ -97,10 +100,6 @@ export class ProcessorServiceImpl implements ProcessorServiceImplementation {
97
100
  }
98
101
 
99
102
  async processBindings(request: ProcessBindingsRequest, options?: CallContext): Promise<ProcessBindingResponse> {
100
- if (!this.started) {
101
- throw new ServerError(Status.UNAVAILABLE, 'Service Not started.')
102
- }
103
-
104
103
  const promises = request.bindings.map((binding) => this.processBinding(binding))
105
104
  let promise
106
105
  try {
@@ -122,6 +121,21 @@ export class ProcessorServiceImpl implements ProcessorServiceImplementation {
122
121
  }
123
122
 
124
123
  async processBinding(request: DataBinding, options?: CallContext): Promise<ProcessResult> {
124
+ if (!this.started) {
125
+ throw new ServerError(Status.UNAVAILABLE, 'Service Not started.')
126
+ }
127
+ if (this.unhandled) {
128
+ throw new RichServerError(
129
+ Status.UNAVAILABLE,
130
+ 'Unhandled exception/rejection in previous request' + errorString(this.unhandled),
131
+ [
132
+ DebugInfo.fromPartial({
133
+ detail: this.unhandled.message,
134
+ stackEntries: this.unhandled.stack?.split('\n'),
135
+ }),
136
+ ]
137
+ )
138
+ }
125
139
  const result = await PluginManager.INSTANCE.processBinding(request)
126
140
  recordRuntimeInfo(result, request.handlerType)
127
141
  return result