编辑任务后, 自动重新加载

pull/21/merge
ouqiang 2017-05-27 21:42:18 +08:00
parent 7e68ed9b60
commit 499bf561d9
7 changed files with 40 additions and 23 deletions

View File

@ -3,6 +3,7 @@ package models
import ( import (
"time" "time"
"github.com/go-xorm/xorm" "github.com/go-xorm/xorm"
"errors"
) )
type TaskProtocol int8 type TaskProtocol int8
@ -126,6 +127,18 @@ func (task *Task) NameExist(name string, id int) (bool, error) {
return count > 0, err return count > 0, err
} }
func (task *Task) GetStatus(id int) (Status, error) {
exist, err := Db.Id(id).Get(task)
if err != nil {
return 0, err
}
if !exist {
return 0, errors.New("not exist")
}
return task.Status, nil
}
func(task *Task) Detail(id int) (TaskHost, error) { func(task *Task) Detail(id int) (TaskHost, error) {
taskHost := TaskHost{} taskHost := TaskHost{}
fields := "t.*, host.alias,host.name,host.port" fields := "t.*, host.alias,host.name,host.port"

View File

@ -55,7 +55,7 @@ func WritePid() {
pidStr := strconv.Itoa(pid) pidStr := strconv.Itoa(pid)
err := ioutil.WriteFile(PidFile, []byte(pidStr), 0644) err := ioutil.WriteFile(PidFile, []byte(pidStr), 0644)
if err != nil { if err != nil {
logger.Fatalf("写入pid文件失败", err) logger.Fatal("写入pid文件失败", err)
} }
} }

View File

@ -21,7 +21,8 @@ func Exec(ip string, port int, taskReq *pb.TaskRequest) (string, error) {
taskReq.Timeout = 86400 taskReq.Timeout = 86400
} }
timeout := time.Duration(taskReq.Timeout) * time.Second timeout := time.Duration(taskReq.Timeout) * time.Second
ctx, _ := context.WithTimeout(context.Background(), timeout) ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
resp, err := c.Run(ctx, taskReq) resp, err := c.Run(ctx, taskReq)
if err != nil { if err != nil {
return "", err return "", err

View File

@ -32,7 +32,4 @@ func ExecShell(ctx context.Context, command string) (string, error) {
case result := <- resultChan: case result := <- resultChan:
return result.output, result.err return result.output, result.err
} }
return "", nil
} }

View File

@ -27,8 +27,8 @@ type TaskForm struct {
RetryTimes int8 RetryTimes int8
HostId int16 HostId int16
Remark string Remark string
NotifyStatus int8 `binding:In(1,2,3)` NotifyStatus int8 `binding:"In(1,2,3)"`
NotifyType int8 `binding:In(1,2)` NotifyType int8 `binding:"In(1,2)"`
NotifyReceiverId string NotifyReceiverId string
} }
@ -153,14 +153,22 @@ func Store(ctx *macaron.Context, form TaskForm) string {
if id == 0 { if id == 0 {
// 任务添加后开始调度执行
taskModel.Status = models.Running
id, err = taskModel.Create() id, err = taskModel.Create()
} else { } else {
_, err = taskModel.UpdateBean(id) _, err = taskModel.UpdateBean(id)
} }
if err != nil { if err != nil {
return json.CommonFailure("保存失败", err) return json.CommonFailure("保存失败", err)
} }
status, err := taskModel.GetStatus(id)
if status == models.Enabled {
addTaskToTimer(id)
}
return json.Success("保存成功", nil) return json.Success("保存成功", nil)
} }
@ -237,7 +245,7 @@ func addTaskToTimer(id int) {
} }
taskService := service.Task{} taskService := service.Task{}
taskService.Add(task) taskService.Add(&task)
} }
// 解析查询参数 // 解析查询参数

View File

