异步任务回调成功-发送通知

pull/21/merge
ouqiang 2017-05-10 17:58:05 +08:00
parent 70bccfaea0
commit f699a72c28
4 changed files with 44 additions and 4 deletions

View File

@ -72,6 +72,17 @@ func (taskLog *TaskLog) List(params CommonMap) ([]TaskLog, error) {
return list, err
}
// 根据通知ID获取任务ID
func (taskLog *TaskLog) GetTaskIdByNotifyId(notifyId string) (taskId int, err error) {
exist, err := Db.Where("notify_id = ?", notifyId).Get(taskLog)
if !exist || err != nil {
return
}
taskId = taskLog.TaskId
return
}
// 清空表
func (taskLog *TaskLog) Clear() (int64, error) {
return Db.Where("1=1").Delete(taskLog);

View File

@ -150,7 +150,9 @@ func RegisterMiddleware(m *macaron.Macaron) {
})
}
// 系统未安装,重定向到安装页面
// region 自定义中间件
/** 系统未安装,重定向到安装页面 **/
func checkAppInstall(m *macaron.Macaron) {
m.Use(func(ctx *macaron.Context) {
installUrl := "/install"
@ -196,7 +198,9 @@ func userAuth(ctx *macaron.Context, sess session.Store) {
}
}
// 设置共享数据
// endregion
/** 设置共享数据 **/
func setShareData(ctx *macaron.Context, sess session.Store) {
ctx.Data["URI"] = ctx.Req.URL.Path
urlPath := strings.TrimPrefix(ctx.Req.URL.Path, "/")

View File

@ -11,6 +11,8 @@ import (
"fmt"
"html/template"
"github.com/ouqiang/gocron/routers/base"
"github.com/ouqiang/gocron/service"
"errors"
)
func Index(ctx *macaron.Context) {
@ -86,6 +88,26 @@ func UpdateStatus(ctx *macaron.Context) string {
return json.CommonFailure("更新任务状态失败")
}
// 发送通知
taskId, err := taskLogModel.GetTaskIdByNotifyId(id)
if err != nil || taskId <= 0 {
logger.Error("异步任务回调#根据notify-id获取taskId失败", err)
return json.Success("success", nil)
}
taskModel := new(models.Task)
task, err := taskModel.Detail(taskId)
if err != nil || task.Id <= 0 {
logger.Error("异步任务回调#根据获取任务详情失败", err)
return json.Success("success", nil)
}
taskResult := service.TaskResult{}
taskResult.Result = result
if status == 0 {
taskResult.Err = errors.New("error")
}
service.SendNotification(task, taskResult)
return json.Success("success", nil)
}

View File

@ -346,11 +346,11 @@ func afterExecJob(taskModel models.TaskHost, taskResult TaskResult, taskLogId in
return
}
sendNotification(taskModel, taskResult)
SendNotification(taskModel, taskResult)
}
// 发送任务结果通知
func sendNotification(taskModel models.TaskHost, taskResult TaskResult) {
func SendNotification(taskModel models.TaskHost, taskResult TaskResult) {
var statusName string
// 未开启通知
if taskModel.NotifyStatus == 0 {
@ -360,6 +360,9 @@ func sendNotification(taskModel models.TaskHost, taskResult TaskResult) {
// 执行失败才发送通知
return
}
if taskModel.NotifyReceiverId == "" {
return
}
if taskResult.Err != nil {
statusName = "失败"
} else {