新增出错短信提醒

This commit is contained in:
george
2018-08-09 18:47:42 +08:00
parent 72ceb95845
commit 3febcce23d
8 changed files with 151 additions and 28 deletions

View File

@@ -33,5 +33,6 @@ email.pool = 10
# 其他通知方式
msg.url = http://xxx.com/sms/url?id=12&msg=12121
msg.url = http://chanxiyou.com/api/tools/send_sms
msg.pool = 10

View File

@@ -285,14 +285,14 @@ func serverListByGroupId(groupId int) []string {
return servers
}
type adminInfo struct {
type AdminInfo struct {
Id int
Email string
Phone string
RealName string
}
func AllAdminInfo(adminIds string) []*adminInfo {
func AllAdminInfo(adminIds string) []*AdminInfo {
Filters := make([]interface{}, 0)
Filters = append(Filters, "status", 1)
//Filters = append(Filters, "id__gt", 1)
@@ -307,9 +307,9 @@ func AllAdminInfo(adminIds string) []*adminInfo {
}
Result, _ := models.AdminGetList(1, 1000, Filters...)
adminInfos := make([]*adminInfo, 0)
adminInfos := make([]*AdminInfo, 0)
for _, v := range Result {
ai := adminInfo{
ai := AdminInfo{
Id: v.Id,
Email: v.Email,
Phone: v.Phone,

View File

@@ -166,8 +166,7 @@ func (self *TaskController) Detail() {
}
//是否出错通知
self.Data["adminInfo"] = []int{0}
fmt.Println(task.NotifyUserIds)
self.Data["adminInfo"] = []*AdminInfo{}
if task.NotifyUserIds != "0" && task.NotifyUserIds != "" {
self.Data["adminInfo"] = AllAdminInfo(task.NotifyUserIds)
}

View File

@@ -266,25 +266,13 @@ func (j *Job) Run() {
log.Status = models.TASK_ERROR
log.Error = err.Error() + ":" + cmdErr
}
fmt.Println()
fmt.Println()
fmt.Println(log.Status, j.task.IsNotify)
if log.Status < 0 && j.task.IsNotify == 1 {
fmt.Println()
fmt.Println()
fmt.Println(j.task.NotifyUserIds)
fmt.Println()
fmt.Println(j.task.NotifyUserIds != "0")
fmt.Println(j.task.NotifyUserIds != "")
if j.task.NotifyUserIds != "0" && j.task.NotifyUserIds != "" {
admin_info := AllAdminInfo(j.task.NotifyUserIds)
fmt.Println("ADMIN:", admin_info)
adminInfo := AllAdminInfo(j.task.NotifyUserIds)
phone := make([]string, 0)
toEmail := ""
for _, v := range admin_info {
for _, v := range adminInfo {
if v.Phone != "0" && v.Phone != "" {
phone = append(phone, v.Phone)
}
@@ -294,9 +282,6 @@ func (j *Job) Run() {
}
toEmail = strings.TrimRight(toEmail, ";")
fmt.Println("EMAIL:", toEmail)
fmt.Println("TYPE:", j.task.NotifyType)
TextStatus := []string{
"<font color='red'>超时</font>",
"<font color='red'>错误</font>",
@@ -342,9 +327,18 @@ func (j *Job) Run() {
fmt.Println("发送邮件错误", toEmail)
}
} else if j.task.NotifyType == 1 {
} else if j.task.NotifyType == 1 && len(phone) > 0 {
//信息
TextStatus := []string{
" 超时",
" 错误",
" 正常",
}
param := make(map[string]string)
param["task_id"] = " " + strconv.Itoa(j.task.Id)
param["task_name"] = " " + j.task.TaskName
param["status"] = " " + TextStatus[status]
notify.SendSmsToChan(phone, param)
}
}

58
libs/http.go Normal file
View File

@@ -0,0 +1,58 @@
/************************************************************
** @Description: libs
** @Author: george hao
** @Date: 2018-08-09 13:29
** @Last Modified by: george hao
** @Last Modified time: 2018-08-09 13:29
*************************************************************/
package libs
import (
"encoding/json"
"github.com/pkg/errors"
"io/ioutil"
"net/http"
"strings"
)
type AjaxReturn struct {
Status int `json:"status"`
Message string `json:"message"`
Data interface{} `json:"data"`
}
func HttpGet(url string, param map[string]string) error {
if url == "" {
return errors.Errorf("url %s is not exists", url)
}
paramStr := ""
for k, v := range param {
paramStr += k + "=" + v + "&"
}
paramStr = strings.TrimRight(paramStr, "&")
if paramStr != "" {
url += "?" + paramStr
}
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
ajaxData := AjaxReturn{}
json.Unmarshal(body, &ajaxData)
if ajaxData.Status != 200 {
return errors.Errorf("msg %s", ajaxData.Message)
}
return nil
}

71
notify/sms.go Normal file
View File

@@ -0,0 +1,71 @@
/************************************************************
** @Description: notify
** @Author: george hao
** @Date: 2018-08-09 13:05
** @Last Modified by: george hao
** @Last Modified time: 2018-08-09 13:05
*************************************************************/
package notify
import (
"github.com/astaxie/beego"
"github.com/george518/PPGo_Job/libs"
"log"
"time"
)
type Sms struct {
Mobiles []string
Param map[string]string
}
var SmsChan chan *Sms
var SmsUrl string
func init() {
SmsUrl = beego.AppConfig.String("msg.url")
poolSize, _ := beego.AppConfig.Int("msg.pool")
//创建通道
SmsChan = make(chan *Sms, poolSize)
go func() {
for {
select {
case m, ok := <-SmsChan:
if !ok {
return
}
if err := m.SendSms(); err != nil {
beego.Error("SendSms:", err.Error())
}
}
}
}()
}
func SendSmsToChan(mobiles []string, param map[string]string) bool {
sms := &Sms{
Mobiles: mobiles,
Param: param,
}
select {
case SmsChan <- sms:
return true
case <-time.After(time.Second * 3):
return false
}
}
func (s *Sms) SendSms() error {
for _, v := range s.Mobiles {
s.Param["mobile"] = v
err := libs.HttpGet(SmsUrl, s.Param)
if err != nil {
log.Println(err)
}
}
return nil
}

View File

@@ -92,7 +92,7 @@
<tr>
<td>出错通知</td>
<td>{{if eq .task.IsNotify 0}}{{end}} {{if eq .task.IsNotify 1}}{{end}}</td>
<td>{{if eq .task.IsNotify 0}}{{end}} {{if eq .task.IsNotify 1}}{{end}}</td>
<td></td>
</tr>

View File

@@ -42,7 +42,7 @@
<tr>
<td>输出大小</td>
<td>{{.taskLog.ouput_size}}</td>
<td>{{.taskLog.output_size}}</td>
<td></td>
</tr>
<tr>