mirror of
https://github.com/bufanyun/hotgo.git
synced 2025-10-13 13:33:44 +08:00
增加集群部署支持,修复定时任务分组添加后选项不显示
This commit is contained in:
16
server/internal/library/hgrds/pubsub/publish.go
Normal file
16
server/internal/library/hgrds/pubsub/publish.go
Normal file
@@ -0,0 +1,16 @@
|
||||
// Package pubsub
|
||||
// @Link https://github.com/bufanyun/hotgo
|
||||
// @Copyright Copyright (c) 2023 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
package pubsub
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
)
|
||||
|
||||
// Publish 推送消息
|
||||
func Publish(ctx context.Context, channel string, message interface{}) (int64, error) {
|
||||
return g.Redis().Publish(ctx, channel, message)
|
||||
}
|
68
server/internal/library/hgrds/pubsub/subscribe.go
Normal file
68
server/internal/library/hgrds/pubsub/subscribe.go
Normal file
@@ -0,0 +1,68 @@
|
||||
// Package pubsub
|
||||
// @Link https://github.com/bufanyun/hotgo
|
||||
// @Copyright Copyright (c) 2023 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
package pubsub
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/database/gredis"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gctx"
|
||||
"hotgo/utility/simple"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type SubHandler func(ctx context.Context, message *gredis.Message)
|
||||
|
||||
type subscribeManager struct {
|
||||
mutex sync.RWMutex
|
||||
List map[string]SubHandler
|
||||
}
|
||||
|
||||
var subscribes = &subscribeManager{
|
||||
List: make(map[string]SubHandler),
|
||||
}
|
||||
|
||||
// Subscribe 订阅消息
|
||||
func Subscribe(channel string, hr SubHandler) (err error) {
|
||||
subscribes.mutex.Lock()
|
||||
defer subscribes.mutex.Unlock()
|
||||
|
||||
if _, ok := subscribes.List[channel]; ok {
|
||||
err = gerror.Newf("repeat the subscribe:%v register", channel)
|
||||
return
|
||||
}
|
||||
subscribes.List[channel] = hr
|
||||
go doSubscribe(channel, hr)
|
||||
return
|
||||
}
|
||||
|
||||
func doSubscribe(channel string, hr SubHandler) {
|
||||
ctx := gctx.New()
|
||||
conn, err := g.Redis().Conn(ctx)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer conn.Close(ctx)
|
||||
|
||||
_, err = conn.Subscribe(ctx, channel)
|
||||
for {
|
||||
msg, err := conn.ReceiveMessage(ctx)
|
||||
if err != nil {
|
||||
g.Log().Warningf(ctx, "subscribe quit, err:%v", err)
|
||||
return
|
||||
}
|
||||
handleMessage(hr, msg)
|
||||
}
|
||||
}
|
||||
|
||||
func handleMessage(hr SubHandler, message *gredis.Message) {
|
||||
simple.SafeGo(gctx.New(), func(ctx context.Context) {
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
hr(ctx, message)
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user