gocron/routers/task/task.go

180 lines
4.7 KiB
Go
Raw Normal View History

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"
"github.com/jakecoffman/cron"
2017-04-07 09:26:46 +00:00
)
2017-04-07 01:13:36 +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-17 10:04:30 +00:00
ctx.Data["Title"] = "添加任务"
ctx.Data["Hosts"] = hosts
if len(hosts) > 0 {
ctx.Data["FirstHostName"] = hosts[0].Name
ctx.Data["FirstHostId"] = hosts[0].Id
}
ctx.HTML(200, "task/task_form")
}
func Edit(ctx *macaron.Context) {
id := ctx.ParamsInt(":id")
hostModel := new(models.Host)
hosts, err := hostModel.List()
if err != nil || len(hosts) == 0 {
logger.Error(err)
}
taskModel := new(models.Task)
task, err := taskModel.Detail(id)
2017-04-20 01:36:42 +00:00
if err != nil || task.Id != id {
2017-04-17 10:04:30 +00:00
logger.Errorf("编辑任务#获取任务详情失败#任务ID-%d#%s", id, err.Error())
ctx.Redirect("/task")
}
ctx.Data["Task"] = task
ctx.Data["Title"] = "编辑"
2017-04-07 09:26:46 +00:00
ctx.Data["Hosts"] = hosts
if len(hosts) > 0 {
ctx.Data["FirstHostName"] = hosts[0].Name
ctx.Data["FirstHostId"] = hosts[0].Id
}
2017-04-16 08:24:09 +00:00
ctx.HTML(200, "task/task_form")
2017-04-07 01:13:36 +00:00
}
2017-04-07 09:26:46 +00:00
type TaskForm struct {
2017-04-17 10:04:30 +00:00
Id int
2017-04-13 09:35:59 +00:00
Name string `binding:"Required;"`
Spec string `binding:"Required;MaxSize(64)"`
2017-04-16 18:01:41 +00:00
Protocol models.TaskProtocol `binding:"In(1,2,3)"`
2017-04-13 09:35:59 +00:00
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-17 10:04:30 +00:00
Status models.Status `binding:"In(1,2)"`
2017-04-07 09:26:46 +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-17 10:04:30 +00:00
var id int = form.Id
_, err := cron.Parse(form.Spec)
if err != nil {
return json.CommonFailure("crontab表达式解析失败", err)
}
2017-04-17 10:04:30 +00:00
nameExists, err := taskModel.NameExist(form.Name, form.Id)
2017-04-13 09:35:59 +00:00
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
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
2017-04-17 10:04:30 +00:00
if taskModel.Status != models.Enabled {
taskModel.Status = models.Disabled
}
2017-04-13 09:35:59 +00:00
taskModel.Spec = form.Spec
2017-04-17 10:04:30 +00:00
if id == 0 {
id, err = taskModel.Create()
} else {
_, err = taskModel.UpdateBean(id)
2017-04-17 10:04:30 +00:00
}
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) {
2017-04-17 10:04:30 +00:00
addTaskToTimer(id)
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-13 09:35:59 +00:00
func Remove(ctx *macaron.Context) string {
2017-04-16 08:24:09 +00:00
id := ctx.ParamsInt(":id")
2017-04-13 09:35:59 +00:00
json := utils.JsonResponse{}
taskModel := new(models.Task)
2017-04-16 08:24:09 +00:00
_, err := taskModel.Delete(id)
2017-04-13 09:35:59 +00:00
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 {
2017-04-16 08:24:09 +00:00
id := ctx.ParamsInt(":id")
2017-04-13 09:35:59 +00:00
json := utils.JsonResponse{}
taskModel := new(models.Task)
2017-04-16 08:24:09 +00:00
_, err := taskModel.Update(id, models.CommonMap{
2017-04-13 09:35:59 +00:00
"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-13 09:35:59 +00:00
taskService := service.Task{}
taskService.Add(task)
}