@naanlang/naan 1.0.0
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/LICENSE.md +21 -0
- package/README.md +46 -0
- package/dist/env_web.js +431 -0
- package/dist/naan.min.js +73 -0
- package/frameworks/browser/browser.nlg +30 -0
- package/frameworks/browser/pouchdb/pouchdb.min.js +7 -0
- package/frameworks/browser/spark-md5/spark-md5.min.js +1 -0
- package/frameworks/browser/sworker.js +192 -0
- package/frameworks/browser/terminals.nlg +787 -0
- package/frameworks/browser/workers.nlg +386 -0
- package/frameworks/client/apiclient.nlg +183 -0
- package/frameworks/client/client.nlg +29 -0
- package/frameworks/client/psm_client.nlg +572 -0
- package/frameworks/common/common.nlg +464 -0
- package/frameworks/common/preferences.nlg +131 -0
- package/frameworks/common/repltools.nlg +100 -0
- package/frameworks/common/watching.nlg +97 -0
- package/frameworks/node/apiserver.nlg +399 -0
- package/frameworks/node/filesystem.nlg +1012 -0
- package/frameworks/node/gitter.nlg +234 -0
- package/frameworks/node/node.nlg +81 -0
- package/frameworks/node/psm_server.nlg +465 -0
- package/frameworks/node/worker.nlg +560 -0
- package/frameworks/project/build.nlg +1229 -0
- package/frameworks/project/jszip.js +30 -0
- package/frameworks/project/jszip.nlg +87 -0
- package/frameworks/project/proj_cliser.nlg +139 -0
- package/frameworks/project/proj_console.nlg +137 -0
- package/frameworks/project/proj_folder.nlg +143 -0
- package/frameworks/project/proj_lambda.nlg +137 -0
- package/frameworks/project/proj_static.nlg +138 -0
- package/frameworks/project/project.nlg +29 -0
- package/frameworks/project/projects.nlg +587 -0
- package/frameworks/project/uglifyjs.js +1 -0
- package/frameworks/project/uglifyjs.nlg +79 -0
- package/frameworks/running/debugcom.nlg +163 -0
- package/frameworks/running/debugnub.nlg +565 -0
- package/frameworks/running/debugutil.nlg +510 -0
- package/frameworks/running/executors.nlg +767 -0
- package/frameworks/running/running.nlg +151 -0
- package/frameworks/running/sourcecode.nlg +346 -0
- package/frameworks/running/taskexec.nlg +196 -0
- package/frameworks/storage/dbt_pouch.nlg +773 -0
- package/frameworks/storage/file_manager.nlg +772 -0
- package/frameworks/storage/psm.nlg +326 -0
- package/frameworks/storage/psm_dbtables.nlg +814 -0
- package/frameworks/storage/resources.nlg +445 -0
- package/frameworks/storage/storage.nlg +52 -0
- package/lib/browser/env_web.js +431 -0
- package/lib/browser/env_webworker.js +146 -0
- package/lib/browser/require.js +1653 -0
- package/lib/core/naanlib.js +73 -0
- package/lib/env_node.js +282 -0
- package/lib/env_nodeworker.js +165 -0
- package/lib/node_repl.js +25 -0
- package/lib/node_repl_init.nlg +52 -0
- package/package.json +13 -0
- package/plugins/gitClient/clientops.nlg +317 -0
- package/plugins/gitClient/gitClient.nlg +63 -0
- package/plugins/gitClient/gitutils.nlg +1331 -0
- package/plugins/gitClient/minimatch.js +945 -0
- package/plugins/gitClient/minimatch_license.txt +15 -0
- package/plugins/gitClient/minimatch_readme.md +208 -0
- package/plugins/nodeLib/nodeLib.nlg +26 -0
- package/plugins/nodeLib/node_cp.nlg +65 -0
- package/plugins/nodeLib/node_fs.nlg +137 -0
- package/plugins/nodeLib/node_https.nlg +172 -0
- package/plugins/nodeLib/node_util.nlg +180 -0
- package/plugins/serviceAws/aws/aws-sdk.min.js +88 -0
- package/plugins/serviceAws/aws/lambda_rest_index.js +122 -0
- package/plugins/serviceAws/aws/lambda_rest_init.nlg +58 -0
- package/plugins/serviceAws/aws/lambda_ws_index.js +498 -0
- package/plugins/serviceAws/aws/lambda_ws_init.nlg +49 -0
- package/plugins/serviceAws/aws_cloudwatchlogs.nlg +122 -0
- package/plugins/serviceAws/aws_cognito.nlg +76 -0
- package/plugins/serviceAws/aws_dynamo.nlg +509 -0
- package/plugins/serviceAws/aws_lambda.nlg +95 -0
- package/plugins/serviceAws/aws_s3.nlg +134 -0
- package/plugins/serviceAws/aws_utils.nlg +86 -0
- package/plugins/serviceAws/dbt_aws.nlg +769 -0
- package/plugins/serviceAws/lambda_naan_control.nlg +175 -0
- package/plugins/serviceAws/psm_aws.nlg +258 -0
- package/plugins/serviceAws/serviceAws.nlg +38 -0
- package/plugins/serviceGitHub/gitops.nlg +522 -0
- package/plugins/serviceGitHub/psm_github.nlg +205 -0
- package/plugins/serviceGitHub/serviceGitHub.nlg +65 -0
- package/plugins/serviceGitLab/gitops.nlg +528 -0
- package/plugins/serviceGitLab/psm_gitlab.nlg +205 -0
- package/plugins/serviceGitLab/serviceGitLab.nlg +65 -0
- package/test/harness.nlg +312 -0
- package/test/node_test.js +33 -0
- package/test/test_01_core.nlg +768 -0
- package/test/test_02_context.nlg +269 -0
- package/test/test_03_jsinterop.nlg +137 -0
- package/test/test_04_numerics.nlg +450 -0
- package/test/test_05_strings.nlg +63 -0
- package/test/test_06_lingoparse.nlg +500 -0
- package/test/test_07_lingo.nlg +623 -0
|
@@ -0,0 +1,560 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* worker.nlg
|
|
3
|
+
* Naanlib/frameworks/node
|
|
4
|
+
*
|
|
5
|
+
* Nide worker controller.
|
|
6
|
+
*
|
|
7
|
+
* column positioning: // // !
|
|
8
|
+
*
|
|
9
|
+
* Copyright (c) 2019-2021 by Richard C. Zulch
|
|
10
|
+
*
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
/*
|
|
15
|
+
* workCurrent
|
|
16
|
+
*
|
|
17
|
+
* Attach a remote terminal interface to the current NodeJS thread.
|
|
18
|
+
*
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
closure workCurrent(workerID, local current) {
|
|
22
|
+
current = new(object, this)
|
|
23
|
+
current.workerID = workerID
|
|
24
|
+
current.links = []
|
|
25
|
+
current.termif = xnew({
|
|
26
|
+
textout: ONtextOut.proc
|
|
27
|
+
debugtext: ONdebugText.proc
|
|
28
|
+
status: ONstatus.proc
|
|
29
|
+
debugout: ONdebugOut.proc
|
|
30
|
+
messageout: ONmessageOut.proc
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
current.host = js.t // our magic Naan controller reference
|
|
34
|
+
|
|
35
|
+
// ONtextOut
|
|
36
|
+
// Process console text output by Naan.
|
|
37
|
+
|
|
38
|
+
function ONtextOut(text) {
|
|
39
|
+
sendAll({
|
|
40
|
+
id: "textout",
|
|
41
|
+
text: text,
|
|
42
|
+
})
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// ONdebugText
|
|
46
|
+
// Process console debug text output by Naan.
|
|
47
|
+
|
|
48
|
+
function ONdebugText(text, level) {
|
|
49
|
+
sendAll({
|
|
50
|
+
id: "debugtext",
|
|
51
|
+
text: text,
|
|
52
|
+
level: level
|
|
53
|
+
})
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// ONstatus
|
|
57
|
+
// Process status update output by Naan.
|
|
58
|
+
|
|
59
|
+
function ONstatus(status) {
|
|
60
|
+
sendAll({
|
|
61
|
+
id: "status",
|
|
62
|
+
status: status
|
|
63
|
+
})
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// ONdebugOut
|
|
67
|
+
// Process debugger output.
|
|
68
|
+
|
|
69
|
+
function ONdebugOut(data) {
|
|
70
|
+
sendAll({
|
|
71
|
+
id: "debugout",
|
|
72
|
+
data: data
|
|
73
|
+
})
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// ONmessageOut
|
|
77
|
+
// Process message output.
|
|
78
|
+
|
|
79
|
+
function ONmessageOut(data) {
|
|
80
|
+
sendAll({
|
|
81
|
+
id: "targetout",
|
|
82
|
+
data: data
|
|
83
|
+
})
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// removeLink
|
|
87
|
+
// Remove a link.
|
|
88
|
+
|
|
89
|
+
function removeLink(link) {
|
|
90
|
+
current.links = current.links.filter(function(item) {
|
|
91
|
+
!(item.link eq link)
|
|
92
|
+
})
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// exitNotify
|
|
96
|
+
// Notify all links that we have terminated.
|
|
97
|
+
|
|
98
|
+
function exitNotify(exitCode, local link) {
|
|
99
|
+
for link in current.links
|
|
100
|
+
link.exitWorker(exitCode)
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// sendAll
|
|
104
|
+
// Send a message to all links.
|
|
105
|
+
|
|
106
|
+
function sendAll(msg, local link) {
|
|
107
|
+
for link in current.links
|
|
108
|
+
link.responder(msg)
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
//
|
|
112
|
+
// attach
|
|
113
|
+
//
|
|
114
|
+
// Attach a link.
|
|
115
|
+
//
|
|
116
|
+
|
|
117
|
+
current.attach = function attach(link, responder) {
|
|
118
|
+
removeLink(link)
|
|
119
|
+
current.links.push({
|
|
120
|
+
link: link // a link to us
|
|
121
|
+
responder: responder // link's responder
|
|
122
|
+
})
|
|
123
|
+
if current.links.length == 1
|
|
124
|
+
current.host.Attach(current.termif) // attached while one link is active
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
//
|
|
128
|
+
// detach
|
|
129
|
+
//
|
|
130
|
+
// Detach a link.
|
|
131
|
+
//
|
|
132
|
+
|
|
133
|
+
current.detach = function detach(link) {
|
|
134
|
+
removeLink(link)
|
|
135
|
+
if current.links.length == 0
|
|
136
|
+
current.host.Detach(current.termif) // attached while one link is active
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
//
|
|
140
|
+
// msgin
|
|
141
|
+
//
|
|
142
|
+
// Send a message to the host interface.
|
|
143
|
+
|
|
144
|
+
current.msgin = function msgin(msg) {
|
|
145
|
+
current.host.DispatchMessage(msg)
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
//
|
|
149
|
+
// echo
|
|
150
|
+
//
|
|
151
|
+
// Echo a message to the *other* links.
|
|
152
|
+
|
|
153
|
+
current.echo = function echo(msg, thislink, local linkref) {
|
|
154
|
+
for linkref in current.links
|
|
155
|
+
if !(linkref.link eq thislink)
|
|
156
|
+
linkref.responder(msg)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
//
|
|
160
|
+
// vitalUpdate
|
|
161
|
+
//
|
|
162
|
+
// Worker thread birth/death/enumeration.
|
|
163
|
+
|
|
164
|
+
current.vitalUpdate = function vitalUpdate(msg) {
|
|
165
|
+
sendAll(msg)
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// finis
|
|
169
|
+
current
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
/*
|
|
174
|
+
* workThread
|
|
175
|
+
*
|
|
176
|
+
* Spawn a Naan worker in its own thread.
|
|
177
|
+
*
|
|
178
|
+
*/
|
|
179
|
+
|
|
180
|
+
closure workThread(workerID, startup, local worker, subChannel, deathWatcher) {
|
|
181
|
+
worker = new(object, this)
|
|
182
|
+
worker.workerID = workerID
|
|
183
|
+
worker.links = []
|
|
184
|
+
|
|
185
|
+
// create our worker thread
|
|
186
|
+
|
|
187
|
+
worker.thread = xnew(threads.Worker, JSpath.resolve(js.d, "node_worker.js"))
|
|
188
|
+
subChannel = xnew(threads.MessageChannel)
|
|
189
|
+
worker.thread.postMessage({ hereIsYourPort: subChannel.port1 }, [subChannel.port1])
|
|
190
|
+
worker.msgport = subChannel.port2
|
|
191
|
+
|
|
192
|
+
// removeLink
|
|
193
|
+
// Remove a link.
|
|
194
|
+
|
|
195
|
+
function removeLink(link) {
|
|
196
|
+
worker.links = worker.links.filter(function(item) {
|
|
197
|
+
!(item.link eq link)
|
|
198
|
+
})
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// exitNotify
|
|
202
|
+
// Notify all links that we have terminated.
|
|
203
|
+
|
|
204
|
+
function exitNotify(exitCode, local link) {
|
|
205
|
+
for link in worker.links
|
|
206
|
+
link.exitWorker(exitCode)
|
|
207
|
+
worker.links = []
|
|
208
|
+
deathWatcher(workerID)
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// sendAll
|
|
212
|
+
// Send a message to all links.
|
|
213
|
+
|
|
214
|
+
function sendAll(msg, local link) {
|
|
215
|
+
for link in worker.links
|
|
216
|
+
link.responder(msg)
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
//
|
|
220
|
+
// onerror
|
|
221
|
+
//
|
|
222
|
+
// An exception occurred on the worker thread. Since Naan catches all exceptions this should
|
|
223
|
+
// not occur normally.
|
|
224
|
+
|
|
225
|
+
worker.thread.on("error", function onerror(error) {
|
|
226
|
+
debuglog("worker terminated with error", workerID, error)
|
|
227
|
+
exitNotify(exitCode)
|
|
228
|
+
})
|
|
229
|
+
|
|
230
|
+
//
|
|
231
|
+
// onexit
|
|
232
|
+
//
|
|
233
|
+
// Note that the worker thread has terminated.
|
|
234
|
+
|
|
235
|
+
worker.thread.on("exit", function onexit(exitCode) {
|
|
236
|
+
exitNotify(exitCode)
|
|
237
|
+
})
|
|
238
|
+
|
|
239
|
+
//
|
|
240
|
+
// naanmsg
|
|
241
|
+
//
|
|
242
|
+
// A message has been received from Naan running on the worker thread.
|
|
243
|
+
|
|
244
|
+
worker.msgport.on("message", function naanmsg(msg, local startmsg) {
|
|
245
|
+
if msg.id == "loaded" {
|
|
246
|
+
startmsg = {
|
|
247
|
+
id: "start"
|
|
248
|
+
state: false // resume state, if we have any
|
|
249
|
+
altcmd: false
|
|
250
|
+
dirpath: js.d
|
|
251
|
+
}
|
|
252
|
+
if startup.initcmds
|
|
253
|
+
startmsg.altcmd = startup.initcmds.concat("\n")
|
|
254
|
+
if startup.dirpath
|
|
255
|
+
startmsg.dirpath = startup.dirpath
|
|
256
|
+
worker.msgport.postMessage(startmsg)
|
|
257
|
+
worker.links[0].responder({ // only send this to first link
|
|
258
|
+
id: "started" // don't want multiple initializers
|
|
259
|
+
}) }
|
|
260
|
+
else {
|
|
261
|
+
if msg.id == "status"
|
|
262
|
+
worker.lastStatus = milliseconds()
|
|
263
|
+
sendAll(msg) } // default is just return message to the client
|
|
264
|
+
})
|
|
265
|
+
|
|
266
|
+
//
|
|
267
|
+
// terminate
|
|
268
|
+
//
|
|
269
|
+
// Terminate this worker asap.
|
|
270
|
+
|
|
271
|
+
worker.terminate = function terminate(local error, exitCode) {
|
|
272
|
+
`(error, exitCode) = await(worker.thread.terminate())
|
|
273
|
+
exitNotify(exitCode)
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
//
|
|
277
|
+
// attach
|
|
278
|
+
//
|
|
279
|
+
// Attach a connection to our worker.
|
|
280
|
+
//
|
|
281
|
+
|
|
282
|
+
worker.attach = function attach(link, responder) {
|
|
283
|
+
removeLink(link)
|
|
284
|
+
worker.links.push({
|
|
285
|
+
link: link // a link to us
|
|
286
|
+
responder: responder // link's responder
|
|
287
|
+
})
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
//
|
|
291
|
+
// detach
|
|
292
|
+
//
|
|
293
|
+
// Detach a link from our worker.
|
|
294
|
+
//
|
|
295
|
+
|
|
296
|
+
worker.detach = function detach(link) {
|
|
297
|
+
removeLink(link)
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
//
|
|
301
|
+
// msgin
|
|
302
|
+
//
|
|
303
|
+
// Send a message to the worker thread.
|
|
304
|
+
|
|
305
|
+
worker.msgin = function msgin(msg) {
|
|
306
|
+
worker.msgport.postMessage(msg)
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
//
|
|
310
|
+
// echo
|
|
311
|
+
//
|
|
312
|
+
// Echo a message to the *other* links.
|
|
313
|
+
|
|
314
|
+
worker.echo = function echo(msg, thislink, local linkref) {
|
|
315
|
+
for linkref in worker.links
|
|
316
|
+
if !(linkref.link eq thislink)
|
|
317
|
+
linkref.responder(msg)
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
//
|
|
321
|
+
// vitalWatch
|
|
322
|
+
//
|
|
323
|
+
// Report our termination to the specified watcher.
|
|
324
|
+
|
|
325
|
+
worker.vitalWatch = function vitalWatch(watcher) {
|
|
326
|
+
deathWatcher = watcher
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
// finis
|
|
330
|
+
worker
|
|
331
|
+
};
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
/*
|
|
335
|
+
* workLink
|
|
336
|
+
*
|
|
337
|
+
* An object representing a specific connection and worker pair. Both workers and connections may
|
|
338
|
+
* have multiple links. This allows a worker to communicate with multiple connections and allows
|
|
339
|
+
* clients to create a new connection to talk with an existing worker.
|
|
340
|
+
*
|
|
341
|
+
*/
|
|
342
|
+
|
|
343
|
+
closure workLink(workco, conn, worker, sender, local link) {
|
|
344
|
+
link = new(object, this)
|
|
345
|
+
|
|
346
|
+
//
|
|
347
|
+
// closeConn
|
|
348
|
+
//
|
|
349
|
+
// Note that the connection has closed.
|
|
350
|
+
//
|
|
351
|
+
|
|
352
|
+
link.closeConn = function closeConn() {
|
|
353
|
+
worker.detach(link) // stop receiving updates from worker
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
//
|
|
357
|
+
// exitWorker
|
|
358
|
+
//
|
|
359
|
+
// Note that the worker has exited.
|
|
360
|
+
//
|
|
361
|
+
|
|
362
|
+
link.exitWorker = function exitWorker(exitCode) {
|
|
363
|
+
sender("terminated", [false, {
|
|
364
|
+
workerID: worker.workerID // notify client they've lost a worker
|
|
365
|
+
exitCode: exitCode
|
|
366
|
+
}])
|
|
367
|
+
workco.closeLink(link, conn, worker.workerID)
|
|
368
|
+
worker.detach(link)
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
// initialize
|
|
372
|
+
|
|
373
|
+
worker.attach(link, function msgout(data) {
|
|
374
|
+
sender("msgout", data)
|
|
375
|
+
})
|
|
376
|
+
sender("linked", [false, {
|
|
377
|
+
workerID: worker.workerID // notify client they've gained a worker
|
|
378
|
+
}])
|
|
379
|
+
|
|
380
|
+
// finis
|
|
381
|
+
|
|
382
|
+
link
|
|
383
|
+
};
|
|
384
|
+
|
|
385
|
+
|
|
386
|
+
/*
|
|
387
|
+
* workController
|
|
388
|
+
*
|
|
389
|
+
* Controller for workers. This knows about all the workers and links. If a connection dies then
|
|
390
|
+
* this tears down all links from the connection. If a worker dies then this tears down all links to
|
|
391
|
+
* that worker.
|
|
392
|
+
*
|
|
393
|
+
*/
|
|
394
|
+
|
|
395
|
+
closure workController(api, local workco) {
|
|
396
|
+
workco = new(object, this)
|
|
397
|
+
workco.serverID = "Workers" // our serverID, used for communications
|
|
398
|
+
workco.workers = { } // dictionary of workers by workerID
|
|
399
|
+
workco.links = new(weakmap)
|
|
400
|
+
api.Register(workco.serverID, workco)
|
|
401
|
+
|
|
402
|
+
// addLinkForConnection
|
|
403
|
+
// Add a link for a connection under its workerID.
|
|
404
|
+
|
|
405
|
+
function addLinkForConnection(conn, workerID, link, local iddict) {
|
|
406
|
+
iddict = workco.links[conn]
|
|
407
|
+
if !iddict
|
|
408
|
+
workco.links[conn] = iddict = { }
|
|
409
|
+
iddict[workerID] = link
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
// deleteLinkForConnection
|
|
413
|
+
// Remove a link for a connection under its workerID, or false.
|
|
414
|
+
|
|
415
|
+
function deleteLinkForConnection(conn, workerID) {
|
|
416
|
+
if workco.links[conn][workerID]
|
|
417
|
+
workco.links[conn][workerID] = undefined
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
// makeSender
|
|
421
|
+
// Sender factory for specified connection and message.
|
|
422
|
+
|
|
423
|
+
closure makeSender(conn, message) {
|
|
424
|
+
function sender(respOp, payload) {
|
|
425
|
+
conn.send({
|
|
426
|
+
clientID: message.clientID
|
|
427
|
+
serverID: message.serverID
|
|
428
|
+
workerID: message.workerID
|
|
429
|
+
respOp: respOp
|
|
430
|
+
payload: payload
|
|
431
|
+
})
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
// makeLink
|
|
436
|
+
// Link the specified message, connection and worker.
|
|
437
|
+
|
|
438
|
+
function makeLink(message, conn, local worker, link) {
|
|
439
|
+
worker = workco.workers[message.workerID]
|
|
440
|
+
link = workLink(workco, conn, worker, makeSender(conn, message))
|
|
441
|
+
addLinkForConnection(conn, message.workerID, link)
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
// termination
|
|
445
|
+
// Note that a worker has terminated.
|
|
446
|
+
|
|
447
|
+
function termination(workerID) {
|
|
448
|
+
workco.current.vitalUpdate({
|
|
449
|
+
id: "termination", // note a thread has gone away
|
|
450
|
+
workerID: workerID
|
|
451
|
+
})
|
|
452
|
+
workco.workers[workerID] = undefined // it's like this worker has never been
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
//
|
|
456
|
+
// wsReceive
|
|
457
|
+
//
|
|
458
|
+
// Process a message from the websocket API.
|
|
459
|
+
//
|
|
460
|
+
|
|
461
|
+
workco.wsReceive = function wsReceive(message, conn, local worker, link, error) {
|
|
462
|
+
if !message.workerID
|
|
463
|
+
error = Error("workerID required", message.workerOp)
|
|
464
|
+
else
|
|
465
|
+
{
|
|
466
|
+
worker = workco.workers[message.workerID]
|
|
467
|
+
link = workco.links[conn][message.workerID]
|
|
468
|
+
if message.workerOp == "msgin" {
|
|
469
|
+
if worker && message.payload {
|
|
470
|
+
if !link
|
|
471
|
+
makeLink(message, conn) // make sure we have a link for responses
|
|
472
|
+
worker.msgin(message.payload) // send message to worker
|
|
473
|
+
return } }
|
|
474
|
+
else if message.workerOp == "echo" {
|
|
475
|
+
if worker && message.payload {
|
|
476
|
+
if !link
|
|
477
|
+
makeLink(message, conn) // make sure we have a link for responses
|
|
478
|
+
worker.echo(message.payload, link) // echo message to other links
|
|
479
|
+
return } }
|
|
480
|
+
else if message.workerOp == "spawn" {
|
|
481
|
+
if !worker { // create worker if needed
|
|
482
|
+
if message.workerID == "NideServer"
|
|
483
|
+
worker = workco.current = workCurrent(message.workerID)
|
|
484
|
+
else
|
|
485
|
+
worker = workThread(message.workerID, message.payload)
|
|
486
|
+
workco.workers[message.workerID] = worker }
|
|
487
|
+
if !link
|
|
488
|
+
makeLink(message, conn) // make sure we have a link for responses
|
|
489
|
+
if message.workerID == "NideServer"
|
|
490
|
+
workco.current.vitalUpdate({
|
|
491
|
+
id: "workerlist", // send the initial list of workers
|
|
492
|
+
workerIDs: workco.workers.*
|
|
493
|
+
})
|
|
494
|
+
else {
|
|
495
|
+
worker.vitalWatch(termination)
|
|
496
|
+
workco.current.vitalUpdate({
|
|
497
|
+
id: "spawned", // note a new worker thread
|
|
498
|
+
name: message.payload.name
|
|
499
|
+
workerID: message.workerID
|
|
500
|
+
}) }
|
|
501
|
+
return }
|
|
502
|
+
else if message.workerOp == "terminate" {
|
|
503
|
+
if worker {
|
|
504
|
+
if !link
|
|
505
|
+
makeLink(message, conn) // make sure we have a link for responses
|
|
506
|
+
worker.terminate() // echo message to other links
|
|
507
|
+
return } }
|
|
508
|
+
error = Error("invalid parameters", message.workerOp, message.workerID)
|
|
509
|
+
}
|
|
510
|
+
call(makeSender(conn, message), "error", [error])
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
//
|
|
514
|
+
// wsClose
|
|
515
|
+
//
|
|
516
|
+
// Note that the connection with the client was closed.
|
|
517
|
+
//
|
|
518
|
+
|
|
519
|
+
workco.wsClose = function wsClose(conn, local workerID) {
|
|
520
|
+
for workerID in workco.workers {
|
|
521
|
+
link = workco.links[conn][workerID]
|
|
522
|
+
if link
|
|
523
|
+
link.closeConn()
|
|
524
|
+
deleteLinkForConnection(conn, workerID)
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
//
|
|
529
|
+
// closeLink
|
|
530
|
+
//
|
|
531
|
+
// Note that a link with the worker has closed.
|
|
532
|
+
//
|
|
533
|
+
|
|
534
|
+
workco.closeLink = function closeLink(link, conn, workerID) {
|
|
535
|
+
deleteLinkForConnection(conn, workerID)
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
// finis
|
|
539
|
+
workco
|
|
540
|
+
};
|
|
541
|
+
|
|
542
|
+
|
|
543
|
+
/*
|
|
544
|
+
* workInit
|
|
545
|
+
*
|
|
546
|
+
* Initialize the worker controller.
|
|
547
|
+
*
|
|
548
|
+
*/
|
|
549
|
+
|
|
550
|
+
function workInit(local manifest) {
|
|
551
|
+
|
|
552
|
+
manifest = `(workCurrent, workThread, workLink, workController, workInit)
|
|
553
|
+
|
|
554
|
+
Naan.module.build(module.id, "worker", function(modobj, compobj) {
|
|
555
|
+
require("./node.nlg")
|
|
556
|
+
threads = js.r("worker_threads") // ### should be on reload
|
|
557
|
+
compobj.manifest = manifest
|
|
558
|
+
modobj.exports.WorkerController = workController
|
|
559
|
+
})
|
|
560
|
+
}();
|