code: func (nodeInfo *NodeInfo) addTask(t *api.Task) bool {
  	oldTask, ok := nodeInfo.Tasks[t.ID]
  	if ok {
  		if t.DesiredState <= api.TaskStateRunning && oldTask.DesiredState > api.TaskStateRunning {
  			nodeInfo.Tasks[t.ID] = t
  			nodeInfo.ActiveTasksCount++
  			nodeInfo.ActiveTasksCountByService[t.ServiceID]++
  			return true
  		} else if t.DesiredState > api.TaskStateRunning && oldTask.DesiredState <= api.TaskStateRunning {
  			nodeInfo.Tasks[t.ID] = t
  			nodeInfo.ActiveTasksCount--
  			nodeInfo.ActiveTasksCountByService[t.ServiceID]--
  			return true
  		}
  		return false
  	}
  
  	nodeInfo.Tasks[t.ID] = t
  
  	reservations := taskReservations(t.Spec)
  	resources := nodeInfo.AvailableResources
  
  	resources.MemoryBytes -= reservations.MemoryBytes
  	resources.NanoCPUs -= reservations.NanoCPUs
  
  	// minimum size required
  	t.AssignedGenericResources = make([]*api.GenericResource, 0, len(resources.Generic))
  	taskAssigned := &t.AssignedGenericResources
  
  	genericresource.Claim(&resources.Generic, taskAssigned, reservations.Generic)
  
  	if t.Endpoint != nil {
  		for _, port := range t.Endpoint.Ports {
  			if port.PublishMode == api.PublishModeHost && port.PublishedPort != 0 {
  				portSpec := hostPortSpec{protocol: port.Protocol, publishedPort: port.PublishedPort}
  				nodeInfo.usedHostPorts[portSpec] = struct{}{}
  			}
  		}
  	}
  
  	if t.DesiredState <= api.TaskStateRunning {
  		nodeInfo.ActiveTasksCount++
  		nodeInfo.ActiveTasksCountByService[t.ServiceID]++
  	}
  
  	return true
  }
docs: // addTask adds or updates a task on nodeInfo, and returns true if nodeInfo was
  // modified.