2023-03-23 15:58:12 +08:00
|
|
|
package model
|
|
|
|
|
2023-04-24 15:14:04 +08:00
|
|
|
import (
|
|
|
|
"hpds_control_center/internal/proto"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2023-03-23 15:58:12 +08:00
|
|
|
type Task struct {
|
|
|
|
TaskId int64 `xorm:"not null pk autoincr INT(11)" json:"taskId"`
|
|
|
|
ModelId int64 `xorm:"INT(11) index" json:"modelId"`
|
|
|
|
NodeId int64 `xorm:"INT(11) index" json:"nodeId"`
|
|
|
|
TaskName string `xorm:"VARCHAR(200)" json:"taskName"`
|
|
|
|
TaskDesc string `xorm:"VARCHAR(500)" json:"taskDesc"`
|
|
|
|
DatasetArr string `xorm:"TEXT" json:"datasetArr"`
|
|
|
|
ResultStorage string `xorm:"TEXT" json:"resultStorage"`
|
|
|
|
AppointmentTime string `xorm:"VARCHAR(30)" json:"appointmentTime"`
|
|
|
|
StartTime int64 `xorm:"BIGINT" json:"startTime"`
|
|
|
|
FinishTime int64 `xorm:"BIGINT" json:"finishTime"`
|
2023-04-24 15:14:04 +08:00
|
|
|
TotalCount int64 `xorm:"INT" json:"totalCount"`
|
|
|
|
FailingCount int64 `xorm:"INT" json:"failingCount"`
|
|
|
|
CompletedCount int64 `xorm:"INT" json:"completedCount"`
|
|
|
|
UnfinishedCount int64 `xorm:"INT" json:"unfinishedCount"`
|
2023-03-23 15:58:12 +08:00
|
|
|
Status int `xorm:"not null SMALLINT default 0" json:"status"` // 1:等待执行; 2:执行中; 3:执行完成; 4:任务分配失败; 5:任务执行失败
|
|
|
|
CreateAt int64 `xorm:"created" json:"createAt"`
|
|
|
|
UpdateAt int64 `xorm:"updated" json:"updateAt"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func UpdateTaskExecuteNode(id, nodeId int64) {
|
|
|
|
item := new(Task)
|
|
|
|
item.TaskId = id
|
|
|
|
item.NodeId = nodeId
|
|
|
|
_, _ = DB.ID(id).Cols("node_id").Update(item)
|
|
|
|
}
|
2023-04-24 15:14:04 +08:00
|
|
|
|
|
|
|
func UpdateTaskProgress(taskProgress *proto.TaskLogProgress) {
|
|
|
|
task := new(Task)
|
|
|
|
h, err := DB.ID(taskProgress.TaskId).Get(task)
|
|
|
|
if err != nil || !h {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if taskProgress.CompletedCount > task.CompletedCount {
|
|
|
|
task.CompletedCount = taskProgress.CompletedCount
|
|
|
|
}
|
|
|
|
if taskProgress.FailingCount > task.FailingCount {
|
|
|
|
task.FailingCount = taskProgress.FailingCount
|
|
|
|
}
|
|
|
|
if taskProgress.TotalCount != task.TotalCount && taskProgress.TotalCount > 0 {
|
|
|
|
task.TotalCount = taskProgress.TotalCount
|
|
|
|
}
|
|
|
|
if taskProgress.UnfinishedCount != task.UnfinishedCount && taskProgress.UnfinishedCount >= 0 {
|
|
|
|
task.UnfinishedCount = taskProgress.UnfinishedCount
|
|
|
|
}
|
|
|
|
if task.CompletedCount+task.FailingCount >= task.TotalCount {
|
|
|
|
task.Status = 3
|
|
|
|
} else {
|
|
|
|
task.Status = 2
|
|
|
|
}
|
|
|
|
task.UpdateAt = time.Now().Unix()
|
|
|
|
_, err = DB.ID(taskProgress.TaskId).Cols("completed_count", "failing_count", "total_count", "unfinished_count", "update_at").Update(task)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-17 09:39:13 +08:00
|
|
|
func UpdateTaskProgressByLog(res *TaskResult, isFailing bool) (int, int) {
|
2023-04-24 15:14:04 +08:00
|
|
|
item := new(Task)
|
|
|
|
h, err := DB.ID(res.TaskId).Get(item)
|
|
|
|
if err != nil || !h {
|
2023-06-17 09:39:13 +08:00
|
|
|
return 0, 0
|
2023-05-18 10:59:35 +08:00
|
|
|
}
|
|
|
|
if isFailing {
|
|
|
|
item.FailingCount += 1
|
|
|
|
} else {
|
|
|
|
item.CompletedCount += 1
|
2023-04-24 15:14:04 +08:00
|
|
|
}
|
|
|
|
item.UnfinishedCount -= 1
|
2023-05-18 10:59:35 +08:00
|
|
|
if item.CompletedCount+item.FailingCount >= item.TotalCount {
|
2023-04-24 15:14:04 +08:00
|
|
|
item.FinishTime = time.Now().Unix()
|
|
|
|
item.UnfinishedCount = 0
|
|
|
|
item.Status = 3
|
|
|
|
}
|
|
|
|
item.UpdateAt = time.Now().Unix()
|
2023-05-18 10:59:35 +08:00
|
|
|
_, _ = DB.ID(res.TaskId).Cols("completed_count", "failing_count", "total_count", "unfinished_count", "update_at", "finish_time", "status").Update(item)
|
2023-04-24 15:14:04 +08:00
|
|
|
if item.TotalCount > 0 {
|
2023-06-17 09:39:13 +08:00
|
|
|
return int(item.CompletedCount), int(item.UnfinishedCount)
|
2023-04-24 15:14:04 +08:00
|
|
|
}
|
2023-06-17 09:39:13 +08:00
|
|
|
return int(item.CompletedCount), int(item.UnfinishedCount)
|
2023-04-24 15:14:04 +08:00
|
|
|
}
|