A server notify

This commit is contained in:
zhenorzz 2021-11-20 15:49:23 +08:00
parent 252d24467e
commit 278bb5dd6d
3 changed files with 287 additions and 0 deletions

203
model/ServerMonitorModel.go Normal file
View File

@ -0,0 +1,203 @@
package model
import (
"errors"
sq "github.com/Masterminds/squirrel"
)
const serverTable = "`server`"
// Server -
type Server struct {
ID int64 `json:"id"`
Name string `json:"name"`
IP string `json:"ip"`
Port int `json:"port"`
Owner string `json:"owner"`
Path string `json:"path"`
Password string `json:"password"`
NamespaceID int64 `json:"namespaceId"`
Description string `json:"description"`
OSInfo string `json:"osInfo"`
State int8 `json:"state"`
InsertTime string `json:"insertTime"`
UpdateTime string `json:"updateTime"`
}
// Servers -
type Servers []Server
// GetList -
func (s Server) GetList(pagination Pagination) (Servers, error) {
rows, err := sq.
Select("id, namespace_id, name, ip, port, owner, path, password, description, os_info, state, insert_time, update_time").
From(serverTable).
Where(sq.Eq{
"namespace_id": []int64{0, s.NamespaceID},
}).
Limit(pagination.Rows).
Offset((pagination.Page - 1) * pagination.Rows).
OrderBy("id DESC").
RunWith(DB).
Query()
if err != nil {
return nil, err
}
servers := Servers{}
for rows.Next() {
var server Server
if err := rows.Scan(
&server.ID,
&server.NamespaceID,
&server.Name,
&server.IP,
&server.Port,
&server.Owner,
&server.Path,
&server.Password,
&server.Description,
&server.OSInfo,
&server.State,
&server.InsertTime,
&server.UpdateTime); err != nil {
return nil, err
}
servers = append(servers, server)
}
return servers, nil
}
// GetTotal -
func (s Server) GetTotal() (int64, error) {
var total int64
err := sq.
Select("COUNT(*) AS count").
From(serverTable).
Where(sq.Eq{
"namespace_id": []int64{0, s.NamespaceID},
}).
RunWith(DB).
QueryRow().
Scan(&total)
if err != nil {
return 0, err
}
return total, nil
}
// GetAll -
func (s Server) GetAll() (Servers, error) {
rows, err := sq.
Select("id, name, ip, owner, description, insert_time, update_time").
From(serverTable).
Where(sq.Eq{
"namespace_id": []int64{0, s.NamespaceID},
"state": Enable,
}).
OrderBy("id DESC").
RunWith(DB).
Query()
if err != nil {
return nil, err
}
servers := Servers{}
for rows.Next() {
var server Server
if err := rows.Scan(&server.ID, &server.Name, &server.IP, &server.Owner, &server.Description, &server.InsertTime, &server.UpdateTime); err != nil {
return nil, err
}
servers = append(servers, server)
}
return servers, nil
}
// GetData -
func (s Server) GetData() (Server, error) {
var server Server
err := sq.
Select("id, namespace_id, name, ip, port, owner, path, password").
From(serverTable).
Where(sq.Eq{"id": s.ID}).
OrderBy("id DESC").
RunWith(DB).
QueryRow().
Scan(&server.ID, &server.NamespaceID, &server.Name, &server.IP, &server.Port, &server.Owner, &server.Path, &server.Password)
if err != nil {
return server, errors.New("数据查询失败")
}
return server, nil
}
// AddRow return LastInsertId
func (s Server) AddRow() (int64, error) {
result, err := sq.
Insert(serverTable).
Columns("namespace_id", "name", "ip", "port", "owner", "password", "path", "description", "os_info").
Values(s.NamespaceID, s.Name, s.IP, s.Port, s.Owner, s.Password, s.Path, s.Description, s.OSInfo).
RunWith(DB).
Exec()
if err != nil {
return 0, err
}
id, err := result.LastInsertId()
return id, err
}
// EditRow -
func (s Server) EditRow() error {
_, err := sq.
Update(serverTable).
SetMap(sq.Eq{
"namespace_id": s.NamespaceID,
"name": s.Name,
"ip": s.IP,
"port": s.Port,
"owner": s.Owner,
"password": s.Password,
"path": s.Path,
"description": s.Description,
"os_info": s.OSInfo,
}).
Where(sq.Eq{"id": s.ID}).
RunWith(DB).
Exec()
return err
}
// ToggleRow -
func (s Server) ToggleRow() error {
tx, err := DB.Begin()
if err != nil {
return errors.New("开启事务失败")
}
_, err = sq.
Update(serverTable).
SetMap(sq.Eq{
"state": s.State,
}).
Where(sq.Eq{"id": s.ID}).
RunWith(tx).
Exec()
if err != nil {
tx.Rollback()
return err
}
if s.State == Disable {
_, err = sq.
Delete(projectServerTable).
Where(sq.Eq{"server_id": s.ID}).
RunWith(tx).
Exec()
if err != nil {
tx.Rollback()
return err
}
}
if err = tx.Commit(); err != nil {
return errors.New("事务提交失败")
}
return nil
}

