@sentio/runtime 2.57.12-rc.c → 2.57.12-rc.e

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,2 +1,7 @@
1
+ import { DataBinding, ProcessResult } from '@sentio/protos';
1
2
 
2
- export { }
3
+ declare function export_default({ request }: {
4
+ request: DataBinding;
5
+ }): Promise<ProcessResult>;
6
+
7
+ export { export_default as default };
@@ -1,24 +1,23 @@
1
1
  import { createRequire as createRequireShim } from 'module'; const require = createRequireShim(import.meta.url);
2
2
  import {
3
- command_line_args_default,
4
- optionDefinitions
5
- } from "./chunk-ANRIDUBX.js";
6
- import {
7
- PluginManager,
8
- ProcessConfigResponse,
9
- __toESM,
10
3
  errorString,
11
4
  freezeGlobalConfig,
5
+ recordRuntimeInfo,
12
6
  require_lib2 as require_lib,
13
7
  require_lib3 as require_lib2
14
- } from "./chunk-HBDDXJ53.js";
8
+ } from "./chunk-YVOMEKB4.js";
9
+ import {
10
+ PluginManager,
11
+ ProcessConfigResponse,
12
+ __toESM
13
+ } from "./chunk-6YBY6GSE.js";
15
14
 
16
15
  // src/service-worker.ts
17
16
  var import_nice_grpc = __toESM(require_lib(), 1);
18
17
  var import_nice_grpc_error_details = __toESM(require_lib2(), 1);
19
18
  import { BroadcastChannel } from "worker_threads";
19
+ import { Piscina } from "piscina";
20
20
  var started = false;
21
- var options = command_line_args_default(optionDefinitions, { partial: true });
22
21
  var unhandled;
