4 Star 14 Fork 5

cloudtask/cloudtask-center

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
notify.go 2.85 KB
一键复制 编辑 原始数据 按行查看 历史
Rick.B.Liu 提交于 2018-02-02 15:28 +08:00 . modify mongo driver.
package notify
import (
"io/ioutil"
"strings"
"sync"
"time"
)
//notify template string
var (
LocationServersNotifyBody string
JobResultNotifyBody string
)
//NotifySender is exported
type NotifySender struct {
sync.RWMutex
initWatch bool
endPoints []IEndPoint
events map[string]*Event
}
func init() {
var (
buf []byte
err error
)
if buf, err = ioutil.ReadFile("./notify/templates/location.html"); err == nil {
LocationServersNotifyBody = string(buf)
}
if buf, err = ioutil.ReadFile("./notify/templates/job.html"); err == nil {
JobResultNotifyBody = string(buf)
}
}
//NewNotifySender is exported
func NewNotifySender(endPoints []EndPoint) *NotifySender {
sender := &NotifySender{
initWatch: true,
endPoints: []IEndPoint{},
events: make(map[string]*Event),
}
factory := &NotifyEndPointFactory{}
sender.Lock()
for _, endPoint := range endPoints {
switch strings.ToUpper(endPoint.Name) {
case "API":
apiEndPoint := factory.CreateAPIEndPoint(endPoint)
sender.endPoints = append(sender.endPoints, apiEndPoint)
case "SMTP":
smtpEndPoint := factory.CreateSMTPEndPoint(endPoint)
sender.endPoints = append(sender.endPoints, smtpEndPoint)
}
}
sender.Unlock()
go func() {
time.Sleep(60 * time.Second)
sender.initWatch = false
}()
return sender
}
//AddLocationServersEvent is exported
func (sender *NotifySender) AddLocationServersEvent(description string, watchLocation *WatchLocation) {
event := NewEvent(LocationServersEvent, description, nil, watchLocation.ContactInfo, sender.endPoints)
event.data["WatchLocation"] = watchLocation
sender.Lock()
sender.events[event.ID] = event
go sender.dispatchEvents()
sender.Unlock()
}
//AddJobNotifyEvent is exported
func (sender *NotifySender) AddJobNotifyEvent(description string, watchJobNotify *WatchJobNotify) {
event := NewEvent(JobNotifyEvent, description, nil, watchJobNotify.ContactInfo, sender.endPoints)
event.data["WatchJobNotify"] = watchJobNotify
sender.Lock()
sender.events[event.ID] = event
go sender.dispatchEvents()
sender.Unlock()
}
//dispatchEvents is exported
//dispatch all events.
func (sender *NotifySender) dispatchEvents() {
sender.Lock()
for {
if len(sender.events) == 0 {
break
}
if !sender.initWatch {
wgroup := sync.WaitGroup{}
for _, event := range sender.events {
if len(event.ContactInfo) > 0 {
wgroup.Add(1)
go func(e *Event) {
templateBody := getNotifyTemplateBody(e.Type)
if templateBody != "" {
e.dispatch(templateBody)
}
wgroup.Done()
}(event)
}
}
wgroup.Wait()
}
for _, event := range sender.events {
delete(sender.events, event.ID)
}
}
sender.Unlock()
}
func getNotifyTemplateBody(evt EventType) string {
if evt == LocationServersEvent {
return LocationServersNotifyBody
} else if evt == JobNotifyEvent {
return JobResultNotifyBody
}
return ""
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/cloudtask/cloudtask-center.git
git@gitee.com:cloudtask/cloudtask-center.git
cloudtask
cloudtask-center
cloudtask-center
v2.0.0

搜索帮助