@sentio/runtime 2.57.12-rc.f → 2.57.12-rc.g
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.
- package/lib/processor-runner.js +15 -15
- package/lib/processor-runner.js.map +1 -1
- package/lib/service-worker.js +4 -2
- package/lib/service-worker.js.map +1 -1
- package/lib/test-processor.test.js.map +1 -1
- package/package.json +1 -1
- package/src/service-manager.ts +19 -17
- package/src/service-worker.ts +5 -2
package/lib/service-worker.js
CHANGED
@@ -64,15 +64,17 @@ async function start(request, options) {
|
|
64
64
|
}
|
65
65
|
async function service_worker_default({ request }) {
|
66
66
|
try {
|
67
|
+
const { startRequest, configRequest, options } = Piscina.workerData;
|
67
68
|
if (!started) {
|
68
|
-
const { startRequest, configRequest, options } = Piscina.workerData;
|
69
69
|
if (startRequest) {
|
70
70
|
await start(startRequest, options);
|
71
|
+
console.info("worker started");
|
71
72
|
}
|
73
|
+
console.info("new processor worker started");
|
72
74
|
if (configRequest) {
|
73
75
|
await getConfig(configRequest);
|
76
|
+
console.info("worker configured");
|
74
77
|
}
|
75
|
-
console.info("new processor worker started");
|
76
78
|
}
|
77
79
|
if (unhandled) {
|
78
80
|
throw new import_nice_grpc_error_details.RichServerError(
|
@@ -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'\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 try {\n
|
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 try {\n const { startRequest, configRequest, options } = Piscina.workerData\n if (!started) {\n if (startRequest) {\n await start(startRequest, options)\n console.info('worker started')\n }\n console.info('new processor worker started')\n\n if (configRequest) {\n await getConfig(configRequest)\n console.info('worker configured')\n }\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 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;AACF,UAAM,EAAE,cAAc,eAAe,QAAQ,IAAI,QAAQ;AACzD,QAAI,CAAC,SAAS;AACZ,UAAI,cAAc;AAChB,cAAM,MAAM,cAAc,OAAO;AACjC,gBAAQ,KAAK,gBAAgB;AAAA,MAC/B;AACA,cAAQ,KAAK,8BAA8B;AAE3C,UAAI,eAAe;AACjB,cAAM,UAAU,aAAa;AAC7B,gBAAQ,KAAK,mBAAmB;AAAA,MAClC;AAAA,IACF;AAEA,QAAI,WAAW;AACb,YAAM,IAAI;AAAA,QACR,wBAAO;AAAA,QACP,wDAAwD,YAAY,SAAS;AAAA,QAC7E;AAAA,UACE,yCAAU,YAAY;AAAA,YACpB,QAAQ,UAAU;AAAA,YAClB,cAAc,UAAU,OAAO,MAAM,IAAI;AAAA,UAC3C,CAAC;AAAA,QACH;AAAA,MACF;AAAA,IACF;AAEA,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":[]}
|
@@ -1 +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
|
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]\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,gBAAoB;AAC1C;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
package/src/service-manager.ts
CHANGED
@@ -44,11 +44,15 @@ export class ServiceManager implements ProcessorServiceImplementation {
|
|
44
44
|
// private processorConfig: ProcessConfigResponse
|
45
45
|
private pool: Piscina<any, any>
|
46
46
|
|
47
|
+
private workerData: any = {}
|
48
|
+
|
47
49
|
constructor(
|
48
50
|
readonly options: any,
|
49
51
|
readonly loader: () => Promise<any>,
|
50
52
|
readonly shutdownHandler?: () => void
|
51
|
-
) {
|
53
|
+
) {
|
54
|
+
this.workerData.options = options
|
55
|
+
}
|
52
56
|
|
53
57
|
async *preprocessBindingsStream(requests: AsyncIterable<PreprocessStreamRequest>, context: CallContext) {
|
54
58
|
throw new Error('not supported')
|
@@ -66,10 +70,8 @@ export class ServiceManager implements ProcessorServiceImplementation {
|
|
66
70
|
// also configure the main thread
|
67
71
|
const newConfig = ProcessConfigResponse.fromPartial({})
|
68
72
|
await PluginManager.INSTANCE.configure(newConfig)
|
69
|
-
|
70
|
-
|
71
|
-
...this.pool.options.workerData
|
72
|
-
}
|
73
|
+
|
74
|
+
this.workerData.configRequest = request
|
73
75
|
return newConfig
|
74
76
|
}
|
75
77
|
|
@@ -84,26 +86,17 @@ export class ServiceManager implements ProcessorServiceImplementation {
|
|
84
86
|
throw new ServerError(Status.INVALID_ARGUMENT, 'Failed to load processor: ' + errorString(e))
|
85
87
|
}
|
86
88
|
|
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
|
-
})
|
97
|
-
|
98
89
|
// also start the processor in main thread
|
99
90
|
await PluginManager.INSTANCE.start(request)
|
100
91
|
this.started = true
|
92
|
+
this.workerData.startRequest = request
|
101
93
|
return {}
|
102
94
|
}
|
103
95
|
|
104
96
|
async stop(request: Empty, context: CallContext): Promise<Empty> {
|
105
|
-
|
97
|
+
await this.pool.destroy()
|
106
98
|
if (this.shutdownHandler) {
|
99
|
+
console.log('Server Shutting down in 5 seconds')
|
107
100
|
setTimeout(this.shutdownHandler, 5000)
|
108
101
|
}
|
109
102
|
return {}
|
@@ -143,6 +136,15 @@ export class ServiceManager implements ProcessorServiceImplementation {
|
|
143
136
|
if (!this.started) {
|
144
137
|
throw new ServerError(Status.UNAVAILABLE, 'Service Not started.')
|
145
138
|
}
|
139
|
+
if (!this.pool) {
|
140
|
+
this.pool = new Piscina({
|
141
|
+
maxThreads: this.options.worker,
|
142
|
+
minThreads: this.options.worker,
|
143
|
+
filename: new URL('./service-worker.js', import.meta.url).href.replaceAll('runtime/src', 'runtime/lib'),
|
144
|
+
argv: process.argv,
|
145
|
+
workerData: this.workerData
|
146
|
+
})
|
147
|
+
}
|
146
148
|
if (this.unhandled) {
|
147
149
|
throw new RichServerError(
|
148
150
|
Status.UNAVAILABLE,
|
package/src/service-worker.ts
CHANGED
@@ -80,15 +80,18 @@ async function start(request: StartRequest, options: any): Promise<Empty> {
|
|
80
80
|
|
81
81
|
export default async function ({ request }: { request: DataBinding }) {
|
82
82
|
try {
|
83
|
+
const { startRequest, configRequest, options } = Piscina.workerData
|
83
84
|
if (!started) {
|
84
|
-
const { startRequest, configRequest, options } = Piscina.workerData
|
85
85
|
if (startRequest) {
|
86
86
|
await start(startRequest, options)
|
87
|
+
console.info('worker started')
|
87
88
|
}
|
89
|
+
console.info('new processor worker started')
|
90
|
+
|
88
91
|
if (configRequest) {
|
89
92
|
await getConfig(configRequest)
|
93
|
+
console.info('worker configured')
|
90
94
|
}
|
91
|
-
console.info('new processor worker started')
|
92
95
|
}
|
93
96
|
|
94
97
|
if (unhandled) {
|