public registerWorkerFleet()

in packages/aws-rfdk/lib/core/lib/load-balancer-manager.ts [232:332]


  public registerWorkerFleet(
    loadBalancer: ApplicationLoadBalancer,
    fleet: IMonitorableFleet,
    healthCheckConfig: HealthCheckConfig,
    healthMonitorProps: HealthMonitorProps) {

    // this initializes with 0 and keeps the track of all components
    // newly added down the hierarchy.
    const statsDelta = new LoadBalancerComponentStats();

    // Do all the load balancer level service limit checks first

    // check for target limit in load balancer
    const targetPerLoadBalancerLimit = LoadBalancerFactory.getAccountLimit('targets-per-application-load-balancer',
      LoadBalancerFactory.DEFAULT_TARGETS_PER_APPLICATION_LOAD_BALANCER,
      healthMonitorProps.elbAccountLimits);
    if ((this.loadBalancerComponentCount.targetCount + fleet.targetCapacity) > targetPerLoadBalancerLimit) {
      throw new AWSLimitExhaustedError('AWS service limit "targets-per-application-load-balancer" reached. Limit: ' +
        targetPerLoadBalancerLimit);
    }

    // check for target group limit in load balancer
    const targetGroupsPerLoadBalancerLimit = LoadBalancerFactory.getAccountLimit('target-groups-per-application-load-balancer',
      LoadBalancerFactory.DEFAULT_TARGET_GROUPS_PER_APPLICATION_LOAD_BALANCER,
      healthMonitorProps.elbAccountLimits);
    if ((this.loadBalancerComponentCount.targetGroupCount + 1) > targetGroupsPerLoadBalancerLimit) {
      throw new AWSLimitExhaustedError('AWS service limit "target-groups-per-application-load-balancer" reached. Limit: ' +
        targetGroupsPerLoadBalancerLimit);
    }

    let listenerParent = null;
    let targetGroupParent = null;

    // try registering to each listener.
    for (const [listener, listenerMeta] of this.listenerMap.entries()) {

      try {
        const {componentsAdded, targetGroup} = listenerMeta.registerWorkerFleet(
          loadBalancer,
          listener,
          fleet,
          healthCheckConfig,
          healthMonitorProps);

        statsDelta.add(componentsAdded);
        listenerParent = listener;
        targetGroupParent = targetGroup;
        break;
      } catch (e) {
        // suppress all AWSLimitExhaustedError, we will scale in case of this error
        /* istanbul ignore next */
        if (!(e instanceof AWSLimitExhaustedError)) {
          /* istanbul ignore next */
          throw e;
        }
      }
    }

    /* istanbul ignore next */
    if (!listenerParent) {
      // If this section is reached, no listener was found which could accommodate fleet
      // create new listener and register

      const listenersPerLoadBalancerLimit = LoadBalancerFactory.getAccountLimit('listeners-per-application-load-balancer',
        LoadBalancerFactory.DEFAULT_LISTENERS_PER_APPLICATION_LOAD_BALANCER,
        healthMonitorProps.elbAccountLimits);
      if ((this.loadBalancerComponentCount.listenerCount + 1) > listenersPerLoadBalancerLimit) {
        throw new AWSLimitExhaustedError('AWS service limit "listeners-per-application-load-balancer" reached. Limit: ' +
          listenersPerLoadBalancerLimit);
      }

      listenerParent = this.createListener(fleet.targetScope, loadBalancer);
      const listenerManager = new ListenerManager();

      this.listenerMap.set(listenerParent, listenerManager);
      statsDelta.add(new LoadBalancerComponentStats(1, 0, 0));

      try {
        const {componentsAdded, targetGroup} = listenerManager.registerWorkerFleet(
          loadBalancer,
          listenerParent,
          fleet,
          healthCheckConfig,
          healthMonitorProps);

        targetGroupParent = targetGroup;
        statsDelta.add(componentsAdded);
      } catch (e) {
        throw e;
      }
    }

    // update the current load balancer's stats
    this.loadBalancerComponentCount.add(statsDelta);

    return {
      componentsAdded: statsDelta,
      listener: listenerParent,
      targetGroup: targetGroupParent,
    };
  }