gocron/models/task_log.go

70 lines
2.4 KiB
Go
Raw Normal View History

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-03-14 06:31:46 +00:00
)
2017-03-10 09:24:06 +00:00
2017-03-23 05:31:16 +00:00
// 任务执行日志
2017-04-02 02:19:52 +00:00
type TaskLog struct {
2017-04-02 02:38:49 +00:00
Id int `xorm:"int pk autoincr"`
2017-04-07 01:13:36 +00:00
Name string `xorm:"varchar(64) notnull"` // 任务名称
Spec string `xorm:"varchar(64) notnull"` // crontab
Protocol Protocol `xorm:"tinyint notnull"` // 协议 1:http 2:ssh-command
2017-04-07 01:13:36 +00:00
Type TaskType `xorm:"tinyint notnull default 1"` // 任务类型 1: 定时任务 2: 延时任务
Command string `xorm:"varchar(512) notnull"` // URL地址或shell命令
Timeout int `xorm:"mediumint notnull default 0"` // 任务执行超时时间(单位秒),0不限制
Delay int `xorm:"int notnull default 0"` // 延时任务,延时时间(单位秒)
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"` // 执行完成(失败)时间
Status Status `xorm:"tinyint notnull default 1"` // 状态 1:执行中 2:执行完毕 0:执行失败
Result string `xorm:"varchar(65535) notnull defalut '' "` // 执行结果
Page int `xorm:"-"`
PageSize int `xorm:"-"`
2017-04-02 02:19:52 +00:00
}
func (taskLog *TaskLog) Create() (insertId int, 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-02 02:19:52 +00:00
func (taskLog *TaskLog) Update(id int, 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-02 02:19:52 +00:00
func (taskLog *TaskLog) setStatus(id int, 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-02 02:19:52 +00:00
func (taskLog *TaskLog) List() ([]TaskLog, error) {
2017-04-02 02:38:49 +00:00
taskLog.parsePageAndPageSize()
list := make([]TaskLog, 0)
err := Db.Desc("id").Limit(taskLog.PageSize, taskLog.pageLimitOffset()).Find(&list)
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-02 02:19:52 +00:00
func (task *Task) Total() (int64, error) {
2017-04-02 02:38:49 +00:00
return Db.Count(task)
2017-03-10 09:24:06 +00:00
}
2017-04-02 02:19:52 +00:00
func (task *Task) parsePageAndPageSize() {
2017-04-02 02:38:49 +00:00
if task.Page <= 0 {
task.Page = Page
}
if task.PageSize >= 0 || task.PageSize > MaxPageSize {
task.PageSize = PageSize
}
2017-03-10 09:24:06 +00:00
}
2017-04-02 02:19:52 +00:00
func (task *Task) pageLimitOffset() int {
2017-04-02 02:38:49 +00:00
return (task.Page - 1) * task.PageSize
2017-04-02 02:19:52 +00:00
}