gocron/modules/notify/notify.go

52 lines
1.3 KiB
Go
Raw Normal View History

2017-04-28 03:54:46 +00:00
package notify
import (
"time"
2017-04-30 17:12:07 +00:00
"github.com/ouqiang/gocron/modules/logger"
"fmt"
2017-04-28 03:54:46 +00:00
)
type Message map[string]interface{}
2017-04-28 06:55:24 +00:00
2017-04-28 03:54:46 +00:00
type Notifiable interface {
2017-04-28 06:55:24 +00:00
Send(msg Message)
2017-04-28 03:54:46 +00:00
}
var queue chan Message = make(chan Message, 100)
func init() {
go run()
}
// 把消息推入队列
func Push(msg Message) {
queue <- msg
}
func run() {
for msg := range queue {
2017-04-28 06:55:24 +00:00
// 根据任务配置发送通知
2017-04-30 17:12:07 +00:00
taskType, taskTypeOk := msg["task_type"]
_, taskReceiverIdOk := msg["task_receiver_id"]
_, nameOk := msg["name"]
_, outputOk := msg["output"]
_, statusOk := msg["status"]
if !taskTypeOk || !taskReceiverIdOk || !nameOk || !outputOk || !statusOk {
logger.Errorf("#notify#参数不完整#%+v", msg)
continue
}
msg["content"] = fmt.Sprintf("============\n============\n============\n任务名称: %s\n状态: %s\n输出:\n %s\n", msg["name"], msg["status"], msg["output"])
logger.Debugf("%+v", msg)
switch(taskType.(int8)) {
case 1:
// 邮件
mail := Mail{}
go mail.Send(msg)
case 2:
// Slack
slack := Slack{}
go slack.Send(msg)
}
2017-04-28 03:54:46 +00:00
time.Sleep(1 * time.Second)
}
}