MCPcopy Index your code
hub / github.com/cryptpad/cryptpad / launchWorker

Function launchWorker

server.js:120–171  ·  view source on GitHub ↗
(online)

Source from the content-addressed store, hash-verified

118 });
119
120 var launchWorker = (online) => {
121 var worker = Cluster.fork(workerState);
122 var pid = worker.process.pid;
123 worker.on('online', () => {
124 online();
125 });
126
127 worker.on('message', msg => {
128 if (!msg) { return; }
129 var txid = msg.txid;
130 var content = msg.content;
131 if (!content) { return; }
132
133 var command = COMMANDS[content.command];
134 if (typeof(command) !== 'function') {
135 return void Env.Log.error('UNHANDLED_HTTP_WORKER_COMMAND', msg);
136 }
137
138 const cb = Util.once(Util.mkAsync(function (err, value) {
139 worker.send({
140 type: 'REPLY',
141 error: Util.serializeError(err),
142 txid: txid,
143 pid: msg.pid,
144 value: value,
145 });
146 }));
147
148 command(content, cb);
149 });
150
151 worker.on('exit', (code, signal) => {
152 Object.keys(Env.plugins || {}).forEach(name => {
153 let plugin = Env.plugins[name];
154 if (!plugin.onWorkerClosed) { return; }
155 try { plugin.onWorkerClosed("http-worker", pid); }
156 catch (e) {}
157 });
158
159 if (!signal && code === 0) { return; }
160 // relaunch http workers if they crash
161 Env.Log.error('HTTP_WORKER_EXIT', {
162 signal,
163 code,
164 });
165 // update the environment with the latest state before relaunching
166 workerState.Env = Environment.serialize(Env);
167 launchWorker(function () {
168 Env.Log.info('HTTP_WORKER_RELAUNCH', {});
169 });
170 });
171 };
172
173 var txids = {};
174

Callers 1

server.jsFile · 0.85

Calls 2

serializeMethod · 0.80
onMethod · 0.45

Tested by

no test coverage detected