11
model/sql/1.3.8.sql Normal file
View File

@ -0,0 +1,11 @@
CREATE TABLE IF NOT EXISTS `server_agent_log` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`server_id` int(10) unsigned NOT NULL DEFAULT '0',
`type` tinyint(3) unsigned NOT NULL DEFAULT '0',
`item` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT '',
`value` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT '',
`report_time` datetime NOT NULL,
`insert_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`) USING BTREE,
KEY `idx_server_type_item_time` (`server_id`,`type`,`item`,`report_time`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

73
task/ServerMonitorTask.go Normal file
View File

@ -0,0 +1,73 @@
package task
import (
"database/sql"
"github.com/google/uuid"
"github.com/zhenorzz/goploy/core"
"github.com/zhenorzz/goploy/model"
"github.com/zhenorzz/goploy/service"
"sync"
"time"
)
func projectTask() {
date := time.Now().Format("2006-01-02 15:04:05")
projectTasks, err := model.ProjectTask{}.GetNotRunListLTDate(date)
if err != nil && err != sql.ErrNoRows {
core.Log(core.ERROR, "get project task list error, detail:"+err.Error())
}
wg := sync.WaitGroup{}
for _, projectTask := range projectTasks {
project, err := model.Project{ID: projectTask.ProjectID}.GetData()
if err != nil {
core.Log(core.ERROR, "publish task has no project, detail:"+err.Error())
continue
}
if project.DeployState == model.ProjectDeploying {
core.Log(core.ERROR, "The project in publish is being build by other")
continue
}
if err := projectTask.SetRun(); err != nil {
core.Log(core.ERROR, "publish task set run fail, detail:"+err.Error())
continue
}
projectServers, err := model.ProjectServer{ProjectID: projectTask.ProjectID}.GetBindServerListByProjectID()
if err != nil {
core.Log(core.ERROR, "publish task has no server, detail:"+err.Error())
continue
}
userInfo, err := model.User{ID: 1}.GetData()
if err != nil {
core.Log(core.ERROR, "publish task has no user, detail:"+err.Error())
continue
}
project.PublisherID = userInfo.ID
project.PublisherName = userInfo.Name
project.DeployState = model.ProjectDeploying
project.LastPublishToken = uuid.New().String()
err = project.Publish()
if err != nil {
core.Log(core.ERROR, "publish task change state error, detail:"+err.Error())
continue
}
wg.Add(1)
go func(projectTask model.ProjectTask) {
defer wg.Done()
service.Gsync{
UserInfo: userInfo,
Project: project,
ProjectServers: projectServers,
CommitID: projectTask.CommitID,
Branch: projectTask.Branch,
}.Exec()
}(projectTask)
}
wg.Wait()
}