package main

import (
	"time"

	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/constant"

	"github.com/beego/beego/v2/server/web"
	serviceTask "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/node_task"
	serviceSummary "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/summary_evaluation/service"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/log"
	_ "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/port/beego"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/port/consumer"
)

func main() {
	startNodeTask()
	startSummaryEvaluation()
	go consumer.Run()
	web.Run()
}

// 定时任务-间隔发送评估环节
func startNodeTask() {
	go func() {
		nodeTaskService := serviceTask.NewNodeTaskService()

		var duration time.Duration
		if constant.Env == "prd" {
			duration = time.Minute * 5
		} else {
			duration = time.Minute * 1
		}
		timer := time.NewTimer(duration)
		for {
			<-timer.C
			if err := nodeTaskService.SendEvaluationNode(); err != nil {
				log.Logger.Error(err.Error())
			}
			timer.Reset(duration) // 重置定时
		}
	}()
}

// 定时任务-间隔检查时间,发送周期评估
func startSummaryEvaluation() {
	go func() {
		duration := 10 * time.Minute
		if constant.Env != "prd" {
			duration = 1 * time.Minute
		}
		timer := time.NewTimer(duration)
		for {
			<-timer.C
			err := serviceSummary.TaskSendSummaryEvaluation()
			if err != nil {
				log.Logger.Error(err.Error())
			}
			timer.Reset(duration) // 重置定时
		}
	}()
}