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

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 (
"time"
"github.com/go-xorm/xorm"
"errors"
)
type TaskProtocol int8
@ -126,6 +127,18 @@ func (task *Task) NameExist(name string, id int) (bool, error) {
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) {
taskHost := TaskHost{}
fields := "t.*, host.alias,host.name,host.port"

View File

@ -55,7 +55,7 @@ func WritePid() {
pidStr := strconv.Itoa(pid)
err := ioutil.WriteFile(PidFile, []byte(pidStr), 0644)
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
}
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)
if err != nil {
return "", err

View File

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

View File

@ -27,8 +27,8 @@ type TaskForm struct {
RetryTimes int8
HostId int16
Remark string
NotifyStatus int8 `binding:In(1,2,3)`
NotifyType int8 `binding:In(1,2)`
NotifyStatus int8 `binding:"In(1,2,3)"`
NotifyType int8 `binding:"In(1,2)"`
NotifyReceiverId string
}
@ -153,14 +153,22 @@ func Store(ctx *macaron.Context, form TaskForm) string {
if id == 0 {
// 任务添加后开始调度执行
taskModel.Status = models.Running
id, err = taskModel.Create()
} else {
_, err = taskModel.UpdateBean(id)
}
if err != nil {
return json.CommonFailure("保存失败", err)
}
status, err := taskModel.GetStatus(id)
if status == models.Enabled {
addTaskToTimer(id)
}
return json.Success("保存成功", nil)
}
@ -237,7 +245,7 @@ func addTaskToTimer(id int) {
}
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) {
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)
if taskFunc == nil {
logger.Error("创建任务处理Job失败,不支持的任务协议#", taskModel.Protocol)
@ -129,11 +129,11 @@ func (task *Task) StopAll() {
// 直接运行任务
func (task *Task) Run(taskModel models.TaskHost) {
go createJob(taskModel)()
go createJob(&taskModel)()
}
type Handler interface {
Run(taskModel models.TaskHost) (string, error)
Run(taskModel *models.TaskHost) (string, error)
}
@ -143,7 +143,7 @@ type HTTPHandler struct{}
// http任务执行时间不超过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 {
taskModel.Timeout = HttpExecTimeout
}
@ -159,7 +159,7 @@ func (h *HTTPHandler) Run(taskModel models.TaskHost) (result string, err error)
// RPC调用执行任务
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.Timeout = int32(taskModel.Timeout)
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.TaskId = taskModel.Id
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)
if handler == nil {
return nil
@ -226,7 +226,7 @@ func createJob(taskModel models.TaskHost) cron.FuncJob {
return taskFunc
}
func createHandler(taskModel models.TaskHost) Handler {
func createHandler(taskModel *models.TaskHost) Handler {
var handler Handler = nil
switch taskModel.Protocol {
case models.TaskHTTP:
@ -238,7 +238,7 @@ func createHandler(taskModel models.TaskHost) 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) {
createTaskLog(taskModel, models.Cancel)
return
@ -257,7 +257,7 @@ func beforeExecJob(taskModel models.TaskHost) (taskLogId int64) {
return taskLogId
}
func afterExecJob(taskModel models.TaskHost, taskResult TaskResult, taskLogId int64) {
func afterExecJob(taskModel *models.TaskHost, taskResult TaskResult, taskLogId int64) {
if taskResult.Err != nil {
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
// 未开启通知
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 {
defer runInstance.done(taskModel.Id)
}

View File

@ -55,7 +55,6 @@
<th></th>
<th>cron</th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
@ -70,7 +69,6 @@
<td>{{{.Name}}}</td>
<td>{{{.Spec}}}</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>{{{.Hostname}}}</td>
<td>