@remix_labs/machine-starter 2.12944.0-dev → 2.12946.0-dev
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/common.js +161 -112
- package/groovebox_build.js +1 -1
- package/package.json +2 -2
package/common.js
CHANGED
|
@@ -12,6 +12,7 @@ let children = {};
|
|
|
12
12
|
let ffiLoops = {};
|
|
13
13
|
let blobs = {};
|
|
14
14
|
let blobsLoops = {};
|
|
15
|
+
let coordination = {};
|
|
15
16
|
|
|
16
17
|
function terminateAll() {
|
|
17
18
|
console.log("[machine-starter] terminate all VMs");
|
|
@@ -37,47 +38,69 @@ function terminateAll() {
|
|
|
37
38
|
blobsLoops = {};
|
|
38
39
|
}
|
|
39
40
|
|
|
40
|
-
function terminateVM(vmID) {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
41
|
+
async function terminateVM(vmID) {
|
|
42
|
+
let coord = coordination[vmID];
|
|
43
|
+
if (coord) {
|
|
44
|
+
console.log("[machine-starter] terminate VM:", vmID);
|
|
45
|
+
const do_it = (async () => {
|
|
46
|
+
// make the VM first unknown to the outside and disconnect it,
|
|
47
|
+
// then clean up resources (including stopping the VM)
|
|
48
|
+
let channel = await last_hub.newChannel();
|
|
49
|
+
let topic = worker_topics[vmID];
|
|
50
|
+
let payload = { _rmx_type:"msg_vm_register", id: vmID, alive: false };
|
|
51
|
+
let msg = channel.newJSONMessage("msg_vm_register", "vm", payload);
|
|
52
|
+
await channel.publish(topic + "/announce", msg, true);
|
|
53
|
+
// NB. This msg is a leak - it will never be removed.
|
|
54
|
+
await channel.disconnect("vm_" + vmID);
|
|
55
|
+
// now the VM is isolated and cannot communicate with the
|
|
56
|
+
// hub anymore
|
|
57
|
+
let worker = workers[vmID];
|
|
58
|
+
let vmChildren = children[vmID] || [];
|
|
59
|
+
let blobList = blobs[vmID] || [];
|
|
60
|
+
let ffiLoop = ffiLoops[vmID];
|
|
61
|
+
let blobsLoop = blobsLoops[vmID];
|
|
62
|
+
delete children[vmID];
|
|
63
|
+
delete ffiLoops[vmID];
|
|
64
|
+
delete blobsLoops[vmID];
|
|
65
|
+
delete worker_topics[vmID];
|
|
66
|
+
delete workers[vmID];
|
|
67
|
+
// terminate our endpoints of communication
|
|
68
|
+
if (ffiLoop) {
|
|
69
|
+
ffiLoop.terminate();
|
|
70
|
+
}
|
|
71
|
+
if (blobsLoop) {
|
|
72
|
+
blobsLoop.terminate();
|
|
73
|
+
}
|
|
74
|
+
for (let blob_url of blobList) {
|
|
75
|
+
console.log("[machine-starter] revoke ", blob_url);
|
|
76
|
+
URL.revokeObjectURL(blob_url);
|
|
77
|
+
}
|
|
78
|
+
// kill the worker:
|
|
79
|
+
console.log("[machine-starter] terminate web worker for VM:", vmID);
|
|
80
|
+
worker.terminate();
|
|
81
|
+
// go on with sub VMs
|
|
61
82
|
for (let subID of vmChildren) {
|
|
62
|
-
terminateVM(subID)
|
|
83
|
+
terminateVM(subID) // do not wait
|
|
63
84
|
}
|
|
85
|
+
});
|
|
86
|
+
// if termination is already in progress, just wait for completion
|
|
87
|
+
while (coord.term_promise) {
|
|
88
|
+
console.log("[machine-starter] termination already in progress, waiting until completed:", vmID);
|
|
89
|
+
await coord.term_promise;
|
|
90
|
+
return; // that's it!
|
|
64
91
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
channel.publish(topic + "/announce", msg, true)
|
|
78
|
-
}).then(_ => worker.terminate())
|
|
79
|
-
} else {
|
|
80
|
-
worker.terminate()
|
|
92
|
+
let term_done = (() => null);
|
|
93
|
+
coord.term_promise = new Promise((succeed, reject) => term_done = succeed);
|
|
94
|
+
// if start-up is still in progress, wait for completion
|
|
95
|
+
if (coord.start_promise) {
|
|
96
|
+
console.log("[machine-starter] termination during start-up, waiting until completed:", vmID);
|
|
97
|
+
await coord.start_promise;
|
|
98
|
+
}
|
|
99
|
+
// now: do it!
|
|
100
|
+
await do_it();
|
|
101
|
+
coord.term_promise = null;
|
|
102
|
+
if (!coord.will_start) delete coordination[vmID];
|
|
103
|
+
term_done();
|
|
81
104
|
}
|
|
82
105
|
}
|
|
83
106
|
|
|
@@ -133,84 +156,110 @@ async function StartWASM2(hub, config, getWorker) {
|
|
|
133
156
|
console.log("[machine-starter] Call MixExplain() for explaining debug levels, and MixSetMask(m) for changing these")
|
|
134
157
|
};
|
|
135
158
|
last_hub = hub;
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
if
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
159
|
+
// get thr coord object - perhaps the ID was already used for a VM
|
|
160
|
+
let coord = coordination[config.vmID];
|
|
161
|
+
if (!coord) {
|
|
162
|
+
coord = { start_promise: null, term_promise: null, will_start: false };
|
|
163
|
+
coordination[config.vmID] = coord;
|
|
164
|
+
}
|
|
165
|
+
// if there is already a worker with the same ID, check whether it is
|
|
166
|
+
// already terminating. If so, wait for completion.
|
|
167
|
+
coord.will_start = true;
|
|
168
|
+
while (coord.term_promise) {
|
|
169
|
+
if (initialMask & DEBUG_STATE) {
|
|
170
|
+
console.debug("[machine-starter] waiting for termination of VM: " + config.vmID);
|
|
171
|
+
};
|
|
172
|
+
await coord.term_promise;
|
|
146
173
|
}
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
let mixcore = await createStarterMixcore(config.mixcore);
|
|
151
|
-
if (mixcore) {
|
|
152
|
-
// merge the ffis and store them back into the config so sub vms also get them
|
|
153
|
-
config.localFFIs = Object.assign(starterFFIs, await mixcoreStarterFFIs(mixcore));
|
|
154
|
-
// delete the mixcore config to avoid being handled further by the worker
|
|
155
|
-
delete config.mixcore;
|
|
174
|
+
// if already starting up, this is an error.
|
|
175
|
+
if (coord.start_promise || workers[config.vmID]) {
|
|
176
|
+
throw new Error("[machine-starter] Cannot start VM, the ID is used: " + config.vmID);
|
|
156
177
|
}
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
178
|
+
// set to starting:
|
|
179
|
+
coord.will_start = false;
|
|
180
|
+
let start_done = (() => null);
|
|
181
|
+
coord.start_promise = new Promise((succeed,fail) => start_done = succeed);
|
|
182
|
+
try {
|
|
183
|
+
let channel = await hub.newChannel("vm_" + config.vmID);
|
|
184
|
+
// injected by either index.js or node.js
|
|
185
|
+
let worker = await getWorker();
|
|
186
|
+
workers[config.vmID] = worker;
|
|
187
|
+
if (config.topicPrefix != undefined && config.topicPrefix != "") {
|
|
188
|
+
worker_topics[config.vmID] = config.topicPrefix + "/vm/" + config.vmID;
|
|
163
189
|
} else {
|
|
164
|
-
|
|
165
|
-
delete starterFFIkind[k].run;
|
|
190
|
+
worker_topics[config.vmID] = "/org/" + config.org + "/ws/" + config.workspace + "/vm/" + config.vmID;
|
|
166
191
|
}
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
// startup times
|
|
177
|
-
if (globalThis.mixrtCodeModule && globalThis.mixrtCodeURL == mixrtCode) {
|
|
178
|
-
console.log("[machine-starter] reusing mixrt");
|
|
179
|
-
mixrtCode = globalThis.mixrtCodeModule;
|
|
180
|
-
} else {
|
|
181
|
-
let url = mixrtCode;
|
|
182
|
-
console.log("[machine-starter] pre-compiling mixrt");
|
|
183
|
-
mixrtCode = await WebAssembly.compileStreaming(fetch(url));
|
|
184
|
-
globalThis.mixrtCodeURL = url;
|
|
185
|
-
globalThis.mixrtCodeModule = mixrtCode;
|
|
192
|
+
let starterFFIs = config.localFFIs || {};
|
|
193
|
+
// from now on, the "local" FFIs are called "starter" FFIs, because this
|
|
194
|
+
// distinguishes these from other locally defined FFIs
|
|
195
|
+
let mixcore = await createStarterMixcore(config.mixcore);
|
|
196
|
+
if (mixcore) {
|
|
197
|
+
// merge the ffis and store them back into the config so sub vms also get them
|
|
198
|
+
config.localFFIs = Object.assign(starterFFIs, await mixcoreStarterFFIs(mixcore));
|
|
199
|
+
// delete the mixcore config to avoid being handled further by the worker
|
|
200
|
+
delete config.mixcore;
|
|
186
201
|
}
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
"user": config.user,
|
|
198
|
-
"token": config.token,
|
|
199
|
-
"hub": channel.port,
|
|
200
|
-
"outputViaMQTT": !(config.noOutputViaMQTT),
|
|
201
|
-
"machType": config.machType,
|
|
202
|
-
"debugMask": initialMask,
|
|
203
|
-
"starterFFIs": starterFFIkind,
|
|
204
|
-
"allowInsecureHttp": globalThis.GROOVEBOX_ALLOW_INSECURE_HTTP,
|
|
205
|
-
"interceptFFI": config.interceptFFI,
|
|
206
|
-
"interceptDynloadFFI": config.interceptDynloadFFI,
|
|
207
|
-
"mixcore": config.mixcore,
|
|
208
|
-
"mixrtCode": mixrtCode
|
|
209
|
-
// can be: base64-encoded string, Uint8Array, or a URL as string
|
|
210
|
-
// (http, https, data)
|
|
202
|
+
let starterFFIkind = {};
|
|
203
|
+
let starterFFIfuns = {};
|
|
204
|
+
for (const [k, v] of Object.entries(starterFFIs)) {
|
|
205
|
+
starterFFIfuns[k] = {...v};
|
|
206
|
+
if (v.isRaw && (v.canFail || v.useJsonDecoder)) {
|
|
207
|
+
console.error("A raw, starter FFI must neither be failing nor use the JSON decoder: " + k);
|
|
208
|
+
} else {
|
|
209
|
+
starterFFIkind[k] = {...v};
|
|
210
|
+
delete starterFFIkind[k].run;
|
|
211
|
+
}
|
|
211
212
|
};
|
|
212
|
-
|
|
213
|
-
|
|
213
|
+
let vm_kind = { isRaw:false, canFail:false, useJsonDecoder:false};
|
|
214
|
+
starterFFIkind["$vm_start"] = vm_kind;
|
|
215
|
+
starterFFIfuns["$vm_start"] = (conn, args) => ffi_vm_start(conn, args, getWorker);
|
|
216
|
+
starterFFIkind["$vm_stop"] = vm_kind;
|
|
217
|
+
starterFFIfuns["$vm_stop"] = ffi_vm_stop;
|
|
218
|
+
let mixrtCode = config.mixrtCode;
|
|
219
|
+
if (typeof(mixrtCode) == "string" && mixrtCode.startsWith("remix://")) {
|
|
220
|
+
// for the desktop only: precompile the mixrt module to get faster
|
|
221
|
+
// startup times
|
|
222
|
+
if (globalThis.mixrtCodeModule && globalThis.mixrtCodeURL == mixrtCode) {
|
|
223
|
+
console.log("[machine-starter] reusing mixrt");
|
|
224
|
+
mixrtCode = globalThis.mixrtCodeModule;
|
|
225
|
+
} else {
|
|
226
|
+
let url = mixrtCode;
|
|
227
|
+
console.log("[machine-starter] pre-compiling mixrt");
|
|
228
|
+
mixrtCode = await WebAssembly.compileStreaming(fetch(url));
|
|
229
|
+
globalThis.mixrtCodeURL = url;
|
|
230
|
+
globalThis.mixrtCodeModule = mixrtCode;
|
|
231
|
+
}
|
|
232
|
+
};
|
|
233
|
+
ffiLoops[config.vmID] = setupStarterFFIs(hub, config, starterFFIfuns);
|
|
234
|
+
blobsLoops[config.vmID] = setupBlobs(hub, config);
|
|
235
|
+
let config_msg =
|
|
236
|
+
{ "_rmx_type": "msg_vm_configure",
|
|
237
|
+
"baseURL": config.baseURL,
|
|
238
|
+
"org": config.org,
|
|
239
|
+
"workspace": config.workspace,
|
|
240
|
+
"topicPrefix": config.topicPrefix,
|
|
241
|
+
"vmID": config.vmID,
|
|
242
|
+
"user": config.user,
|
|
243
|
+
"token": config.token,
|
|
244
|
+
"hub": channel.port,
|
|
245
|
+
"outputViaMQTT": !(config.noOutputViaMQTT),
|
|
246
|
+
"machType": config.machType,
|
|
247
|
+
"debugMask": initialMask,
|
|
248
|
+
"starterFFIs": starterFFIkind,
|
|
249
|
+
"allowInsecureHttp": globalThis.GROOVEBOX_ALLOW_INSECURE_HTTP,
|
|
250
|
+
"interceptFFI": config.interceptFFI,
|
|
251
|
+
"interceptDynloadFFI": config.interceptDynloadFFI,
|
|
252
|
+
"mixcore": config.mixcore,
|
|
253
|
+
"mixrtCode": mixrtCode
|
|
254
|
+
// can be: base64-encoded string, Uint8Array, or a URL as string
|
|
255
|
+
// (http, https, data)
|
|
256
|
+
};
|
|
257
|
+
worker.postMessage(config_msg, [ channel.port ]);
|
|
258
|
+
return worker;
|
|
259
|
+
} finally {
|
|
260
|
+
coord.start_promise = null;
|
|
261
|
+
start_done();
|
|
262
|
+
}
|
|
214
263
|
}
|
|
215
264
|
|
|
216
265
|
async function mixcoreStarterFFIs(mixcore) {
|
|
@@ -387,7 +436,7 @@ async function starterFFIsMessageLoop(hub, config, starterFFIs, terminate) {
|
|
|
387
436
|
sub.next().then((msg) => ({msg})),
|
|
388
437
|
terminate.then(() => ({terminated: true})),
|
|
389
438
|
]);
|
|
390
|
-
if (terminated) {
|
|
439
|
+
if (terminated || msg.eof) {
|
|
391
440
|
// console.log("[machine-starter] terminate FFI loop", vmID);
|
|
392
441
|
return;
|
|
393
442
|
}
|
|
@@ -447,7 +496,7 @@ async function blobsMessageLoop(hub, config, terminate) {
|
|
|
447
496
|
sub.next().then((msg) => ({msg})),
|
|
448
497
|
terminate.then(() => ({terminated: true})),
|
|
449
498
|
]);
|
|
450
|
-
if (terminated) {
|
|
499
|
+
if (terminated || msg.eof) {
|
|
451
500
|
// console.log("[machine-starter] terminate blobs loop", vmID);
|
|
452
501
|
return;
|
|
453
502
|
}
|
package/groovebox_build.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var GROOVEBOX_BUILD = "
|
|
1
|
+
var GROOVEBOX_BUILD = "12946";
|
|
2
2
|
export { GROOVEBOX_BUILD }
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@remix_labs/machine-starter",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.12946.0-dev",
|
|
4
4
|
"description": "start the groove",
|
|
5
5
|
"main": "node.js",
|
|
6
6
|
"browser": "index.js",
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"author": "Remixlabs staff",
|
|
12
12
|
"license": "ISC",
|
|
13
13
|
"dependencies": {
|
|
14
|
-
"@remix_labs/hub-client": "2.
|
|
14
|
+
"@remix_labs/hub-client": "2.12946.0-dev",
|
|
15
15
|
"nanoid": "^5.0.2",
|
|
16
16
|
"web-worker": "^1.2.0"
|
|
17
17
|
},
|