diff --git a/conf/app.conf b/conf/app.conf index 9d5c9a8..0be776c 100644 --- a/conf/app.conf +++ b/conf/app.conf @@ -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 diff --git a/controllers/common.go b/controllers/common.go index c7316e4..6a6869c 100644 --- a/controllers/common.go +++ b/controllers/common.go @@ -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, diff --git a/controllers/task.go b/controllers/task.go index 958e824..24f2e5c 100644 --- a/controllers/task.go +++ b/controllers/task.go @@ -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) } diff --git a/jobs/job.go b/jobs/job.go index e28743e..f6db1a1 100644 --- a/jobs/job.go +++ b/jobs/job.go @@ -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{ "超时", "错误", @@ -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) } } diff --git a/libs/http.go b/libs/http.go new file mode 100644 index 0000000..b284db8 --- /dev/null +++ b/libs/http.go @@ -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 +} diff --git a/notify/sms.go b/notify/sms.go new file mode 100644 index 0000000..8969a1b --- /dev/null +++ b/notify/sms.go @@ -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 +} diff --git a/views/task/detail.html b/views/task/detail.html index 327fa1d..9362579 100644 --- a/views/task/detail.html +++ b/views/task/detail.html @@ -92,7 +92,7 @@