提交 f0655f95 编写于 作者: aaronchen2k2k's avatar aaronchen2k2k

close task#8649

上级 ffed4b60
......@@ -20,6 +20,7 @@ require (
github.com/mattn/go-runewidth v0.0.9
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/rifflock/lfshook v0.0.0-20180920164130-b9218ef580f5
github.com/robfig/cron/v3 v3.0.1
github.com/satori/go.uuid v1.2.0
github.com/sirupsen/logrus v1.6.0
github.com/smartystreets/assertions v1.1.0 // indirect
......
package model
package serverModel
import (
"time"
......
package serverModel
type SysInfo struct {
SysArch string `json:"sysArch"`
SysCores int `json:"sysCores"`
OsType string `json:"osType"`
OsName interface{} `json:"osName"`
OsVersion interface{} `json:"osVersion"`
Local string `json:"local"`
Lang string `json:"lang"`
}
......@@ -3,7 +3,7 @@ package server
import (
"encoding/json"
"fmt"
"github.com/easysoft/zentaoatf/src/server/model"
serverModel "github.com/easysoft/zentaoatf/src/server/model"
"github.com/easysoft/zentaoatf/src/server/service"
serverUtils "github.com/easysoft/zentaoatf/src/server/utils"
"github.com/easysoft/zentaoatf/src/utils/vari"
......@@ -13,13 +13,18 @@ import (
)
type Server struct {
agentService *service.AgentService
commonService *service.CommonService
agentService *service.AgentService
cronService *service.CronService
}
func NewServer() *Server {
commonService := service.NewCommonService()
agentService := service.NewAgentService()
cronService := service.NewCronService(commonService)
cronService.Init()
return &Server{agentService: agentService}
return &Server{commonService: commonService, agentService: agentService, cronService: cronService}
}
func (s *Server) Run() {
......@@ -40,7 +45,7 @@ func (s *Server) Handler() http.Handler {
}
func (s *Server) handle(writer http.ResponseWriter, req *http.Request) {
ret := model.ResData{Code: 1, Msg: "success"}
ret := serverModel.ResData{Code: 1, Msg: "success"}
var err error
serverUtils.SetupCORS(&writer, req)
......@@ -64,7 +69,7 @@ func (s *Server) handle(writer http.ResponseWriter, req *http.Request) {
io.WriteString(writer, string(bytes))
}
func (s *Server) get(req *http.Request) (resp model.ResData, err error) {
func (s *Server) get(req *http.Request) (resp serverModel.ResData, err error) {
method, _ := serverUtils.ParserGetParams(req)
switch method {
......@@ -87,13 +92,13 @@ func (s *Server) get(req *http.Request) (resp model.ResData, err error) {
return
}
func (s *Server) post(req *http.Request) (resp model.ResData, err error) {
func (s *Server) post(req *http.Request) (resp serverModel.ResData, err error) {
body, err := ioutil.ReadAll(req.Body)
if len(body) == 0 {
return
}
reqData := model.ReqData{}
reqData := serverModel.ReqData{}
err = serverUtils.ParserJsonReq(body, &reqData)
if err != nil {
return
......
package service
import (
commonUtils "github.com/easysoft/zentaoatf/src/server/utils/common"
"github.com/easysoft/zentaoatf/src/service/client"
zentaoService "github.com/easysoft/zentaoatf/src/service/zentao"
logUtils "github.com/easysoft/zentaoatf/src/utils/log"
"github.com/easysoft/zentaoatf/src/utils/vari"
zentaoUtils "github.com/easysoft/zentaoatf/src/utils/zentao"
)
type CommonService struct {
}
func NewCommonService() *CommonService {
return &CommonService{}
}
func (s *CommonService) HeartBeat() {
sysInfo := commonUtils.GetSysInfo()
// send request
zentaoService.GetConfig(vari.Config.Url)
url := vari.Config.Url + zentaoUtils.GenApiUri("agent", "heartBeat", "")
data := map[string]interface{}{"sys": sysInfo}
_, ok := client.PostObject(url, data, false)
if ok {
logUtils.PrintTo("heart beat success")
} else {
logUtils.PrintTo("heart beat fail")
}
return
}
package service
import (
"fmt"
serverConst "github.com/easysoft/zentaoatf/src/server/utils/const"
cronUtils "github.com/easysoft/zentaoatf/src/server/utils/cron"
)
type CronService struct {
commonService *CommonService
}
func NewCronService(commonService *CommonService) *CronService {
return &CronService{commonService: commonService}
}
func (s *CronService) Init() {
cronUtils.AddTaskFuc(
"HeartBeat",
fmt.Sprintf("@every %ds", serverConst.HeartBeatInterval),
func() {
s.commonService.HeartBeat()
},
)
}
package commonUtils
import (
serverModel "github.com/easysoft/zentaoatf/src/server/model"
"os"
"runtime"
"strings"
)
func GetSysInfo() (info serverModel.SysInfo) {
info.SysArch = runtime.GOARCH
info.SysCores = runtime.GOMAXPROCS(0)
info.OsType = runtime.GOOS
info.OsName, _ = os.Hostname()
envs := os.Environ()
for _, env := range envs {
if strings.Index(env, "LC_CTYPE=") > -1 { // LC_CTYPE=zh_CN.UTF-8
info.Lang = strings.Split(env, "=")[1]
}
}
return
}
package serverConst
const (
HeartBeatInterval = 5
)
package cronUtils
import (
"fmt"
"github.com/robfig/cron/v3"
)
var cronInstace *cron.Cron
var taskFunc = make(map[string]func())
func GetCrontabInstance() *cron.Cron {
if cronInstace != nil {
return cronInstace
}
cronInstace = cron.New()
cronInstace.Start()
return cronInstace
}
func AddTaskFuc(name string, schedule string, f func()) {
if _, ok := taskFunc[name]; !ok {
fmt.Println("Add a new task:", name)
cInstance := GetCrontabInstance()
cInstance.AddFunc(schedule, f)
taskFunc[name] = f
} else {
fmt.Println("Don't add same task `" + name + "` repeatedly!")
}
}
func Stop() {
cronInstace.Stop()
}
......@@ -4,7 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/easysoft/zentaoatf/src/server/model"
serverModel "github.com/easysoft/zentaoatf/src/server/model"
"io"
"log"
"net/http"
......@@ -23,16 +23,16 @@ func OutputErr(err error, writer http.ResponseWriter) {
WriteRes(errRes, writer)
}
func WriteRes(ret model.ResData, writer http.ResponseWriter) {
func WriteRes(ret serverModel.ResData, writer http.ResponseWriter) {
jsonStr, _ := json.Marshal(ret)
io.WriteString(writer, string(jsonStr))
}
func ErrRes(msg string) model.ResData {
return model.ResData{Code: 0, Msg: msg}
func ErrRes(msg string) serverModel.ResData {
return serverModel.ResData{Code: 0, Msg: msg}
}
func ParserJsonReq(bytes []byte, obj *model.ReqData) (err error) {
func ParserJsonReq(bytes []byte, obj *serverModel.ReqData) (err error) {
err = json.Unmarshal(bytes, &obj)
if err != nil {
log.Println(fmt.Sprintf("parse json error %s", err))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册