2017-03-10 09:24:06 +00:00
|
|
|
|
package models
|
|
|
|
|
|
2017-03-14 06:31:46 +00:00
|
|
|
|
import (
|
2017-04-02 02:38:49 +00:00
|
|
|
|
"time"
|
2017-04-22 15:39:33 +00:00
|
|
|
|
"github.com/go-xorm/xorm"
|
2017-03-14 06:31:46 +00:00
|
|
|
|
)
|
2017-03-10 09:24:06 +00:00
|
|
|
|
|
2017-04-14 10:05:34 +00:00
|
|
|
|
type TaskType int8
|
|
|
|
|
|
|
|
|
|
|
2017-03-23 05:31:16 +00:00
|
|
|
|
// 任务执行日志
|
2017-04-02 02:19:52 +00:00
|
|
|
|
type TaskLog struct {
|
2017-04-13 09:35:59 +00:00
|
|
|
|
Id int64 `xorm:"bigint pk autoincr"`
|
2017-04-14 10:05:34 +00:00
|
|
|
|
TaskId int `xorm:"int notnull index default 0"` // 任务id
|
2017-04-07 01:13:36 +00:00
|
|
|
|
Name string `xorm:"varchar(64) notnull"` // 任务名称
|
|
|
|
|
Spec string `xorm:"varchar(64) notnull"` // crontab
|
2017-04-13 09:35:59 +00:00
|
|
|
|
Protocol TaskProtocol `xorm:"tinyint notnull"` // 协议 1:http 2:ssh-command
|
2017-04-07 01:13:36 +00:00
|
|
|
|
Command string `xorm:"varchar(512) notnull"` // URL地址或shell命令
|
|
|
|
|
Timeout int `xorm:"mediumint notnull default 0"` // 任务执行超时时间(单位秒),0不限制
|
2017-04-21 06:50:40 +00:00
|
|
|
|
RetryTimes int8 `xorm:"tinyint notnull default 0"` // 任务重试次数
|
2017-04-10 09:37:16 +00:00
|
|
|
|
Hostname string `xorm:"varchar(512) notnull defalut '' "` // SSH主机名,逗号分隔
|
2017-04-02 02:38:49 +00:00
|
|
|
|
StartTime time.Time `xorm:"datetime created"` // 开始执行时间
|
|
|
|
|
EndTime time.Time `xorm:"datetime updated"` // 执行完成(失败)时间
|
2017-04-22 15:39:33 +00:00
|
|
|
|
Status Status `xorm:"tinyint notnull default 1"` // 状态 0:执行失败 1:执行中 2:执行完毕
|
2017-04-02 02:38:49 +00:00
|
|
|
|
Result string `xorm:"varchar(65535) notnull defalut '' "` // 执行结果
|
|
|
|
|
Page int `xorm:"-"`
|
|
|
|
|
PageSize int `xorm:"-"`
|
2017-04-21 05:36:45 +00:00
|
|
|
|
TotalTime int `xorm:"-"` // 执行总时长
|
2017-04-02 02:19:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-13 09:35:59 +00:00
|
|
|
|
func (taskLog *TaskLog) Create() (insertId int64, err error) {
|
2017-04-02 02:38:49 +00:00
|
|
|
|
taskLog.Status = Running
|
2017-03-10 09:24:06 +00:00
|
|
|
|
|
2017-04-02 02:38:49 +00:00
|
|
|
|
_, err = Db.Insert(taskLog)
|
|
|
|
|
if err == nil {
|
|
|
|
|
insertId = taskLog.Id
|
|
|
|
|
}
|
2017-03-24 09:55:44 +00:00
|
|
|
|
|
2017-04-02 02:38:49 +00:00
|
|
|
|
return
|
2017-03-10 09:24:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 更新
|
2017-04-13 09:35:59 +00:00
|
|
|
|
func (taskLog *TaskLog) Update(id int64, data CommonMap) (int64, error) {
|
2017-04-02 02:38:49 +00:00
|
|
|
|
return Db.Table(taskLog).ID(id).Update(data)
|
2017-03-10 09:24:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-13 09:35:59 +00:00
|
|
|
|
func (taskLog *TaskLog) setStatus(id int64, status Status) (int64, error) {
|
2017-04-02 02:38:49 +00:00
|
|
|
|
return taskLog.Update(id, CommonMap{"status": status})
|
2017-03-10 09:24:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-22 15:39:33 +00:00
|
|
|
|
func (taskLog *TaskLog) List(params CommonMap) ([]TaskLog, error) {
|
2017-04-02 02:38:49 +00:00
|
|
|
|
taskLog.parsePageAndPageSize()
|
|
|
|
|
list := make([]TaskLog, 0)
|
2017-04-22 15:39:33 +00:00
|
|
|
|
session := Db.Desc("id")
|
|
|
|
|
taskLog.parseWhere(session, params)
|
|
|
|
|
err := session.Limit(taskLog.PageSize, taskLog.pageLimitOffset()).Find(&list)
|
2017-04-21 05:36:45 +00:00
|
|
|
|
if len(list) > 0 {
|
|
|
|
|
for i, item := range list {
|
|
|
|
|
endTime := item.EndTime
|
|
|
|
|
if item.Status == Running {
|
|
|
|
|
endTime = time.Now()
|
|
|
|
|
}
|
|
|
|
|
execSeconds := endTime.Sub(item.StartTime).Seconds()
|
|
|
|
|
list[i].TotalTime = int(execSeconds)
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-03-10 09:24:06 +00:00
|
|
|
|
|
2017-04-02 02:38:49 +00:00
|
|
|
|
return list, err
|
2017-03-10 09:24:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-13 09:35:59 +00:00
|
|
|
|
// 清空表
|
|
|
|
|
func (TaskLog *TaskLog) Clear() (int64, error) {
|
|
|
|
|
return Db.Where("1=1").Delete(TaskLog);
|
2017-03-10 09:24:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-13 09:35:59 +00:00
|
|
|
|
func (taskLog *TaskLog) Total() (int64, error) {
|
|
|
|
|
return Db.Count(taskLog)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (taskLog *TaskLog) parsePageAndPageSize() {
|
|
|
|
|
if taskLog.Page <= 0 {
|
|
|
|
|
taskLog.Page = Page
|
2017-04-02 02:38:49 +00:00
|
|
|
|
}
|
2017-04-13 09:35:59 +00:00
|
|
|
|
if taskLog.PageSize >= 0 || taskLog.PageSize > MaxPageSize {
|
|
|
|
|
taskLog.PageSize = PageSize
|
2017-04-02 02:38:49 +00:00
|
|
|
|
}
|
2017-03-10 09:24:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-13 09:35:59 +00:00
|
|
|
|
func (taskLog *TaskLog) pageLimitOffset() int {
|
|
|
|
|
return (taskLog.Page - 1) * taskLog.PageSize
|
2017-04-22 15:39:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 解析where
|
|
|
|
|
func (taskLog *TaskLog) parseWhere(session *xorm.Session, params CommonMap) {
|
|
|
|
|
if len(params) == 0 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
taskId, ok := params["TaskId"]
|
|
|
|
|
if ok && taskId.(int) > 0 {
|
|
|
|
|
session.And("task_id = ?", taskId)
|
|
|
|
|
}
|
|
|
|
|
protocol, ok := params["Protocol"]
|
|
|
|
|
if ok && protocol.(int) > 0 {
|
|
|
|
|
session.And("protocol = ?", protocol)
|
|
|
|
|
}
|
|
|
|
|
status, ok := params["Status"]
|
|
|
|
|
if ok && status.(int) > -1 {
|
|
|
|
|
session.And("status = ?", status)
|
|
|
|
|
}
|
2017-04-13 09:35:59 +00:00
|
|
|
|
}
|