async run()

in common/lib/plugins/efm2/monitor.ts [153:229]


  async run(): Promise<void> {
    logger.debug(Messages.get("MonitorImpl.startMonitoring", this.hostInfo.host));

    try {
      while (!this.isStopped()) {
        try {
          if (this.activeContexts.length === 0 && !this.hostUnhealthy) {
            await new Promise((resolve) => {
              this.delayMillisTimeoutId = setTimeout(resolve, MonitorImpl.TASK_SLEEP_MILLIS);
            });
            continue;
          }

          const statusCheckStartTimeNanos: number = getCurrentTimeNano();
          const isValid = await this.checkConnectionStatus();
          const statusCheckEndTimeNanos: number = getCurrentTimeNano();

          await this.updateHostHealthStatus(isValid, statusCheckStartTimeNanos, statusCheckEndTimeNanos);

          if (this.hostUnhealthy) {
            this.pluginService.setAvailability(this.hostInfo.aliases, HostAvailability.NOT_AVAILABLE);
          }
          const tmpActiveContexts: WeakRef<MonitorConnectionContext>[] = [];

          let monitorContextRef: WeakRef<MonitorConnectionContext> | undefined;

          while ((monitorContextRef = this.activeContexts?.shift()) != null) {
            if (this.isStopped()) {
              break;
            }

            const monitorContext: MonitorConnectionContext = monitorContextRef?.deref() ?? null;

            if (!monitorContext) {
              continue;
            }

            if (this.hostUnhealthy) {
              // Kill connection
              monitorContext.setHostUnhealthy(true);
              const clientToAbort = monitorContext.getClient();

              monitorContext.setInactive();
              if (clientToAbort != null) {
                await this.endMonitoringClient(clientToAbort);

                this.abortedConnectionsCounter.inc();
              }
            } else if (monitorContext && monitorContext.isActive()) {
              tmpActiveContexts.push(monitorContextRef);
            }
          }

          // activeContexts is empty now and tmpActiveContexts contains all yet active contexts
          // Add active contexts back to the queue.
          this.activeContexts.push(...tmpActiveContexts);

          const delayMillis = (this.failureDetectionIntervalNanos - (statusCheckEndTimeNanos - statusCheckStartTimeNanos)) / 1000000;

          await new Promise((resolve) => {
            this.delayMillisTimeoutId = setTimeout(
              resolve,
              delayMillis < MonitorImpl.TASK_SLEEP_MILLIS ? MonitorImpl.TASK_SLEEP_MILLIS : delayMillis
            );
          });
        } catch (error: any) {
          logger.debug(Messages.get("MonitorImpl.errorDuringMonitoringContinue", error.message));
        }
      }
    } catch (error: any) {
      logger.debug(Messages.get("MonitorImpl.errorDuringMonitoringStop", error.message));
    } finally {
      await this.endMonitoringClient();
    }

    logger.debug(Messages.get("MonitorImpl.stopMonitoring", this.hostInfo.host));
  }