2017-04-07 01:13:36 +00:00
|
|
|
package task
|
|
|
|
|
2017-04-07 09:26:46 +00:00
|
|
|
import (
|
|
|
|
"gopkg.in/macaron.v1"
|
|
|
|
"github.com/ouqiang/gocron/models"
|
|
|
|
"github.com/ouqiang/gocron/modules/logger"
|
|
|
|
"github.com/ouqiang/gocron/modules/utils"
|
2017-04-13 09:35:59 +00:00
|
|
|
"github.com/ouqiang/gocron/service"
|
|
|
|
"strconv"
|
2017-04-07 09:26:46 +00:00
|
|
|
)
|
2017-04-07 01:13:36 +00:00
|
|
|
|
2017-04-08 09:15:30 +00:00
|
|
|
func Index(ctx *macaron.Context) {
|
|
|
|
taskModel := new(models.Task)
|
|
|
|
tasks, err := taskModel.List()
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(err)
|
|
|
|
}
|
|
|
|
ctx.Data["Title"] = "任务列表"
|
|
|
|
ctx.Data["Tasks"] = tasks
|
|
|
|
ctx.HTML(200, "task/index")
|
|
|
|
}
|
|
|
|
|
2017-04-07 01:13:36 +00:00
|
|
|
func Create(ctx *macaron.Context) {
|
2017-04-07 09:26:46 +00:00
|
|
|
hostModel := new(models.Host)
|
|
|
|
hosts, err := hostModel.List()
|
|
|
|
if err != nil || len(hosts) == 0 {
|
|
|
|
logger.Error(err)
|
|
|
|
}
|
2017-04-07 01:13:36 +00:00
|
|
|
ctx.Data["Title"] = "任务管理"
|
2017-04-07 09:26:46 +00:00
|
|
|
ctx.Data["Hosts"] = hosts
|
|
|
|
ctx.Data["FirstHostName"] = hosts[0].Name
|
|
|
|
ctx.Data["FirstHostId"] = hosts[0].Id
|
2017-04-07 01:13:36 +00:00
|
|
|
ctx.HTML(200, "task/create")
|
|
|
|
}
|
|
|
|
|
2017-04-07 09:26:46 +00:00
|
|
|
type TaskForm struct {
|
2017-04-13 09:35:59 +00:00
|
|
|
Name string `binding:"Required;"`
|
|
|
|
Spec string `binding:"Required;MaxSize(64)"`
|
|
|
|
Protocol models.TaskProtocol `binding:"In(1,2)"`
|
|
|
|
Command string `binding:"Required;MaxSize(512)"`
|
|
|
|
Timeout int `binding:"Range(0,86400)"`
|
|
|
|
HostId int16
|
2017-04-07 09:26:46 +00:00
|
|
|
Remark string
|
2017-04-13 09:35:59 +00:00
|
|
|
Status models.Status `binding:"In(1,0)"`
|
2017-04-07 09:26:46 +00:00
|
|
|
}
|
|
|
|
|
2017-04-08 09:15:30 +00:00
|
|
|
// 保存任务
|
2017-04-07 09:26:46 +00:00
|
|
|
func Store(ctx *macaron.Context, form TaskForm) string {
|
2017-04-13 09:35:59 +00:00
|
|
|
json := utils.JsonResponse{}
|
2017-04-07 09:26:46 +00:00
|
|
|
taskModel := models.Task{}
|
2017-04-13 09:35:59 +00:00
|
|
|
nameExists, err := taskModel.NameExist(form.Name)
|
|
|
|
if err != nil {
|
|
|
|
return json.CommonFailure(utils.FailureContent, err)
|
|
|
|
}
|
|
|
|
if nameExists {
|
|
|
|
return json.CommonFailure("任务名称已存在")
|
|
|
|
}
|
|
|
|
|
|
|
|
if form.Protocol == models.TaskSSH && form.HostId <= 0 {
|
|
|
|
return json.CommonFailure("请选择主机名")
|
|
|
|
}
|
|
|
|
|
2017-04-07 09:26:46 +00:00
|
|
|
taskModel.Name = form.Name
|
|
|
|
taskModel.Protocol = form.Protocol
|
|
|
|
taskModel.Command = form.Command
|
|
|
|
taskModel.Timeout = form.Timeout
|
2017-04-10 09:37:16 +00:00
|
|
|
taskModel.HostId = form.HostId
|
2017-04-07 09:26:46 +00:00
|
|
|
taskModel.Remark = form.Remark
|
2017-04-13 09:35:59 +00:00
|
|
|
taskModel.Status = form.Status
|
|
|
|
taskModel.Spec = form.Spec
|
|
|
|
insertId, err := taskModel.Create()
|
2017-04-07 09:26:46 +00:00
|
|
|
if err != nil {
|
2017-04-13 09:35:59 +00:00
|
|
|
return json.CommonFailure("保存失败", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 任务处于激活状态,加入调度管理
|
|
|
|
if (taskModel.Status == models.Enabled) {
|
|
|
|
addTaskToTimer(insertId)
|
2017-04-07 09:26:46 +00:00
|
|
|
}
|
2017-04-07 01:13:36 +00:00
|
|
|
|
2017-04-07 09:26:46 +00:00
|
|
|
return json.Success("保存成功", nil)
|
|
|
|
}
|
2017-04-08 09:15:30 +00:00
|
|
|
|
|
|
|
// 删除任务
|
2017-04-13 09:35:59 +00:00
|
|
|
func Remove(ctx *macaron.Context) string {
|
|
|
|
id, err := strconv.Atoi(ctx.Params(":id"))
|
|
|
|
json := utils.JsonResponse{}
|
|
|
|
if err != nil {
|
|
|
|
return json.CommonFailure("参数错误", err)
|
|
|
|
}
|
|
|
|
taskModel := new(models.Task)
|
|
|
|
_, err = taskModel.Delete(id)
|
|
|
|
if err != nil {
|
|
|
|
return json.CommonFailure(utils.FailureContent, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
service.Cron.RemoveJob(strconv.Itoa(id))
|
|
|
|
|
|
|
|
return json.Success(utils.SuccessContent, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 激活任务
|
|
|
|
func Enable(ctx *macaron.Context) string {
|
|
|
|
return changeStatus(ctx, models.Enabled)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 暂停任务
|
|
|
|
func Disable(ctx *macaron.Context) string {
|
|
|
|
return changeStatus(ctx, models.Disabled)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 改变任务状态
|
|
|
|
func changeStatus(ctx *macaron.Context, status models.Status) string {
|
|
|
|
id, err := strconv.Atoi(ctx.Params(":id"))
|
|
|
|
json := utils.JsonResponse{}
|
|
|
|
if err != nil {
|
|
|
|
return json.CommonFailure("参数错误", err)
|
|
|
|
}
|
|
|
|
taskModel := new(models.Task)
|
|
|
|
_, err = taskModel.Update(id, models.CommonMap{
|
|
|
|
"Status": status,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return json.CommonFailure(utils.FailureContent, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if status == models.Enabled {
|
|
|
|
addTaskToTimer(id)
|
|
|
|
} else {
|
|
|
|
service.Cron.RemoveJob(strconv.Itoa(id))
|
|
|
|
}
|
|
|
|
|
|
|
|
return json.Success(utils.SuccessContent, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 添加任务到定时器
|
|
|
|
func addTaskToTimer(id int) {
|
|
|
|
taskModel := new(models.Task)
|
|
|
|
task, err := taskModel.Detail(id)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(err)
|
|
|
|
return
|
|
|
|
}
|
2017-04-08 09:15:30 +00:00
|
|
|
|
2017-04-13 09:35:59 +00:00
|
|
|
taskService := service.Task{}
|
|
|
|
taskService.Add(task)
|
2017-04-08 09:15:30 +00:00
|
|
|
}
|