2017-03-10 09:24:06 +00:00
|
|
|
|
package models
|
|
|
|
|
|
|
|
|
|
import (
|
2017-04-02 02:38:49 +00:00
|
|
|
|
"time"
|
2017-03-10 09:24:06 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Protocol int8
|
|
|
|
|
|
2017-03-24 05:06:53 +00:00
|
|
|
|
type TaskType int8
|
|
|
|
|
|
2017-03-10 09:24:06 +00:00
|
|
|
|
const (
|
2017-04-02 02:38:49 +00:00
|
|
|
|
HTTP Protocol = 1
|
|
|
|
|
SSHCommand Protocol = 2
|
|
|
|
|
SSHScript Protocol = 3
|
2017-03-10 09:24:06 +00:00
|
|
|
|
)
|
|
|
|
|
|
2017-03-24 05:06:53 +00:00
|
|
|
|
const (
|
2017-04-02 02:38:49 +00:00
|
|
|
|
Timing TaskType = 1
|
|
|
|
|
Delay TaskType = 2
|
2017-03-24 05:06:53 +00:00
|
|
|
|
)
|
|
|
|
|
|
2017-03-23 05:31:16 +00:00
|
|
|
|
// 任务
|
2017-03-10 09:24:06 +00:00
|
|
|
|
type Task struct {
|
2017-04-02 02:38:49 +00:00
|
|
|
|
Id int `xorm:"int pk autoincr"`
|
|
|
|
|
Name string `xorm:"varchar(64) notnull"` // 任务名称
|
|
|
|
|
Spec string `xorm:"varchar(64) notnull"` // crontab 时间格式
|
|
|
|
|
Protocol Protocol `xorm:"tinyint notnull"` // 协议 1:http 2:ssh-command 3:ssh-script
|
|
|
|
|
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"` // 延时任务,延时时间(单位秒)
|
|
|
|
|
SshHosts string `xorm:"varchar(512) notnull defalut '' "` // SSH主机名, host id,逗号分隔
|
|
|
|
|
Remark string `xorm:"varchar(512) notnull default ''"` // 备注
|
|
|
|
|
Created time.Time `xorm:"datetime notnull created"` // 创建时间
|
|
|
|
|
Deleted time.Time `xorm:"datetime deleted"` // 删除时间
|
|
|
|
|
Status Status `xorm:"tinyint notnull default 1"` // 状态 1:正常 0:停止
|
|
|
|
|
Page int `xorm:"-"`
|
|
|
|
|
PageSize int `xorm:"-"`
|
2017-03-10 09:24:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 新增
|
2017-04-02 02:19:52 +00:00
|
|
|
|
func (task *Task) Create() (insertId int, err error) {
|
2017-04-02 02:38:49 +00:00
|
|
|
|
task.Status = Enabled
|
2017-03-10 09:24:06 +00:00
|
|
|
|
|
2017-04-02 02:38:49 +00:00
|
|
|
|
_, err = Db.Insert(task)
|
|
|
|
|
if err == nil {
|
|
|
|
|
insertId = task.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 (task *Task) Update(id int, data CommonMap) (int64, error) {
|
2017-04-02 02:38:49 +00:00
|
|
|
|
return Db.Table(task).ID(id).Update(data)
|
2017-03-10 09:24:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 删除
|
2017-04-02 02:19:52 +00:00
|
|
|
|
func (task *Task) Delete(id int) (int64, error) {
|
2017-04-02 02:38:49 +00:00
|
|
|
|
return Db.Id(id).Delete(task)
|
2017-03-10 09:24:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 禁用
|
2017-04-02 02:19:52 +00:00
|
|
|
|
func (task *Task) Disable(id int) (int64, error) {
|
2017-04-02 02:38:49 +00:00
|
|
|
|
return task.Update(id, CommonMap{"status": Disabled})
|
2017-03-10 09:24:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 激活
|
2017-04-02 02:19:52 +00:00
|
|
|
|
func (task *Task) Enable(id int) (int64, error) {
|
2017-04-02 02:38:49 +00:00
|
|
|
|
return task.Update(id, CommonMap{"status": Enabled})
|
2017-03-10 09:24:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-02 02:19:52 +00:00
|
|
|
|
func (task *Task) ActiveList() ([]Task, error) {
|
2017-04-02 02:38:49 +00:00
|
|
|
|
task.parsePageAndPageSize()
|
|
|
|
|
list := make([]Task, 0)
|
|
|
|
|
err := Db.Where("status = ?", Enabled).Desc("id").Find(&list)
|
2017-03-24 09:55:44 +00:00
|
|
|
|
|
2017-04-02 02:38:49 +00:00
|
|
|
|
return list, err
|
2017-03-24 09:55:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-02 02:19:52 +00:00
|
|
|
|
func (task *Task) List() ([]Task, error) {
|
2017-04-02 02:38:49 +00:00
|
|
|
|
task.parsePageAndPageSize()
|
|
|
|
|
list := make([]Task, 0)
|
|
|
|
|
err := Db.Desc("id").Limit(task.PageSize, task.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 (taskLog *TaskLog) Total() (int64, error) {
|
2017-04-02 02:38:49 +00:00
|
|
|
|
return Db.Count(taskLog)
|
2017-03-10 09:24:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-02 02:19:52 +00:00
|
|
|
|
func (taskLog *TaskLog) parsePageAndPageSize() {
|
2017-04-02 02:38:49 +00:00
|
|
|
|
if taskLog.Page <= 0 {
|
|
|
|
|
taskLog.Page = Page
|
|
|
|
|
}
|
|
|
|
|
if taskLog.PageSize >= 0 || taskLog.PageSize > MaxPageSize {
|
|
|
|
|
taskLog.PageSize = PageSize
|
|
|
|
|
}
|
2017-03-10 09:24:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-02 02:19:52 +00:00
|
|
|
|
func (taskLog *TaskLog) pageLimitOffset() int {
|
2017-04-02 02:38:49 +00:00
|
|
|
|
return (taskLog.Page - 1) * taskLog.PageSize
|
2017-04-02 02:19:52 +00:00
|
|
|
|
}
|