-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathworker-thread-initializer.ts
More file actions
97 lines (82 loc) · 3.49 KB
/
worker-thread-initializer.ts
File metadata and controls
97 lines (82 loc) · 3.49 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { COM, UniversalWorkerHost, type InitializerOptions } from '@wixc3/engine-core';
import type { UniversalWorkerOptions } from '@wixc3/isomorphic-worker/types';
import { Worker } from '@wixc3/isomorphic-worker/worker';
import { createDisposables } from '@wixc3/patterns';
import { createMetadataProvider } from './core-node/create-application-metadata-provider.js';
import type { WorkerThreadCommand, WorkerThreadEnvironmentStartupOptions } from './types.js';
export interface WorkerThreadInitializer {
id: string;
dispose: () => Promise<void>;
initialize: () => Promise<void>;
}
export type WorkerThreadInitializerOptions = InitializerOptions & {
environmentStartupOptions?: {
environmentContextName: string | undefined;
};
};
export function workerThreadInitializer({
communication,
env,
environmentStartupOptions,
}: WorkerThreadInitializerOptions): WorkerThreadInitializer {
const workerThreadEntryPath = require.resolve('./worker-thread-entry');
const disposables = createDisposables('workerThreadInitializer');
const instanceId = communication.getEnvironmentInstanceId(env.env, env.endpointType);
const envIsReady = communication.envReady(instanceId);
const metadataProvider = createMetadataProvider(communication);
disposables.add({
name: 'worker thread metadataProvider',
timeout: 5_000,
dispose: metadataProvider,
});
const initialize = async (): Promise<void> => {
const { requiredModules, basePath, config, featureName, features, runtimeOptions } =
await metadataProvider.getMetadata();
const worker = new Worker(workerThreadEntryPath, {
workerData: {
name: instanceId,
},
execArgv: [...process.execArgv],
} as UniversalWorkerOptions);
disposables.add({
name: `worker thread ${instanceId} terminate`,
timeout: 5_000,
dispose: () => worker.terminate(),
});
const host = new UniversalWorkerHost(worker, instanceId);
communication.registerEnv(instanceId, host);
communication.registerMessageHandler(host);
disposables.add(`worker thread ${instanceId} communication cleanup`, () => {
communication.clearEnvironment(instanceId);
communication.removeMessageHandler(host);
});
const runOptions: WorkerThreadEnvironmentStartupOptions = {
environmentContextName: environmentStartupOptions?.environmentContextName,
runtimeOptions,
requiredModules,
basePath,
environmentName: instanceId,
/**
* configuration contains data that can not be serialized to worker communication channel.
* in the configuration of COM feature there is LOCAL_ENVIRONMENT_INITIALIZER_ENV_ID
* environment info that is needed when node env wants to start new node env.
* this is not a case for workerthread, so ignoring that config here
*/
config: config.filter(([featureId]) => featureId !== COM.id),
featureName,
features,
parentEnvName: communication.getEnvironmentName(),
env,
};
worker.postMessage({
id: 'workerThreadStartupCommand',
runOptions,
} as WorkerThreadCommand);
await envIsReady;
};
return {
id: instanceId,
initialize,
dispose: () => disposables.dispose(),
};
}