(action, event, format, target)
| 183 | // Returns emitter that you can use to handle events on failure or success (i.e 'error' or <event>) |
| 184 | // |
| 185 | function stopOrRestart(action, event, format, target) { |
| 186 | const emitter = newEmitter(); |
| 187 | |
| 188 | function sendAction(proc, next) { |
| 189 | const socket = new nssocket.NsSocket(); |
| 190 | |
| 191 | function onMessage(data) { |
| 192 | // |
| 193 | // Cleanup the socket. |
| 194 | // |
| 195 | socket.undata([action, 'ok'], onMessage); |
| 196 | socket.undata([action, 'error'], onMessage); |
| 197 | socket.end(); |
| 198 | |
| 199 | // |
| 200 | // Messages are only sent back from error cases. The event |
| 201 | // calling context is available from `nssocket`. |
| 202 | // |
| 203 | const message = data && data.message |
| 204 | const type = this.event.slice().pop(); |
| 205 | |
| 206 | // |
| 207 | // Remark (Tjatse): This message comes from `forever-monitor`, the process is marked |
| 208 | // as `STOPPED`: message: Cannot stop process that is not running. |
| 209 | // |
| 210 | // Remark (indexzero): We should probably warn instead of emitting an error in `forever-monitor`, |
| 211 | // OR handle that error in `bin/worker` for better RPC. |
| 212 | // |
| 213 | return type === 'error' && /is not running/.test(message) |
| 214 | ? next(new Error(message)) |
| 215 | : next(null, data); |
| 216 | } |
| 217 | |
| 218 | socket.connect(proc.socket, function (err) { |
| 219 | if (err) { |
| 220 | next(err); |
| 221 | } |
| 222 | |
| 223 | socket.dataOnce([action, 'ok'], onMessage); |
| 224 | socket.dataOnce([action, 'error'], onMessage); |
| 225 | socket.send([action]); |
| 226 | }); |
| 227 | |
| 228 | // |
| 229 | // Remark (indexzero): This is a race condition, but unlikely to ever hit since |
| 230 | // if the socket errors we will never get any data in the first place... |
| 231 | // |
| 232 | socket.on('error', function (err) { |
| 233 | next(err); |
| 234 | }); |
| 235 | } |
| 236 | |
| 237 | |
| 238 | getAllProcesses(function (err, processes) { |
| 239 | if (err) { |
| 240 | return process.nextTick(function () { |
| 241 | emitter.emit('error', err); |
| 242 | }); |
no test coverage detected