func()

in internal/daemon/daemon.go [108:239]


func (d *Daemon) startdaemonHandler(ctx context.Context, cancel context.CancelFunc) error {
	var err error
	d.config, err = configuration.Load(d.configFilePath, os.ReadFile, d.cloudProps)
	if err != nil {
		if d.lp.OSType == "windows" {
			// Windows gobbles up the error message, so we need to log it separately and exit.
			log.Logger.Error("Invalid configuration file, please fix the configuration file and restart the service.")
			log.Logger.Error(err)
			usagemetrics.Misconfigured()
			os.Exit(1)
		}
		return fmt.Errorf("loading %s configuration file, please fix the configuration file and restart the service: %w", d.configFilePath, err)
	}

	d.lp.LogToCloud = d.config.GetLogToCloud()
	d.lp.Level = configuration.LogLevelToZapcore(d.config.GetLogLevel())
	if d.config.GetCloudProperties().GetProjectId() != "" {
		d.lp.CloudLoggingClient = log.CloudLoggingClient(ctx, d.config.GetCloudProperties().GetProjectId())
	}
	if d.lp.CloudLoggingClient != nil {
		defer d.lp.CloudLoggingClient.Close()
	}

	log.SetupLogging(d.lp)

	log.Logger.Infow("Starting daemon mode", "agent_name", configuration.AgentName, "agent_version", configuration.AgentVersion)
	log.Logger.Infow("Cloud Properties",
		"projectid", d.cloudProps.GetProjectId(),
		"projectnumber", d.cloudProps.GetNumericProjectId(),
		"instanceid", d.cloudProps.GetInstanceId(),
		"zone", d.cloudProps.GetZone(),
		"region", d.cloudProps.GetRegion(),
		"instancename", d.cloudProps.GetInstanceName(),
		"machinetype", d.cloudProps.GetMachineType(),
		"image", d.cloudProps.GetImage(),
	)
	log.Logger.Infow("OS Data",
		"name", d.osData.OSName,
		"vendor", d.osData.OSVendor,
		"version", d.osData.OSVersion,
	)

	configureUsageMetricsForDaemon(d.cloudProps)
	usagemetrics.Configured()
	usagemetrics.Started()

	shutdownch := make(chan os.Signal, 1)
	signal.Notify(shutdownch, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)

	wlmClient, err := workloadmanager.Client(ctx, d.config)
	if err != nil {
		log.Logger.Errorw("Error creating WLM Client", "error", err)
		usagemetrics.Error(usagemetrics.WorkloadManagerConnectionError)
		return err
	}

	// Check if the metric override file exists. If it does, operate in override mode.
	// Override mode will collect metrics from the override file and send them to Data Warehouse.
	// Override mode will not start any other services.
	if fileInfo, err := os.ReadFile(workloadmanager.MetricOverridePath); fileInfo != nil && err == nil {
		log.Logger.Info("Metric override file found. Operating in override mode.")
		metricCollectionService := workloadmanager.Service{Config: d.config, Client: wlmClient}
		metricCollectionCtx := log.SetCtx(ctx, "context", "WorkloadManagerMetrics")
		recoverableStart := &recovery.RecoverableRoutine{
			Routine:             metricCollectionService.CollectAndSendMetricsToDataWarehouse,
			RoutineArg:          d.config,
			ErrorCode:           0,
			ExpectedMinDuration: 0,
			UsageLogger:         *usagemetrics.UsageLogger,
		}
		recoverableStart.StartRoutine(metricCollectionCtx)

		// Log a RUNNING usage metric once a day.
		go usagemetrics.LogRunningDaily()
		d.waitForShutdown(shutdownch, cancel)
		return nil
	}

	log.Logger.Info("Starting common discovery")
	oracleCh := make(chan *servicecommunication.Message, 3)
	mySQLCh := make(chan *servicecommunication.Message, 3)
	redisCh := make(chan *servicecommunication.Message, 3)
	sqlserverCh := make(chan *servicecommunication.Message, 3)
	scChs := []chan<- *servicecommunication.Message{mySQLCh, oracleCh, redisCh, sqlserverCh}
	commondiscovery := discovery.Service{
		ProcessLister: discovery.DefaultProcessLister{},
		ReadFile:      os.ReadFile,
		Hostname:      os.Hostname,
		Config:        d.config,
	}
	recoverableStart := &recovery.RecoverableRoutine{
		Routine:             commondiscovery.CommonDiscovery,
		RoutineArg:          scChs,
		ErrorCode:           commondiscovery.ErrorCode(),
		ExpectedMinDuration: commondiscovery.ExpectedMinDuration(),
		UsageLogger:         *usagemetrics.UsageLogger,
	}
	recoverableStart.StartRoutine(ctx)

	dwActivation := datawarehouseactivation.Service{Config: d.config, Client: wlmClient}
	recoverableStart = &recovery.RecoverableRoutine{
		Routine:             dwActivation.DataWarehouseActivationCheck,
		RoutineArg:          scChs,
		ErrorCode:           dwActivation.ErrorCode(),
		ExpectedMinDuration: dwActivation.ExpectedMinDuration(),
		UsageLogger:         *usagemetrics.UsageLogger,
	}
	recoverableStart.StartRoutine(ctx)

	// Add any additional services here.
	d.services = []Service{
		&oracle.Service{Config: d.config, CloudProps: d.cloudProps, CommonCh: oracleCh},
		&mysql.Service{Config: d.config, CloudProps: d.cloudProps, CommonCh: mySQLCh, WLMClient: wlmClient},
		&redis.Service{Config: d.config, CloudProps: d.cloudProps, CommonCh: redisCh, WLMClient: wlmClient, OSData: d.osData},
		&sqlserver.Service{Config: d.config, CloudProps: d.cloudProps, CommonCh: sqlserverCh},
	}
	for _, service := range d.services {
		log.Logger.Infof("Starting %s", service.String())
		recoverableStart := &recovery.RecoverableRoutine{
			Routine:             service.Start,
			ErrorCode:           service.ErrorCode(),
			ExpectedMinDuration: service.ExpectedMinDuration(),
			UsageLogger:         *usagemetrics.UsageLogger,
		}
		recoverableStart.StartRoutine(ctx)
	}

	// Log a RUNNING usage metric once a day.
	go usagemetrics.LogRunningDaily()
	d.waitForShutdown(shutdownch, cancel)
	return nil
}