-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy patha.node.ts
More file actions
71 lines (66 loc) · 2.24 KB
/
a.node.ts
File metadata and controls
71 lines (66 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { bindMetricsListener, bindRpcListener, ParentPortHost } from '@dazl/engine-runtime-node';
import { workerData } from 'node:worker_threads';
import { COM, FeatureClass, RuntimeEngine, TopLevelConfig } from '@dazl/engine-core';
import TestFeature from '../feature/test-feature.js';
import { aEnv } from '../feature/envs.js';
import '../feature/test-feature.a.env.js';
const options = workerData?.runtimeOptions as Map<string, string> | undefined;
const verbose = options?.get('verbose') ?? false;
const env = aEnv;
if (verbose) {
console.log(`[${env.env}: Started with options: `, options);
}
let activateValue: unknown;
export function getActivateValue() {
return activateValue;
}
export function runEnv({
Feature = TestFeature,
topLevelConfig = [],
}: { Feature?: FeatureClass; topLevelConfig?: TopLevelConfig } = {}) {
return new RuntimeEngine(
env,
[
...(workerData
? [
COM.configure({
config: {
host: new ParentPortHost(env.env),
id: env.env,
},
}),
]
: []),
...topLevelConfig,
],
new Map(options?.entries() ?? []),
).run(Feature);
}
if (workerData) {
const unbindMetricsListener = bindMetricsListener();
let running: ReturnType<typeof runEnv>;
const unbindActivateListener = bindRpcListener('activate', (value: unknown) => {
activateValue = value;
unbindActivateListener();
running = runEnv();
});
const unbindTerminationListener = bindRpcListener('terminate', async () => {
if (verbose) {
console.log(`[${env.env}]: Termination Requested. Waiting for engine.`);
}
unbindTerminationListener();
unbindMetricsListener();
try {
const engine = await running;
if (verbose) {
console.log(`[${env.env}]: Terminating`);
}
return engine.shutdown();
} catch (e) {
console.error('[${env.name}]: Error while shutting down', e);
return;
}
});
} else {
console.log('running engine in test mode');
}