23
22
  process.on("uncaughtException", (err) => {
24
23
  console.error("Uncaught Exception, please checking if await is properly used", err);
@@ -38,22 +37,24 @@ async function getConfig(request, context) {
38
37
  await PluginManager.INSTANCE.configure(newConfig);
39
38
  return newConfig;
40
39
  }
41
- var loader = async () => {
42
- const m = await import(options.target);
43
- console.debug("Module loaded", m);
44
- return m;
40
+ var loader = async (options) => {
41
+ if (options.target) {
42
+ const m = await import(options.target);
43
+ console.debug("Module loaded, path:", options.target, "module:", m);
44
+ return m;
45
+ }
45
46
  };
46
47
  var configureChannel = new BroadcastChannel("configure_channel");
47
48
  configureChannel.onmessage = (request) => {
48
49
  getConfig(request);
49
50
  };
50
- async function start(request) {
51
+ async function start(request, options) {
51
52
  if (started) {
52
53
  return {};
53
54
  }
54
55
  freezeGlobalConfig();
55
56
  try {
56
- await loader();
57
+ await loader(options);
57
58
  } catch (e) {
58
59
  throw new import_nice_grpc.ServerError(import_nice_grpc.Status.INVALID_ARGUMENT, "Failed to load processor: " + errorString(e));
59
60
  }
@@ -61,9 +62,44 @@ async function start(request) {
61
62
  started = true;
62
63
  return {};
63
64
  }
64
- var startChannel = new BroadcastChannel("start_channel");
65
- startChannel.onmessage = (request) => {
66
- start(request);
67
- };
65
+ async function service_worker_default({ request }) {
66
+ if (!started) {
67
+ const { startRequest, configRequest, options } = Piscina.workerData;
68
+ if (startRequest) {
69
+ await start(startRequest, options);
70
+ }
71
+ if (configRequest) {
72
+ await getConfig(configRequest);
73
+ }
74
+ console.info("new processor worker started");
75
+ }
76
+ if (unhandled) {
77
+ throw new import_nice_grpc_error_details.RichServerError(
78
+ import_nice_grpc.Status.UNAVAILABLE,
79
+ "Unhandled exception/rejection in previous request: " + errorString(unhandled),
80
+ [
81
+ import_nice_grpc_error_details.DebugInfo.fromPartial({
82
+ detail: unhandled.message,
83
+ stackEntries: unhandled.stack?.split("\n")
84
+ })
85
+ ]
86
+ );
87
+ }
88
+ try {
89
+ const result = await PluginManager.INSTANCE.processBinding(
90
+ request,
91
+ void 0
92
+ // PluginManager.INSTANCE.dbContextLocalStorage.getStore()
93
+ );
94
+ recordRuntimeInfo(result, request.handlerType);
95
+ return result;
96
+ } catch (e) {
97
+ console.error("worker process binding failed", e);
98
+ throw e;
99
+ }
100
+ }
68
101
  import("node:process").then((p) => p.stdout.write(""));
102
+ export {
103
+ service_worker_default as default
104
+ };
69
105
  //# sourceMappingURL=service-worker.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/service-worker.ts"],"sourcesContent":["import {\n DataBinding,\n Empty,\n HandlerType,\n PreparedData,\n ProcessConfigRequest,\n ProcessConfigResponse,\n ProcessResult,\n StartRequest\n} from '@sentio/protos'\nimport { CallContext, ServerError, Status } from 'nice-grpc'\nimport { PluginManager } from './plugin.js'\nimport commandLineArgs from 'command-line-args'\nimport { optionDefinitions } from './processor-runner.js'\nimport { errorString } from './utils.js'\nimport { freezeGlobalConfig } from './global-config.js'\nimport { DebugInfo, RichServerError } from 'nice-grpc-error-details'\nimport { recordRuntimeInfo } from './service.js'\nimport { BroadcastChannel } from 'worker_threads'\n\nlet started = false\nconst options = commandLineArgs(optionDefinitions, { partial: true })\n\nlet unhandled: Error | undefined\n\nprocess\n .on('uncaughtException', (err) => {\n console.error('Uncaught Exception, please checking if await is properly used', err)\n unhandled = err\n })\n .on('unhandledRejection', (reason, p) => {\n // @ts-ignore ignore invalid ens error\n if (reason?.message.startsWith('invalid ENS name (disallowed character: \"*\"')) {\n return\n }\n console.error('Unhandled Rejection, please checking if await is properly', reason)\n unhandled = reason as Error\n // shutdownServers(1)\n })\n\nasync function getConfig(request: ProcessConfigRequest, context?: CallContext): Promise<ProcessConfigResponse> {\n if (!started) {\n throw new ServerError(Status.UNAVAILABLE, 'Service Not started.')\n }\n\n const newConfig = ProcessConfigResponse.fromPartial({})\n await PluginManager.INSTANCE.configure(newConfig)\n return newConfig\n}\n\nconst loader = async () => {\n const m = await import(options.target)\n console.debug('Module loaded', m)\n return m\n}\n\nconst configureChannel = new BroadcastChannel('configure_channel')\nconfigureChannel.onmessage = (request: ProcessConfigRequest) => {\n getConfig(request)\n}\n\nasync function start(request: StartRequest): Promise<Empty> {\n if (started) {\n return {}\n }\n freezeGlobalConfig()\n\n try {\n await 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 started = true\n return {}\n}\n\nconst startChannel = new BroadcastChannel('start_channel')\nstartChannel.onmessage = (request: StartRequest) => {\n start(request)\n}\n\nasync function stop(request: Empty, context: CallContext): Promise<Empty> {\n return {}\n}\n\nasync function processBinding(\n request: DataBinding,\n preparedData: PreparedData | undefined,\n options?: CallContext\n): Promise<ProcessResult> {\n if (!started) {\n throw new ServerError(Status.UNAVAILABLE, 'Service Not started.')\n }\n if (unhandled) {\n throw new RichServerError(\n Status.UNAVAILABLE,\n 'Unhandled exception/rejection in previous request: ' + errorString(unhandled),\n [\n DebugInfo.fromPartial({\n detail: unhandled.message,\n stackEntries: unhandled.stack?.split('\\n')\n })\n ]\n )\n }\n\n const result = await PluginManager.INSTANCE.processBinding(\n request,\n preparedData\n // PluginManager.INSTANCE.dbContextLocalStorage.getStore()\n )\n recordRuntimeInfo(result, request.handlerType)\n return result\n}\n;import(\"node:process\").then((p) => p.stdout.write(\"\"));"],"mappings":";;;;;;;;;;;;;;;;AAUA,uBAAiD;AAMjD,qCAA2C;AAE3C,SAAS,wBAAwB;AAEjC,IAAI,UAAU;AACd,IAAM,UAAU,0BAAgB,mBAAmB,EAAE,SAAS,KAAK,CAAC;AAEpE,IAAI;AAEJ,QACG,GAAG,qBAAqB,CAAC,QAAQ;AAChC,UAAQ,MAAM,iEAAiE,GAAG;AAClF,cAAY;AACd,CAAC,EACA,GAAG,sBAAsB,CAAC,QAAQ,MAAM;AAEvC,MAAI,QAAQ,QAAQ,WAAW,6CAA6C,GAAG;AAC7E;AAAA,EACF;AACA,UAAQ,MAAM,6DAA6D,MAAM;AACjF,cAAY;AAEd,CAAC;AAEH,eAAe,UAAU,SAA+B,SAAuD;AAC7G,MAAI,CAAC,SAAS;AACZ,UAAM,IAAI,6BAAY,wBAAO,aAAa,sBAAsB;AAAA,EAClE;AAEA,QAAM,YAAY,sBAAsB,YAAY,CAAC,CAAC;AACtD,QAAM,cAAc,SAAS,UAAU,SAAS;AAChD,SAAO;AACT;AAEA,IAAM,SAAS,YAAY;AACzB,QAAM,IAAI,MAAM,OAAO,QAAQ;AAC/B,UAAQ,MAAM,iBAAiB,CAAC;AAChC,SAAO;AACT;AAEA,IAAM,mBAAmB,IAAI,iBAAiB,mBAAmB;AACjE,iBAAiB,YAAY,CAAC,YAAkC;AAC9D,YAAU,OAAO;AACnB;AAEA,eAAe,MAAM,SAAuC;AAC1D,MAAI,SAAS;AACX,WAAO,CAAC;AAAA,EACV;AACA,qBAAmB;AAEnB,MAAI;AACF,UAAM,OAAO;AAAA,EACf,SAAS,GAAG;AACV,UAAM,IAAI,6BAAY,wBAAO,kBAAkB,+BAA+B,YAAY,CAAC,CAAC;AAAA,EAC9F;AAEA,QAAM,cAAc,SAAS,MAAM,OAAO;AAC1C,YAAU;AACV,SAAO,CAAC;AACV;AAEA,IAAM,eAAe,IAAI,iBAAiB,eAAe;AACzD,aAAa,YAAY,CAAC,YAA0B;AAClD,QAAM,OAAO;AACf;AAmCC,OAAO,cAAc,EAAE,KAAK,CAAC,MAAM,EAAE,OAAO,MAAM,EAAE,CAAC;","names":[]}
1
+ {"version":3,"sources":["../src/service-worker.ts"],"sourcesContent":["import {\n DataBinding,\n Empty,\n HandlerType,\n PreparedData,\n ProcessConfigRequest,\n ProcessConfigResponse,\n ProcessResult,\n StartRequest\n} from '@sentio/protos'\nimport { CallContext, ServerError, Status } from 'nice-grpc'\nimport { PluginManager } from './plugin.js'\nimport commandLineArgs from 'command-line-args'\nimport { optionDefinitions } from './processor-runner.js'\nimport { errorString } from './utils.js'\nimport { freezeGlobalConfig } from './global-config.js'\nimport { DebugInfo, RichServerError } from 'nice-grpc-error-details'\nimport { recordRuntimeInfo } from './service.js'\nimport { BroadcastChannel } from 'worker_threads'\nimport { Piscina } from 'piscina'\n\nlet started = false\n\nlet unhandled: Error | undefined\n\nprocess\n .on('uncaughtException', (err) => {\n console.error('Uncaught Exception, please checking if await is properly used', err)\n unhandled = err\n })\n .on('unhandledRejection', (reason, p) => {\n // @ts-ignore ignore invalid ens error\n if (reason?.message.startsWith('invalid ENS name (disallowed character: \"*\"')) {\n return\n }\n console.error('Unhandled Rejection, please checking if await is properly', reason)\n unhandled = reason as Error\n // shutdownServers(1)\n })\n\nasync function getConfig(request: ProcessConfigRequest, context?: CallContext): Promise<ProcessConfigResponse> {\n if (!started) {\n throw new ServerError(Status.UNAVAILABLE, 'Service Not started.')\n }\n\n const newConfig = ProcessConfigResponse.fromPartial({})\n await PluginManager.INSTANCE.configure(newConfig)\n return newConfig\n}\n\nconst loader = async (options: any) => {\n if (options.target) {\n const m = await import(options.target)\n console.debug('Module loaded, path:', options.target, \"module:\", m)\n return m\n }\n}\n\nconst configureChannel = new BroadcastChannel('configure_channel')\nconfigureChannel.onmessage = (request: ProcessConfigRequest) => {\n getConfig(request)\n}\n\nasync function start(request: StartRequest, options: any): Promise<Empty> {\n if (started) {\n return {}\n }\n freezeGlobalConfig()\n\n try {\n await loader(options)\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 started = true\n return {}\n}\n\nexport default async function ({ request }: { request: DataBinding }) {\n if (!started) {\n const { startRequest, configRequest, options } = Piscina.workerData\n if (startRequest) {\n await start(startRequest, options)\n }\n if (configRequest) {\n await getConfig(configRequest)\n }\n console.info('new processor worker started')\n }\n\n if (unhandled) {\n throw new RichServerError(\n Status.UNAVAILABLE,\n 'Unhandled exception/rejection in previous request: ' + errorString(unhandled),\n [\n DebugInfo.fromPartial({\n detail: unhandled.message,\n stackEntries: unhandled.stack?.split('\\n')\n })\n ]\n )\n }\n\n try {\n const result = await PluginManager.INSTANCE.processBinding(\n request,\n undefined\n // PluginManager.INSTANCE.dbContextLocalStorage.getStore()\n )\n recordRuntimeInfo(result, request.handlerType)\n return result\n } catch (e) {\n console.error(\"worker process binding failed\", e)\n throw e\n }\n}\n;import(\"node:process\").then((p) => p.stdout.write(\"\"));"],"mappings":";;;;;;;;;;;;;;;AAUA,uBAAiD;AAMjD,qCAA2C;AAE3C,SAAS,wBAAwB;AACjC,SAAS,eAAe;AAExB,IAAI,UAAU;AAEd,IAAI;AAEJ,QACG,GAAG,qBAAqB,CAAC,QAAQ;AAChC,UAAQ,MAAM,iEAAiE,GAAG;AAClF,cAAY;AACd,CAAC,EACA,GAAG,sBAAsB,CAAC,QAAQ,MAAM;AAEvC,MAAI,QAAQ,QAAQ,WAAW,6CAA6C,GAAG;AAC7E;AAAA,EACF;AACA,UAAQ,MAAM,6DAA6D,MAAM;AACjF,cAAY;AAEd,CAAC;AAEH,eAAe,UAAU,SAA+B,SAAuD;AAC7G,MAAI,CAAC,SAAS;AACZ,UAAM,IAAI,6BAAY,wBAAO,aAAa,sBAAsB;AAAA,EAClE;AAEA,QAAM,YAAY,sBAAsB,YAAY,CAAC,CAAC;AACtD,QAAM,cAAc,SAAS,UAAU,SAAS;AAChD,SAAO;AACT;AAEA,IAAM,SAAS,OAAO,YAAiB;AACrC,MAAI,QAAQ,QAAQ;AAClB,UAAM,IAAI,MAAM,OAAO,QAAQ;AAC/B,YAAQ,MAAM,wBAAwB,QAAQ,QAAQ,WAAW,CAAC;AAClE,WAAO;AAAA,EACT;AACF;AAEA,IAAM,mBAAmB,IAAI,iBAAiB,mBAAmB;AACjE,iBAAiB,YAAY,CAAC,YAAkC;AAC9D,YAAU,OAAO;AACnB;AAEA,eAAe,MAAM,SAAuB,SAA8B;AACxE,MAAI,SAAS;AACX,WAAO,CAAC;AAAA,EACV;AACA,qBAAmB;AAEnB,MAAI;AACF,UAAM,OAAO,OAAO;AAAA,EACtB,SAAS,GAAG;AACV,UAAM,IAAI,6BAAY,wBAAO,kBAAkB,+BAA+B,YAAY,CAAC,CAAC;AAAA,EAC9F;AAEA,QAAM,cAAc,SAAS,MAAM,OAAO;AAC1C,YAAU;AACV,SAAO,CAAC;AACV;AAEA,eAAO,uBAAwB,EAAE,QAAQ,GAA6B;AACpE,MAAI,CAAC,SAAS;AACZ,UAAM,EAAE,cAAc,eAAe,QAAQ,IAAI,QAAQ;AACzD,QAAI,cAAc;AAChB,YAAM,MAAM,cAAc,OAAO;AAAA,IACnC;AACA,QAAI,eAAe;AACjB,YAAM,UAAU,aAAa;AAAA,IAC/B;AACA,YAAQ,KAAK,8BAA8B;AAAA,EAC7C;AAEA,MAAI,WAAW;AACb,UAAM,IAAI;AAAA,MACR,wBAAO;AAAA,MACP,wDAAwD,YAAY,SAAS;AAAA,MAC7E;AAAA,QACE,yCAAU,YAAY;AAAA,UACpB,QAAQ,UAAU;AAAA,UAClB,cAAc,UAAU,OAAO,MAAM,IAAI;AAAA,QAC3C,CAAC;AAAA,MACH;AAAA,IACF;AAAA,EACF;AAEA,MAAI;AACF,UAAM,SAAS,MAAM,cAAc,SAAS;AAAA,MAC1C;AAAA,MACA;AAAA;AAAA,IAEF;AACA,sBAAkB,QAAQ,QAAQ,WAAW;AAC7C,WAAO;AAAA,EACT,SAAS,GAAG;AACV,YAAQ,MAAM,iCAAiC,CAAC;AAChD,UAAM;AAAA,EACR;AACF;AACC,OAAO,cAAc,EAAE,KAAK,CAAC,MAAM,EAAE,OAAO,MAAM,EAAE,CAAC;","names":[]}
@@ -0,0 +1,2 @@
1
+
2
+ export { }
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/test-processor.test.ts"],"sourcesContent":["import { Plugin, PluginManager } from './plugin.js'\nimport { DataBinding, HandlerType, ProcessResult } from './gen/processor/protos/processor.js'\n\nclass TestPlugin extends Plugin {\n async processBinding(request: DataBinding): Promise<ProcessResult> {\n return ProcessResult.fromPartial({\n states: {\n configUpdated: true\n }\n })\n }\n supportedHandlers = [HandlerType.UNKNOWN, HandlerType.APT_EVENT]\n}\n\nPluginManager.INSTANCE.plugins = []\nPluginManager.INSTANCE.register(new TestPlugin())\n;import(\"node:process\").then((p) => p.stdout.write(\"\"));"],"mappings":";;;;;;;;AAGA,IAAM,aAAN,cAAyB,OAAO;AAAA,EAC9B,MAAM,eAAe,SAA8C;AACjE,WAAO,cAAc,YAAY;AAAA,MAC/B,QAAQ;AAAA,QACN,eAAe;AAAA,MACjB;AAAA,IACF,CAAC;AAAA,EACH;AAAA,EACA,oBAAoB,mCAA2C;AACjE;AAEA,cAAc,SAAS,UAAU,CAAC;AAClC,cAAc,SAAS,SAAS,IAAI,WAAW,CAAC;AAC/C,OAAO,cAAc,EAAE,KAAK,CAAC,MAAM,EAAE,OAAO,MAAM,EAAE,CAAC;","names":[]}
package/package.json CHANGED
@@ -1,11 +1,10 @@
1
1
  {
2
2
  "name": "@sentio/runtime",
3
- "version": "2.57.12-rc.c",
3
+ "version": "2.57.12-rc.e",
4
4
  "license": "Apache-2.0",
5
5
  "type": "module",
6
6
  "exports": {
7
- ".": "./lib/index.js",
8
- "./service-worker": "./lib/service-worker.js"
7
+ ".": "./lib/index.js"
9
8
  },
10
9
  "bin": {
11
10
  "processor-runner": "./lib/processor-runner.js"
@@ -42,20 +42,13 @@ export class ServiceManager implements ProcessorServiceImplementation {
42
42
  // When there is unhandled error, stop process and return unavailable error
43
43
  unhandled: Error
44
44
  // private processorConfig: ProcessConfigResponse
45
- private readonly pool: Piscina<any, any>
45
+ private pool: Piscina<any, any>
46
46
 
47
47
  constructor(
48
48
  readonly options: any,
49
49
  readonly loader: () => Promise<any>,
50
50
  readonly shutdownHandler?: () => void
51
- ) {
52
- this.pool = new Piscina({
53
- maxThreads: options.worker,
54
- minThreads: options.worker,
55
- filename: new URL('./service-worker.js', import.meta.url).href,
56
- argv: process.argv
57
- })
58
- }
51
+ ) {}
59
52
 
60
53
  async *preprocessBindingsStream(requests: AsyncIterable<PreprocessStreamRequest>, context: CallContext) {
61
54
  throw new Error('not supported')
@@ -73,15 +66,13 @@ export class ServiceManager implements ProcessorServiceImplementation {
73
66
  // also configure the main thread
74
67
  const newConfig = ProcessConfigResponse.fromPartial({})
75
68
  await PluginManager.INSTANCE.configure(newConfig)
69
+ this.pool.options.workerData = {
70
+ configRequest: request,
71
+ ...this.pool.options.workerData
72
+ }
76
73
  return newConfig
77
74
  }
78
75
 
79
- //
80
- // async configure() {
81
- // this.processorConfig = ProcessConfigResponse.fromPartial({})
82
- // await PluginManager.INSTANCE.configure(this.processorConfig)
83
- // }
84
-
85
76
  async start(request: StartRequest, context: CallContext): Promise<Empty> {
86
77
  if (this.started) {
87
78
  return {}
@@ -93,9 +84,16 @@ export class ServiceManager implements ProcessorServiceImplementation {
93
84
  throw new ServerError(Status.INVALID_ARGUMENT, 'Failed to load processor: ' + errorString(e))
94
85
  }
95
86
 
96
- // broadcast to all worker
97
- const bc = new BroadcastChannel('start_channel')
98
- bc.postMessage(request)
87
+ this.pool = new Piscina({
88
+ maxThreads: this.options.worker,
89
+ minThreads: this.options.worker,
90
+ filename: new URL('./service-worker.js', import.meta.url).href.replaceAll('runtime/src', 'runtime/lib'),
91
+ argv: process.argv,
92
+ workerData: {
93
+ startRequest: request,
94
+ options: this.options
95
+ }
96
+ })
99
97
 
100
98
  // also start the processor in main thread
101
99
  await PluginManager.INSTANCE.start(request)
@@ -114,7 +112,7 @@ export class ServiceManager implements ProcessorServiceImplementation {
114
112
  async processBindings(request: ProcessBindingsRequest, options?: CallContext): Promise<ProcessBindingResponse> {
115
113
  const promises = []
116
114
  for (const binding of request.bindings) {
117
- const promise = this.processBinding(binding, undefined)
115
+ const promise = this.processBinding(binding)
118
116
  if (GLOBAL_CONFIG.execution.sequential) {
119
117
  await promise
120
118
  }
@@ -141,11 +139,7 @@ export class ServiceManager implements ProcessorServiceImplementation {
141
139
  }
142
140
  }
143
141
 
144
- async processBinding(
145
- request: DataBinding,
146
- preparedData: PreparedData | undefined,
147
- options?: CallContext
148
- ): Promise<ProcessResult> {
142
+ async processBinding(request: DataBinding): Promise<ProcessResult> {
149
143
  if (!this.started) {
150
144
  throw new ServerError(Status.UNAVAILABLE, 'Service Not started.')
151
145
  }
@@ -162,7 +156,7 @@ export class ServiceManager implements ProcessorServiceImplementation {
162
156
  )
163
157
  }
164
158
 
165
- return await this.pool.run({ request, preparedData }, { name: 'processBinding' })
159
+ return this.pool.run({ request })
166
160
  }
167
161
 
168
162
  async *processBindingsStream(requests: AsyncIterable<ProcessStreamRequest>, context: CallContext) {
@@ -207,7 +201,7 @@ export class ServiceManager implements ProcessorServiceImplementation {
207
201
  // todo support db request
208
202
  // const dbContext = contexts.new(request.processId, subject)
209
203
  const start = Date.now()
210
- await this.processBinding(binding, undefined)
204
+ await this.processBinding(binding)
211
205
  .then(async (result) => {
212
206
  // await all pending db requests
213
207
  // await dbContext.awaitPendings()
@@ -17,9 +17,9 @@ import { freezeGlobalConfig } from './global-config.js'
17
17
  import { DebugInfo, RichServerError } from 'nice-grpc-error-details'
18
18
  import { recordRuntimeInfo } from './service.js'
19
19
  import { BroadcastChannel } from 'worker_threads'
20
+ import { Piscina } from 'piscina'
20
21
 
21
22
  let started = false
22
- const options = commandLineArgs(optionDefinitions, { partial: true })
23
23
 
24
24
  let unhandled: Error | undefined
25
25
 
@@ -48,10 +48,12 @@ async function getConfig(request: ProcessConfigRequest, context?: CallContext):
48
48
  return newConfig
49
49
  }
50
50
 
51
- const loader = async () => {
52
- const m = await import(options.target)
53
- console.debug('Module loaded', m)
54
- return m
51
+ const loader = async (options: any) => {
52
+ if (options.target) {
53
+ const m = await import(options.target)
54
+ console.debug('Module loaded, path:', options.target, "module:", m)
55
+ return m
56
+ }
55
57
  }
56
58
 
57
59
  const configureChannel = new BroadcastChannel('configure_channel')
@@ -59,14 +61,14 @@ configureChannel.onmessage = (request: ProcessConfigRequest) => {
59
61
  getConfig(request)
60
62
  }
61
63
 
62
- async function start(request: StartRequest): Promise<Empty> {
64
+ async function start(request: StartRequest, options: any): Promise<Empty> {
63
65
  if (started) {
64
66
  return {}
65
67
  }
66
68
  freezeGlobalConfig()
67
69
 
68
70
  try {
69
- await loader()
71
+ await loader(options)
70
72
  } catch (e) {
71
73
  throw new ServerError(Status.INVALID_ARGUMENT, 'Failed to load processor: ' + errorString(e))
72
74
  }
@@ -76,23 +78,18 @@ async function start(request: StartRequest): Promise<Empty> {
76
78
  return {}
77
79
  }
78
80
 
79
- const startChannel = new BroadcastChannel('start_channel')
80
- startChannel.onmessage = (request: StartRequest) => {
81
- start(request)
82
- }
83
-
84
- async function stop(request: Empty, context: CallContext): Promise<Empty> {
85
- return {}
86
- }
87
-
88
- async function processBinding(
89
- request: DataBinding,
90
- preparedData: PreparedData | undefined,
91
- options?: CallContext
92
- ): Promise<ProcessResult> {
81
+ export default async function ({ request }: { request: DataBinding }) {
93
82
  if (!started) {
94
- throw new ServerError(Status.UNAVAILABLE, 'Service Not started.')
83
+ const { startRequest, configRequest, options } = Piscina.workerData
84
+ if (startRequest) {
85
+ await start(startRequest, options)
86
+ }
87
+ if (configRequest) {
88
+ await getConfig(configRequest)
89
+ }
90
+ console.info('new processor worker started')
95
91
  }
92
+
96
93
  if (unhandled) {
97
94
  throw new RichServerError(
98
95
  Status.UNAVAILABLE,
@@ -106,11 +103,16 @@ async function processBinding(
106
103
  )
107
104
  }
108
105
 
109
- const result = await PluginManager.INSTANCE.processBinding(
110
- request,
111
- preparedData
112
- // PluginManager.INSTANCE.dbContextLocalStorage.getStore()
113
- )
114
- recordRuntimeInfo(result, request.handlerType)
115
- return result
106
+ try {
107
+ const result = await PluginManager.INSTANCE.processBinding(
108
+ request,
109
+ undefined
110
+ // PluginManager.INSTANCE.dbContextLocalStorage.getStore()
111
+ )
112
+ recordRuntimeInfo(result, request.handlerType)
113
+ return result
114
+ } catch (e) {
115
+ console.error("worker process binding failed", e)
116
+ throw e
117
+ }
116
118
  }
@@ -6,7 +6,7 @@ export default defineConfig({
6
6
  js: `import { createRequire as createRequireShim } from 'module'; const require = createRequireShim(import.meta.url);`
7
7
  }
8
8
  },
9
- entry: ['src/index.ts', 'src/processor-runner.ts', 'src/service-worker.ts'],
9
+ entry: ['src/index.ts', 'src/processor-runner.ts', 'src/service-worker.ts', 'src/test-processor.test.ts'],
10
10
  outDir: 'lib',
11
11
  minify: process.env['BRANCH'] === 'release',
12
12
  sourcemap: true,