func runTaskLoop()

in main.go [195:240]


func runTaskLoop(ctx context.Context, c chan struct{}) {
	var taskNotificationClient *agentendpoint.Client
	var err error
	for {
		// Set debug logging settings so that customers don't need to restart the agent.
		logger.SetDebugLogging(agentconfig.Debug())
		clog.DebugEnabled = agentconfig.Debug()
		if agentconfig.TaskNotificationEnabled() && taskNotificationClient == nil {
			// Call RegisterAgent now since we just either started running or were just enabled.
			// This call is blocking until successful as we can't continue unless register agent has completed.
			registerAgent(ctx)
		}
		if agentconfig.TaskNotificationEnabled() && (taskNotificationClient == nil || taskNotificationClient.Closed()) {
			// Start WaitForTaskNotification if we need to.
			taskNotificationClient, err = agentendpoint.NewClient(ctx)
			if err != nil {
				clog.Errorf(ctx, "%v", err.Error())
			} else {
				taskNotificationClient.WaitForTaskNotification(ctx)
			}
		} else if !agentconfig.TaskNotificationEnabled() && taskNotificationClient != nil && !taskNotificationClient.Closed() {
			// Cancel WaitForTaskNotification if we need to, this will block if there is
			// an existing current task running.
			if err := taskNotificationClient.Close(); err != nil {
				clog.Errorf(ctx, "%v", err.Error())
			}
		}

		// This is just to signal WaitForTaskNotification has run if needed.
		select {
		case c <- struct{}{}:
		default:
		}

		// Wait on any metadata config change.
		if err := agentconfig.WatchConfig(ctx); err != nil {
			clog.Errorf(ctx, "%v", err.Error())
		}
		select {
		case <-ctx.Done():
			return
		default:
			continue
		}
	}
}