func runServiceLoop()

in main.go [266:319]


func runServiceLoop(ctx context.Context) {
	go runInternalPeriodics(ctx)

	// This is just to ensure WaitForTaskNotification runs before any other tasks.
	c := make(chan struct{})
	// Configures WaitForTaskNotification, waits for config changes with WatchConfig.
	go runTaskLoop(ctx, c)
	// Don't continue any other tasks until WaitForTaskNotification has run.
	<-c

	// Runs functions that need to run on a set interval.
	ticker := time.NewTicker(agentconfig.SvcPollInterval())
	defer ticker.Stop()
	// First inventory run will be somewhere between 3 and 5 min.
	firstInventory := time.After(time.Duration(rand.Intn(120)+180) * time.Second)
	ranFirstInventory := false
	for {
		if agentconfig.GuestPoliciesEnabled() {
			policies.Run(ctx)
		}

		if agentconfig.OSInventoryEnabled() {
			if !ranFirstInventory {
				// Only run first inventory after the set waiting period or if the main poll ticker ticks.
				// The default SvcPollInterval is 10min so under normal circumstances firstInventory will
				// always fire first.
				select {
				case <-ticker.C:
				case <-firstInventory:
				case <-ctx.Done():
					return
				}
				ranFirstInventory = true
			}

			// This should always run after ospackage.SetConfig.
			tasker.Enqueue(ctx, "Report OSInventory", func() {
				client, err := agentendpoint.NewClient(ctx)
				if err != nil {
					logger.Errorf("%v", err.Error())
				}
				client.ReportInventory(ctx)
				client.Close()
			})
		}

		select {
		case <-ticker.C:
			continue
		case <-ctx.Done():
			return
		}
	}
}