diff --git a/models/task_log.go b/models/task_log.go index ef8e635..5a988ef 100644 --- a/models/task_log.go +++ b/models/task_log.go @@ -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); diff --git a/routers/routers.go b/routers/routers.go index 5bb2956..426799b 100644 --- a/routers/routers.go +++ b/routers/routers.go @@ -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, "/") diff --git a/routers/tasklog/task_log.go b/routers/tasklog/task_log.go index 3efe460..b966035 100644 --- a/routers/tasklog/task_log.go +++ b/routers/tasklog/task_log.go @@ -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) } diff --git a/service/task.go b/service/task.go index 1c21e0e..cc6e2e7 100644 --- a/service/task.go +++ b/service/task.go @@ -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 {