@ -101,12 +101,12 @@ func (task *Task) Initialize() {
// 批量添加任务 // 批量添加任务
func (task *Task) BatchAdd(tasks []models.TaskHost) { func (task *Task) BatchAdd(tasks []models.TaskHost) {
for _, item := range tasks { for _, item := range tasks {
task.Add(item) task.Add(&item)
} }
} }
// 添加任务 // 添加任务
func (task *Task) Add(taskModel models.TaskHost) { func (task *Task) Add(taskModel *models.TaskHost) {
taskFunc := createJob(taskModel) taskFunc := createJob(taskModel)
if taskFunc == nil { if taskFunc == nil {
logger.Error("创建任务处理Job失败,不支持的任务协议#", taskModel.Protocol) logger.Error("创建任务处理Job失败,不支持的任务协议#", taskModel.Protocol)
@ -129,11 +129,11 @@ func (task *Task) StopAll() {
// 直接运行任务 // 直接运行任务
func (task *Task) Run(taskModel models.TaskHost) { func (task *Task) Run(taskModel models.TaskHost) {
go createJob(taskModel)() go createJob(&taskModel)()
} }
type Handler interface { type Handler interface {
Run(taskModel models.TaskHost) (string, error) Run(taskModel *models.TaskHost) (string, error)
} }
@ -143,7 +143,7 @@ type HTTPHandler struct{}
// http任务执行时间不超过300秒 // http任务执行时间不超过300秒
const HttpExecTimeout = 300 const HttpExecTimeout = 300
func (h *HTTPHandler) Run(taskModel models.TaskHost) (result string, err error) { func (h *HTTPHandler) Run(taskModel *models.TaskHost) (result string, err error) {
if taskModel.Timeout <= 0 || taskModel.Timeout > HttpExecTimeout { if taskModel.Timeout <= 0 || taskModel.Timeout > HttpExecTimeout {
taskModel.Timeout = HttpExecTimeout taskModel.Timeout = HttpExecTimeout
} }
@ -159,7 +159,7 @@ func (h *HTTPHandler) Run(taskModel models.TaskHost) (result string, err error)
// RPC调用执行任务 // RPC调用执行任务
type RPCHandler struct {} type RPCHandler struct {}
func (h *RPCHandler) Run(taskModel models.TaskHost) (result string, err error) { func (h *RPCHandler) Run(taskModel *models.TaskHost) (result string, err error) {
taskRequest := new(pb.TaskRequest) taskRequest := new(pb.TaskRequest)
taskRequest.Timeout = int32(taskModel.Timeout) taskRequest.Timeout = int32(taskModel.Timeout)
taskRequest.Command = taskModel.Command taskRequest.Command = taskModel.Command
@ -169,7 +169,7 @@ func (h *RPCHandler) Run(taskModel models.TaskHost) (result string, err error)
// 创建任务日志 // 创建任务日志
func createTaskLog(taskModel models.TaskHost, status models.Status) (int64, error) { func createTaskLog(taskModel *models.TaskHost, status models.Status) (int64, error) {
taskLogModel := new(models.TaskLog) taskLogModel := new(models.TaskLog)
taskLogModel.TaskId = taskModel.Id taskLogModel.TaskId = taskModel.Id
taskLogModel.Name = taskModel.Task.Name taskLogModel.Name = taskModel.Task.Name
@ -205,7 +205,7 @@ func updateTaskLog(taskLogId int64, taskResult TaskResult) (int64, error) {
} }
func createJob(taskModel models.TaskHost) cron.FuncJob { func createJob(taskModel *models.TaskHost) cron.FuncJob {
var handler Handler = createHandler(taskModel) var handler Handler = createHandler(taskModel)
if handler == nil { if handler == nil {
return nil return nil
@ -226,7 +226,7 @@ func createJob(taskModel models.TaskHost) cron.FuncJob {
return taskFunc return taskFunc
} }
func createHandler(taskModel models.TaskHost) Handler { func createHandler(taskModel *models.TaskHost) Handler {
var handler Handler = nil var handler Handler = nil
switch taskModel.Protocol { switch taskModel.Protocol {
case models.TaskHTTP: case models.TaskHTTP:
@ -238,7 +238,7 @@ func createHandler(taskModel models.TaskHost) Handler {
return handler; return handler;
} }
func beforeExecJob(taskModel models.TaskHost) (taskLogId int64) { func beforeExecJob(taskModel *models.TaskHost) (taskLogId int64) {
if taskModel.Multi == 0 && runInstance.has(taskModel.Id) { if taskModel.Multi == 0 && runInstance.has(taskModel.Id) {
createTaskLog(taskModel, models.Cancel) createTaskLog(taskModel, models.Cancel)
return return
@ -257,7 +257,7 @@ func beforeExecJob(taskModel models.TaskHost) (taskLogId int64) {
return taskLogId return taskLogId
} }
func afterExecJob(taskModel models.TaskHost, taskResult TaskResult, taskLogId int64) { func afterExecJob(taskModel *models.TaskHost, taskResult TaskResult, taskLogId int64) {
if taskResult.Err != nil { if taskResult.Err != nil {
taskResult.Result = taskResult.Err.Error() + "\n" + taskResult.Result taskResult.Result = taskResult.Err.Error() + "\n" + taskResult.Result
} }
@ -270,7 +270,7 @@ func afterExecJob(taskModel models.TaskHost, taskResult TaskResult, taskLogId in
} }
// 发送任务结果通知 // 发送任务结果通知
func SendNotification(taskModel models.TaskHost, taskResult TaskResult) { func SendNotification(taskModel *models.TaskHost, taskResult TaskResult) {
var statusName string var statusName string
// 未开启通知 // 未开启通知
if taskModel.NotifyStatus == 0 { if taskModel.NotifyStatus == 0 {
@ -301,7 +301,7 @@ func SendNotification(taskModel models.TaskHost, taskResult TaskResult) {
} }
// 执行具体任务 // 执行具体任务
func execJob(handler Handler, taskModel models.TaskHost) TaskResult { func execJob(handler Handler, taskModel *models.TaskHost) TaskResult {
if taskModel.Multi == 0 { if taskModel.Multi == 0 {
defer runInstance.done(taskModel.Id) defer runInstance.done(taskModel.Id)
} }

View File

@ -55,7 +55,6 @@
<th></th> <th></th>
<th>cron</th> <th>cron</th>
<th></th> <th></th>
<th></th>
<th></th> <th></th>
<th></th> <th></th>
<th></th> <th></th>
@ -70,7 +69,6 @@
<td>{{{.Name}}}</td> <td>{{{.Name}}}</td>
<td>{{{.Spec}}}</td> <td>{{{.Spec}}}</td>
<td>{{{if eq .Protocol 1}}} HTTP {{{else if eq .Protocol 2}}} RPC {{{end}}}</td> <td>{{{if eq .Protocol 1}}} HTTP {{{else if eq .Protocol 2}}} RPC {{{end}}}</td>
<td>{{{if eq .Timeout -1}}}{{{else if gt .Timeout 0}}}{{{.Timeout}}}{{{else}}}{{{end}}}</td>
<td>{{{.RetryTimes}}}</td> <td>{{{.RetryTimes}}}</td>
<td>{{{.Hostname}}}</td> <td>{{{.Hostname}}}</td>
<td> <td>