1 | 'use strict';
|
---|
2 |
|
---|
3 | function _worker_threads() {
|
---|
4 | const data = require('worker_threads');
|
---|
5 |
|
---|
6 | _worker_threads = function () {
|
---|
7 | return data;
|
---|
8 | };
|
---|
9 |
|
---|
10 | return data;
|
---|
11 | }
|
---|
12 |
|
---|
13 | var _types = require('../types');
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
|
---|
17 | *
|
---|
18 | * This source code is licensed under the MIT license found in the
|
---|
19 | * LICENSE file in the root directory of this source tree.
|
---|
20 | */
|
---|
21 | let file = null;
|
---|
22 | let setupArgs = [];
|
---|
23 | let initialized = false;
|
---|
24 | /**
|
---|
25 | * This file is a small bootstrapper for workers. It sets up the communication
|
---|
26 | * between the worker and the parent process, interpreting parent messages and
|
---|
27 | * sending results back.
|
---|
28 | *
|
---|
29 | * The file loaded will be lazily initialized the first time any of the workers
|
---|
30 | * is called. This is done for optimal performance: if the farm is initialized,
|
---|
31 | * but no call is made to it, child Node processes will be consuming the least
|
---|
32 | * possible amount of memory.
|
---|
33 | *
|
---|
34 | * If an invalid message is detected, the child will exit (by throwing) with a
|
---|
35 | * non-zero exit code.
|
---|
36 | */
|
---|
37 |
|
---|
38 | const messageListener = request => {
|
---|
39 | switch (request[0]) {
|
---|
40 | case _types.CHILD_MESSAGE_INITIALIZE:
|
---|
41 | const init = request;
|
---|
42 | file = init[2];
|
---|
43 | setupArgs = request[3];
|
---|
44 | break;
|
---|
45 |
|
---|
46 | case _types.CHILD_MESSAGE_CALL:
|
---|
47 | const call = request;
|
---|
48 | execMethod(call[2], call[3]);
|
---|
49 | break;
|
---|
50 |
|
---|
51 | case _types.CHILD_MESSAGE_END:
|
---|
52 | end();
|
---|
53 | break;
|
---|
54 |
|
---|
55 | default:
|
---|
56 | throw new TypeError(
|
---|
57 | 'Unexpected request from parent process: ' + request[0]
|
---|
58 | );
|
---|
59 | }
|
---|
60 | };
|
---|
61 |
|
---|
62 | _worker_threads().parentPort.on('message', messageListener);
|
---|
63 |
|
---|
64 | function reportSuccess(result) {
|
---|
65 | if (_worker_threads().isMainThread) {
|
---|
66 | throw new Error('Child can only be used on a forked process');
|
---|
67 | }
|
---|
68 |
|
---|
69 | _worker_threads().parentPort.postMessage([_types.PARENT_MESSAGE_OK, result]);
|
---|
70 | }
|
---|
71 |
|
---|
72 | function reportClientError(error) {
|
---|
73 | return reportError(error, _types.PARENT_MESSAGE_CLIENT_ERROR);
|
---|
74 | }
|
---|
75 |
|
---|
76 | function reportInitializeError(error) {
|
---|
77 | return reportError(error, _types.PARENT_MESSAGE_SETUP_ERROR);
|
---|
78 | }
|
---|
79 |
|
---|
80 | function reportError(error, type) {
|
---|
81 | if (_worker_threads().isMainThread) {
|
---|
82 | throw new Error('Child can only be used on a forked process');
|
---|
83 | }
|
---|
84 |
|
---|
85 | if (error == null) {
|
---|
86 | error = new Error('"null" or "undefined" thrown');
|
---|
87 | }
|
---|
88 |
|
---|
89 | _worker_threads().parentPort.postMessage([
|
---|
90 | type,
|
---|
91 | error.constructor && error.constructor.name,
|
---|
92 | error.message,
|
---|
93 | error.stack,
|
---|
94 | typeof error === 'object' ? {...error} : error
|
---|
95 | ]);
|
---|
96 | }
|
---|
97 |
|
---|
98 | function end() {
|
---|
99 | const main = require(file);
|
---|
100 |
|
---|
101 | if (!main.teardown) {
|
---|
102 | exitProcess();
|
---|
103 | return;
|
---|
104 | }
|
---|
105 |
|
---|
106 | execFunction(main.teardown, main, [], exitProcess, exitProcess);
|
---|
107 | }
|
---|
108 |
|
---|
109 | function exitProcess() {
|
---|
110 | // Clean up open handles so the worker ideally exits gracefully
|
---|
111 | _worker_threads().parentPort.removeListener('message', messageListener);
|
---|
112 | }
|
---|
113 |
|
---|
114 | function execMethod(method, args) {
|
---|
115 | const main = require(file);
|
---|
116 |
|
---|
117 | let fn;
|
---|
118 |
|
---|
119 | if (method === 'default') {
|
---|
120 | fn = main.__esModule ? main['default'] : main;
|
---|
121 | } else {
|
---|
122 | fn = main[method];
|
---|
123 | }
|
---|
124 |
|
---|
125 | function execHelper() {
|
---|
126 | execFunction(fn, main, args, reportSuccess, reportClientError);
|
---|
127 | }
|
---|
128 |
|
---|
129 | if (initialized || !main.setup) {
|
---|
130 | execHelper();
|
---|
131 | return;
|
---|
132 | }
|
---|
133 |
|
---|
134 | initialized = true;
|
---|
135 | execFunction(main.setup, main, setupArgs, execHelper, reportInitializeError);
|
---|
136 | }
|
---|
137 |
|
---|
138 | const isPromise = obj =>
|
---|
139 | !!obj &&
|
---|
140 | (typeof obj === 'object' || typeof obj === 'function') &&
|
---|
141 | typeof obj.then === 'function';
|
---|
142 |
|
---|
143 | function execFunction(fn, ctx, args, onResult, onError) {
|
---|
144 | let result;
|
---|
145 |
|
---|
146 | try {
|
---|
147 | result = fn.apply(ctx, args);
|
---|
148 | } catch (err) {
|
---|
149 | onError(err);
|
---|
150 | return;
|
---|
151 | }
|
---|
152 |
|
---|
153 | if (isPromise(result)) {
|
---|
154 | result.then(onResult, onError);
|
---|
155 | } else {
|
---|
156 | onResult(result);
|
---|
157 | }
|
---|
158 | }
|
---|