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