incyclist-services 1.0.68 → 1.0.69

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -39,7 +39,10 @@ export declare class DeviceRideService extends EventEmitter {
39
39
  startAdapters(adapters: AdapterRideInfo[], startType: 'start' | 'check' | 'pair', props?: RideServiceDeviceProperties): Promise<boolean>;
40
40
  startHealthCheck(ai: AdapterRideInfo): void;
41
41
  stopHealthCheck(ai: AdapterRideInfo): void;
42
- prepareReconnect(down: AdapterRideInfo): Promise<void>;
42
+ prepareReconnect(unhealthy: AdapterRideInfo): Promise<void>;
43
+ private reconnectInterface;
44
+ protected stopAfterRestart(unhealthy: AdapterRideInfo): Promise<void>;
45
+ private reconnectSingle;
43
46
  start(props: RideServiceDeviceProperties): Promise<boolean>;
44
47
  startRetry(props: RideServiceDeviceProperties): Promise<boolean>;
45
48
  cancelStart(): Promise<boolean>;
@@ -481,7 +481,6 @@ class DeviceRideService extends events_1.default {
481
481
  ai.dataStatus = 'green';
482
482
  return;
483
483
  }
484
- const prevStatus = ai.dataStatus;
485
484
  const isAmber = (tsNow - ai.tsLastData) > NO_DATA_THRESHOLD;
486
485
  const isRed = (tsNow - ai.tsLastData) > UNHEALTHY_THRESHOLD;
487
486
  ai.dataStatus = 'green';
@@ -489,7 +488,7 @@ class DeviceRideService extends events_1.default {
489
488
  ai.dataStatus = 'amber';
490
489
  if (isRed)
491
490
  ai.dataStatus = 'red';
492
- if (ai.isHealthy && isAmber) {
491
+ if (ai.isHealthy && (isAmber || isRed)) {
493
492
  ai.isHealthy = false;
494
493
  this.logEvent({ message: 'device unhealthy', device: ai.adapter.getUniqueName(), udid: ai.udid });
495
494
  const { enabledCapabilities } = this.getEnabledCapabilities(ai);
@@ -497,7 +496,7 @@ class DeviceRideService extends events_1.default {
497
496
  console.log('~~~~ health check: prepare reconnect');
498
497
  this.prepareReconnect(ai);
499
498
  }
500
- else if (!ai.isHealthy && !isAmber) {
499
+ else if (!ai.isHealthy && !isAmber && !isRed) {
501
500
  const { enabledCapabilities } = this.getEnabledCapabilities(ai);
502
501
  ai.isHealthy = true;
503
502
  this.logEvent({ message: 'device healthy', device: ai.adapter.getUniqueName(), udid: ai.udid });
@@ -514,75 +513,123 @@ class DeviceRideService extends events_1.default {
514
513
  delete ai.isHealthy;
515
514
  }
516
515
  }
517
- prepareReconnect(down) {
516
+ prepareReconnect(unhealthy) {
518
517
  return __awaiter(this, void 0, void 0, function* () {
519
- if (down.isRestarting)
518
+ if (unhealthy.isRestarting)
520
519
  return;
521
- console.log('~~~~ health check: wait 45s', down.udid);
520
+ console.log('~~~~ health check: wait 45s', unhealthy.udid);
522
521
  yield (0, sleep_1.sleep)(UNHEALTHY_THRESHOLD - NO_DATA_THRESHOLD - 5000);
523
- console.log('~~~~ health check: check if still unhealthy', down.udid, down.isHealthy, down.isRestarting);
524
- if (down.isHealthy || down.isRestarting)
522
+ if (unhealthy.isHealthy || unhealthy.isRestarting)
525
523
  return;
526
- const ifName = down.adapter.getInterface();
524
+ console.log('~~~~ health check: still unhealthy', unhealthy.udid, unhealthy.isHealthy, unhealthy.isRestarting);
525
+ unhealthy.isRestarting = true;
526
+ const ifName = unhealthy.adapter.getInterface();
527
527
  const adapters = this.getAdapterList().filter(ai => ai.adapter.getInterface() === ifName);
528
- down.isRestarting = true;
529
528
  if (!adapters.find(ai => ai.isHealthy)) {
530
- console.log('~~~~ health check: restart interface', ifName);
531
- this.logger.logEvent({ message: 'restart interface', interface: ifName });
532
- const i = incyclist_devices_1.InterfaceFactory.create(ifName);
533
- try {
534
- const promisesStop = [];
535
- adapters.map(ai => {
536
- ai.isRestarting = true;
537
- ai.adapter.off('data', this.deviceDataHandler);
529
+ yield this.reconnectInterface(ifName, adapters);
530
+ }
531
+ else {
532
+ yield this.reconnectSingle(unhealthy);
533
+ }
534
+ unhealthy.isRestarting = false;
535
+ });
536
+ }
537
+ reconnectInterface(ifName, adapters) {
538
+ return __awaiter(this, void 0, void 0, function* () {
539
+ console.log('~~~~ health check: restart interface', ifName);
540
+ this.logger.logEvent({ message: 'restart interface', interface: ifName });
541
+ let stopRequested = false;
542
+ this.once('stop-ride', () => { stopRequested = true; });
543
+ const i = incyclist_devices_1.InterfaceFactory.create(ifName);
544
+ try {
545
+ const promisesStop = [];
546
+ adapters.map(ai => {
547
+ if (ai.isRestarting) {
548
+ promisesStop.push(this.stopAfterRestart(ai));
549
+ }
550
+ else {
538
551
  promisesStop.push(ai.adapter.stop());
539
- });
540
- if (promisesStop.length > 0) {
541
- yield Promise.allSettled(promisesStop);
542
552
  }
553
+ ai.isRestarting = true;
554
+ ai.adapter.off('data', this.deviceDataHandler);
555
+ });
556
+ if (promisesStop.length > 0) {
557
+ yield Promise.allSettled(promisesStop);
558
+ }
559
+ if (!stopRequested) {
543
560
  yield i.disconnect();
544
561
  yield (0, sleep_1.sleep)(1000);
545
- yield i.connect;
562
+ yield i.connect();
563
+ }
564
+ if (!stopRequested) {
546
565
  const promisesStart = [];
547
566
  adapters.map(ai => { promisesStart.push(ai.adapter.start()); });
548
567
  if (promisesStart.length > 0) {
549
568
  yield Promise.allSettled(promisesStart);
550
569
  }
551
570
  }
552
- catch (err) {
553
- this.logger.logEvent({ message: 'restart interface failed', interface: ifName, reason: err.message });
571
+ }
572
+ catch (err) {
573
+ this.logger.logEvent({ message: 'restart interface failed', interface: ifName, reason: err.message });
574
+ }
575
+ adapters.map(ai => {
576
+ if (ai.adapter.isStarted()) {
577
+ ai.tsLastData = Date.now();
554
578
  }
555
- adapters.map(ai => {
556
- if (ai.adapter.isStarted()) {
557
- ai.isHealthy = true;
558
- ai.dataStatus = 'green';
559
- }
560
- else {
561
- ai.isHealthy = false;
562
- ai.dataStatus = 'red';
563
- this.prepareReconnect(ai);
564
- }
565
- ai.adapter.on('data', this.deviceDataHandler);
579
+ else {
566
580
  ai.isRestarting = false;
581
+ this.prepareReconnect(ai);
582
+ }
583
+ ai.adapter.on('data', this.deviceDataHandler);
584
+ ai.isRestarting = false;
585
+ });
586
+ });
587
+ }
588
+ stopAfterRestart(unhealthy) {
589
+ return __awaiter(this, void 0, void 0, function* () {
590
+ if (unhealthy.isRestarting) {
591
+ yield new Promise(done => {
592
+ this.once('stop-adapter-confirmed', (udid) => {
593
+ if (udid === unhealthy.udid)
594
+ done();
595
+ });
567
596
  });
568
597
  }
569
- else {
570
- console.log('~~~~ health check: restart adapter', down.udid);
571
- this.logger.logEvent({ message: 'restart adapter', device: down.udid });
572
- down.adapter.off('data', this.deviceDataHandler);
573
- const adapter = down.adapter;
598
+ yield unhealthy.adapter.stop();
599
+ });
600
+ }
601
+ reconnectSingle(unhealthy) {
602
+ return __awaiter(this, void 0, void 0, function* () {
603
+ let stopRequested = false;
604
+ this.once('stop-ride', () => { stopRequested = true; });
605
+ this.once('stop-adapter', (udid) => {
606
+ if (udid === unhealthy.udid)
607
+ stopRequested = true;
608
+ });
609
+ let success = false;
610
+ do {
611
+ console.log('~~~~ health check: restart adapter', unhealthy.udid);
612
+ this.logger.logEvent({ message: 'restart adapter', device: unhealthy.udid });
613
+ unhealthy.adapter.off('data', this.deviceDataHandler);
614
+ const adapter = unhealthy.adapter;
574
615
  try {
575
- yield adapter.restart();
576
- down.isHealthy = true;
577
- down.dataStatus = 'green';
616
+ const started = yield adapter.restart();
617
+ if (started) {
618
+ unhealthy.tsLastData = Date.now();
619
+ success = true;
620
+ }
578
621
  }
579
622
  catch (err) {
580
- this.logger.logEvent({ message: 'restart adapter failed', device: down.udid, reason: err.message });
581
- this.prepareReconnect(down);
623
+ this.logger.logEvent({ message: 'restart adapter failed', device: unhealthy.udid, reason: err.message });
624
+ this.prepareReconnect(unhealthy);
582
625
  }
583
- down.adapter.on('data', this.deviceDataHandler);
584
- }
585
- down.isRestarting = false;
626
+ unhealthy.adapter.on('data', this.deviceDataHandler);
627
+ if (!success) {
628
+ for (let i = 0; i < 60 && !stopRequested; i++)
629
+ yield (0, sleep_1.sleep)(1000);
630
+ }
631
+ } while (!stopRequested && !success);
632
+ this.emit('stop-adapter-confirmed', unhealthy.udid);
586
633
  });
587
634
  }
588
635
  start(props) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "incyclist-services",
3
- "version": "1.0.68",
3
+ "version": "1.0.69",
4
4
  "peerDependencies": {
5
5
  "gd-eventlog": "^0.1.26"
6
6
  },