| 391 | } |
| 392 | |
| 393 | async _stop(options = {}) { |
| 394 | |
| 395 | options.timeout = options.timeout ?? 5000; // Default timeout to 5 seconds |
| 396 | |
| 397 | if (['stopped', 'initialized', 'started', 'invalid'].indexOf(this.phase) === -1) { |
| 398 | throw new Error('Cannot stop server while in ' + this.phase + ' phase'); |
| 399 | } |
| 400 | |
| 401 | this.phase = 'stopping'; |
| 402 | |
| 403 | try { |
| 404 | await this._invoke('onPreStop'); |
| 405 | |
| 406 | if (this.started) { |
| 407 | this.started = false; |
| 408 | this.info.started = 0; |
| 409 | |
| 410 | await this._unlisten(options.timeout); |
| 411 | } |
| 412 | |
| 413 | const caches = []; |
| 414 | this.caches.forEach((cache) => caches.push(cache.client.stop())); |
| 415 | await Promise.all(caches); |
| 416 | |
| 417 | this.events.emit('stop'); |
| 418 | this.heavy.stop(); |
| 419 | |
| 420 | if (this.controlled) { |
| 421 | await Promise.all(this.controlled.map((control) => control.stop(options))); |
| 422 | } |
| 423 | |
| 424 | await this._invoke('onPostStop'); |
| 425 | this.phase = 'stopped'; |
| 426 | } |
| 427 | catch (err) { |
| 428 | this.phase = 'invalid'; |
| 429 | throw err; |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | _unlisten(timeout) { |
| 434 | |